Пример #1
0
func main() {
	flag.Parse()

	var ok, ko int
	var mu sync.Mutex

	// Keep start time to log since-time
	start := time.Now()

	// Create the interval throttle
	t := throttled.Interval(throttled.D(*delay), *bursts, nil, 0)
	// Set its denied handler
	t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if *output == "v" || *output == "ko" {
			log.Printf("%s: KO: %s", r.URL.Path, time.Since(start))
		}
		throttled.DefaultDeniedHandler.ServeHTTP(w, r)
		mu.Lock()
		defer mu.Unlock()
		ko++
	})
	// Create OK handlers
	rand.Seed(time.Now().Unix())
	makeHandler := func(ix int) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if *output == "v" || *output == "ok" {
				log.Printf("handler %d: %s: ok: %s", ix, r.URL.Path, time.Since(start))
			}
			if *delayRes > 0 {
				wait := time.Duration(rand.Intn(int(*delayRes)))
				time.Sleep(wait)
			}
			w.WriteHeader(200)
			mu.Lock()
			defer mu.Unlock()
			ok++
		})
	}
	// Throttle them using the same interval throttler
	h1 := t.Throttle(makeHandler(1))
	h2 := t.Throttle(makeHandler(2))

	// Handle two paths
	mux := http.NewServeMux()
	mux.Handle("/a", h1)
	mux.Handle("/b", h2)

	// Print stats once in a while
	go func() {
		for _ = range time.Tick(10 * time.Second) {
			mu.Lock()
			log.Printf("ok: %d, ko: %d", ok, ko)
			mu.Unlock()
		}
	}()
	fmt.Println("server listening on port 9000")
	http.ListenAndServe(":9000", mux)
}
Пример #2
0
func main() {
	flag.Parse()

	var h http.Handler
	var ok, ko int
	var mu sync.Mutex

	// Keep the start time to print since-time
	start := time.Now()

	// Create the interval throttler
	t := throttled.Interval(throttled.D(*delay), *bursts, &throttled.VaryBy{
		Path: true,
	}, *maxkeys)
	// Set the denied handler
	t.DeniedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if *output == "v" || *output == "ko" {
			log.Printf("KO: %s", time.Since(start))
		}
		throttled.DefaultDeniedHandler.ServeHTTP(w, r)
		mu.Lock()
		defer mu.Unlock()
		ko++
	})

	// Throttle the OK handler
	rand.Seed(time.Now().Unix())
	h = t.Throttle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if *output == "v" || *output == "ok" {
			log.Printf("%s: ok: %s", r.URL.Path, time.Since(start))
		}
		if *delayRes > 0 {
			wait := time.Duration(rand.Intn(int(*delayRes)))
			time.Sleep(wait)
		}
		w.WriteHeader(200)
		mu.Lock()
		defer mu.Unlock()
		ok++
	}))

	// Print stats once in a while
	go func() {
		for _ = range time.Tick(10 * time.Second) {
			mu.Lock()
			log.Printf("ok: %d, ko: %d", ok, ko)
			mu.Unlock()
		}
	}()
	fmt.Println("server listening on port 9000")
	http.ListenAndServe(":9000", h)
}
Пример #3
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)
}
Пример #4
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)
}