You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
806 B

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "time"
  6. "github.com/gorilla/mux"
  7. )
  8. type Route struct {
  9. Name string
  10. Method string
  11. Pattern string
  12. HandlerFunc http.HandlerFunc
  13. }
  14. func Logger(inner http.Handler, name string) http.Handler {
  15. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. start := time.Now()
  17. inner.ServeHTTP(w, r)
  18. log.Printf(
  19. "%s\t%s\t%s\t%s",
  20. r.Method,
  21. r.RequestURI,
  22. name,
  23. time.Since(start),
  24. )
  25. })
  26. }
  27. func NewRouter() *mux.Router {
  28. router := mux.NewRouter().StrictSlash(true)
  29. for _, route := range routes {
  30. var handler http.Handler
  31. handler = route.HandlerFunc
  32. handler = Logger(handler, route.Name)
  33. router.
  34. Methods(route.Method).
  35. Path(route.Pattern).
  36. Name(route.Name).
  37. Handler(handler)
  38. }
  39. return router
  40. }