Beispiel #1
0
// Returns the "Not found" response.
func notFound(w http.ResponseWriter, req *http.Request) {
	if lib.JSONEncoding(req) {
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprint(w, lib.Response{Error: "Something wrong happened!"})
	} else {
		w.Header().Set("Content-Type", "text/plain")
		fmt.Fprint(w, "404 Not Found")
	}
}
Beispiel #2
0
// It returns true (thus, accepting the route) if the current user is
// logged in, false otherwise.
func userLogged(req *http.Request, rm *mux.RouteMatch) bool {
	var rid string

	if lib.JSONEncoding(req) {
		rid = req.URL.Query().Get("token")
	} else if id, ok := lib.GetCookie(req, "userId").(string); ok {
		rid = id
	}
	return app.Exists("users", rid)
}
Beispiel #3
0
// TopicsCreate responds to: POST /topics
func TopicsCreate(res http.ResponseWriter, req *http.Request) {
	if lib.JSONEncoding(req) {
		TopicsCreateJSON(res, req)
		return
	}

	if t, err := createTopic(req.FormValue("name")); err != nil {
		http.Redirect(res, req, "/topics", http.StatusFound)
	} else {
		http.Redirect(res, req, "/topics/"+t.ID, http.StatusFound)
	}
}
Beispiel #4
0
// Login a user. It expects the "name" and "password" form values. Regardless
// if it was successful or not, it will redirect the user to the root path.
func Login(res http.ResponseWriter, req *http.Request) {
	// Check if the user exists and that the password is spot on.
	n, password := getNamePassword(req)
	id, err := matchPassword(n, password)
	if lib.CheckError(res, req, err) {
		return
	}

	// It's ok to login this user.
	if lib.JSONEncoding(req) {
		b, _ := json.Marshal(User{ID: id})
		fmt.Fprint(res, string(b))
	} else {
		lib.SetCookie(res, req, "userId", id)
		http.Redirect(res, req, "/", http.StatusFound)
	}
}
Beispiel #5
0
// Returns the name and the password parameters as given by the request. This
// method abstracts away the origin of these values.
func getNamePassword(req *http.Request) (string, string) {
	if lib.JSONEncoding(req) {
		if req.Body == nil {
			return "", ""
		}

		decoder := json.NewDecoder(req.Body)

		var t struct{ Name, Password string }
		err := decoder.Decode(&t)
		if err != nil {
			return "", ""
		}
		return t.Name, t.Password
	}
	return req.FormValue("name"), req.FormValue("password")
}
Beispiel #6
0
// TopicsIndex responds to: GET /topics
func TopicsIndex(res http.ResponseWriter, req *http.Request) {
	if lib.JSONEncoding(req) {
		TopicsIndexJSON(res, req)
		return
	}

	var err error
	var t Topic

	if id := lib.GetCookie(req, "topic"); id != "" && id != nil {
		err = Db.SelectOne(&t, "select * from topics where id=$1", id)
	} else {
		err = Db.SelectOne(&t, "select * from topics order by name limit 1")
	}
	if err != nil {
		log.Printf("Could not select topics: %v", err)
	}
	renderShow(res, &t, false)
}
Beispiel #7
0
// TopicsShow responds to: GET /topics/:id
func TopicsShow(res http.ResponseWriter, req *http.Request) {
	if lib.JSONEncoding(req) {
		TopicsShowJSON(res, req)
		return
	}

	var t Topic

	p := mux.Vars(req)
	err := Db.SelectOne(&t, "select * from topics where id=$1", p["id"])
	if err != nil {
		log.Printf("Could not select topic: %v", err)
	}
	if t.ID != "" {
		lib.SetCookie(res, req, "topic", t.ID)
	}
	print := req.URL.Query().Get("print") == "1"
	renderShow(res, &t, print)
}
Beispiel #8
0
// Returns true if this request should not let JSON requests pass.
func private(req *http.Request, rm *mux.RouteMatch) bool {
	return !lib.JSONEncoding(req)
}