예제 #1
0
파일: routing.go 프로젝트: mssola/todo
// 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")
	}
}
예제 #2
0
파일: routing.go 프로젝트: mssola/todo
// 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)
}
예제 #3
0
파일: topics.go 프로젝트: mssola/todo
// 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)
	}
}
예제 #4
0
파일: sessions.go 프로젝트: mssola/todo
// 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)
	}
}
예제 #5
0
파일: sessions.go 프로젝트: mssola/todo
// 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")
}
예제 #6
0
파일: topics.go 프로젝트: mssola/todo
// 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)
}
예제 #7
0
파일: topics.go 프로젝트: mssola/todo
// 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)
}
예제 #8
0
파일: routing.go 프로젝트: mssola/todo
// Returns true if this request should not let JSON requests pass.
func private(req *http.Request, rm *mux.RouteMatch) bool {
	return !lib.JSONEncoding(req)
}