Exemplo n.º 1
0
func NewServer() http.Handler {
	th := throttled.Interval(throttled.PerSec(10), 1, &throttled.VaryBy{Path: true}, 50)

	r := mux.NewRouter()

	// Handle Static files
	r.Handle("/css/{rest}", http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css/")))).Methods("GET")
	r.Handle("/js/{rest}", http.StripPrefix("/js/", http.FileServer(http.Dir("./public/js/")))).Methods("GET")
	r.Handle("/uploads/{rest}", http.StripPrefix("/uploads/", http.FileServer(http.Dir("./public/uploads/")))).Methods("GET")

	// Handle Root
	r.Handle("/", alice.New(th.Throttle, timeoutHandler).Then(http.HandlerFunc(Homepage))).Methods("GET")

	// Handle feeds
	r.HandleFunc("/feeds", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/feeds/rss", http.StatusFound)
	})
	r.HandleFunc("/feeds/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/feeds/rss", http.StatusFound)
	})
	r.HandleFunc("/feeds/atom", ReadFeed).Methods("GET")
	r.HandleFunc("/feeds/rss", ReadFeed).Methods("GET")

	// route: /post

	// Please note that `/new` route has to be before the `/:slug` route. Otherwise the program will try
	// to fetch for Post named "new".
	// For now I'll keep it this way to streamline route naming.
	r.Handle("/post/new", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "post/new", nil)
	}))).Methods("GET")
	r.Handle("/post/new", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(CreatePost))).Methods("POST")
	r.Handle("/post/search", alice.New(th.Throttle, timeoutHandler, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(SearchPost))).Methods("POST")
	r.HandleFunc("/post/{slug}", ReadPost).Methods("GET")
	r.Handle("/post/{slug}/edit", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(EditPost))).Methods("GET")
	r.Handle("/post/{slug}/edit", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(UpdatePost))).Methods("POST")
	r.Handle("/post/{slug}/delete", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(DeletePost))).Methods("GET")
	r.Handle("/post/{slug}/publish", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(PublishPost))).Methods("GET")
	r.Handle("/post/{slug}/unpublish", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(UnpublishPost))).Methods("GET")

	// route: /user
	r.Handle("/user", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(ReadUser))).Methods("GET")
	r.Handle("/user/login", alice.New(th.Throttle, timeoutHandler, SessionRedirect).Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "user/login", nil)
	}))).Methods("GET")
	r.Handle("/user/login", alice.New(th.Throttle, timeoutHandler, SessionRedirect, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(LoginUser))).Methods("POST")
	//r.Handle("/user/login", alice.New(th.Throttle, timeoutHandler).Then(http.HandlerFunc(LoginUser))).Methods("POST")
	r.HandleFunc("/user/logout", LogoutUser).Methods("GET")
	r.Handle("/user/settings", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(ReadBlogSettings))).Methods("GET")
	r.Handle("/user/settings", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(UpdateBlogSettings))).Methods("POST")
	r.Handle("/user/installation", alice.New(th.Throttle, timeoutHandler, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(UpdateBlogSettings))).Methods("POST")
	r.Handle("/user/register", alice.New(th.Throttle, timeoutHandler, SessionRedirect).Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "user/register", nil)
	}))).Methods("GET")
	r.Handle("/user/register", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(CreateUser))).Methods("POST")
	r.Handle("/user/recover", alice.New(th.Throttle, timeoutHandler, SessionRedirect).Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "user/recover", nil)
	}))).Methods("GET")
	r.Handle("/user/recover", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(RecoverUser))).Methods("POST")
	r.Handle("/user/reset/{id}/{recovery}", alice.New(th.Throttle, timeoutHandler, SessionRedirect).Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "user/reset", nil)
	}))).Methods("GET")
	r.Handle("/user/reset/{id}/{recovery}", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictWWWFormUrlEncoded).Then(http.HandlerFunc(ResetUserPassword))).Methods("POST")
	//r.Post("/delete", strict.ContentType("application/x-www-form-urlencoded"), ProtectedPage, binding.Form(User{}), DeleteUser)

	// route: /api
	r.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "api/index", nil)
	})
	r.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
		rend.HTML(w, http.StatusOK, "api/index", nil)
	})
	r.Handle("/api/settings", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(ReadBlogSettings))).Methods("GET")
	r.Handle("/api/settings", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictJSON).Then(http.HandlerFunc(UpdateBlogSettings))).Methods("POST")
	r.Handle("/api/installation", alice.New(th.Throttle, timeoutHandler, StrictJSON).Then(http.HandlerFunc(UpdateBlogSettings))).Methods("POST")
	r.HandleFunc("/api/users", ReadUsers).Methods("GET")

	r.Handle("/api/user/login", alice.New(th.Throttle, timeoutHandler, SessionRedirect, StrictJSON).Then(http.HandlerFunc(LoginUser))).Methods("POST")
	//r.Handle("/api/user/login", alice.New(th.Throttle, timeoutHandler).Then(http.HandlerFunc(LoginUser))).Methods("POST")
	r.HandleFunc("/api/user/logout", LogoutUser).Methods("GET")
	r.Handle("/api/user/recover", alice.New(th.Throttle, timeoutHandler, StrictJSON).Then(http.HandlerFunc(RecoverUser))).Methods("POST")
	r.Handle("/api/user/reset/{id}/{recovery}", alice.New(th.Throttle, timeoutHandler, StrictJSON).Then(http.HandlerFunc(ResetUserPassword))).Methods("POST")

	r.HandleFunc("/api/user/{id}", ReadUser).Methods("GET")
	//r.Delete("/user", DeleteUser)
	r.Handle("/api/user", alice.New(th.Throttle, timeoutHandler, StrictJSON).Then(http.HandlerFunc(CreateUser))).Methods("POST")
	r.HandleFunc("/api/posts", ReadPosts).Methods("GET")
	r.HandleFunc("/api/post/{slug}", ReadPost).Methods("GET")
	r.Handle("/api/post/{slug}/edit", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictJSON).Then(http.HandlerFunc(UpdatePost))).Methods("POST")
	r.Handle("/api/post/{slug}/publish", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(PublishPost))).Methods("GET")
	r.Handle("/api/post/{slug}/unpublish", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(UnpublishPost))).Methods("GET")
	r.Handle("/api/post/{slug}/delete", alice.New(th.Throttle, timeoutHandler, ProtectedPage).Then(http.HandlerFunc(DeletePost))).Methods("GET")
	r.Handle("/api/post", alice.New(th.Throttle, timeoutHandler, ProtectedPage, StrictJSON).Then(http.HandlerFunc(CreatePost))).Methods("POST")
	r.Handle("/api/post/search", alice.New(th.Throttle, timeoutHandler, StrictJSON).Then(http.HandlerFunc(SearchPost))).Methods("POST")

	return alice.New(Logger).Then(r)
}
Exemplo n.º 2
0
func throttle(next http.Handler, o ServerOptions) http.Handler {
	th := throttled.Interval(throttled.PerSec(o.Concurrency), o.Burst, &throttled.VaryBy{Method: true}, o.Burst)
	return th.Throttle(next)
}