예제 #1
0
func loginHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)

	username := r.FormValue("username")
	password := r.FormValue("password")

	u, err := models.FetchUser("username", username)
	if err != nil {
		coms.Error("Unable to login user")
		return
	}

	if err := u.Login(password); err != nil {
		coms.Errorf("Unable to login %v", err)
		return
	}

	claims := make(map[string]interface{})
	claims["id"] = u.ID
	claims["exp"] = time.Now().Add(time.Hour * 72).Unix()

	ts, err := restrict.Token(claims)
	if err != nil {
		coms.Fail("Failure signing the token")
		sentry.CaptureError(err, nil)
		return
	}

	coms.With(ts).OK()
}
예제 #2
0
func secretHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
	coms := communicator.New(w)

	u, err := models.FetchUser("id", t.Claims["id"])
	if err != nil {
		coms.Error("That user does not exist!")
		return
	}

	coms.With(u).OK()
}
예제 #3
0
func defaultServerHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)

	resp := struct {
		Server string `json:"server"`
	}{
		Server: *defaultIRCServer,
	}

	coms.With(resp).
		OK()
}
예제 #4
0
func getMenteeHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)

	i, err := strconv.Atoi(mux.Vars(r)["id"])
	if err != nil {
		coms.With(err).
			Fail("Could no cast id")
		return
	}

	coms.With(mentees[i-len(mentors)]).
		OK()
}
예제 #5
0
func postMessageHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)
	id, err := strconv.Atoi(r.FormValue("from"))
	if err != nil {
		coms.Fail("shit happened")
		return
	}

	m := Message{
		ID:      id,
		Message: r.FormValue("message"),
		Muted:   false,
	}

	messages = append(messages, m)

	coms.With(m).
		OK()
}
예제 #6
0
func registerHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)

	username := r.FormValue("username")
	password := r.FormValue("password")
	email := r.FormValue("email")

	log.WithFields(log.Fields{
		"username": username,
		"email":    email,
	}).Info("User is tryingt to register")

	u, err := models.NewUser(username, password, email)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Error("Unable to connect to user")

		coms.Error("Unable to create user")
		return
	}

	coms.With(u).OK("user")
}
예제 #7
0
func getMenteesHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)

	coms.With(mentees).
		OK("Here are your mentees!")
}
예제 #8
0
func getChatHandler(w http.ResponseWriter, r *http.Request) {
	coms := communicator.New(w)

	coms.With(messages).
		OK()
}