A web blog and personal homepage engine written in Go.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

59 lignes
1.6 KiB

  1. package gophertype
  2. import (
  3. "net/http"
  4. "git.kirsle.net/apps/gophertype/pkg/authentication"
  5. "git.kirsle.net/apps/gophertype/pkg/console"
  6. "git.kirsle.net/apps/gophertype/pkg/controllers"
  7. "git.kirsle.net/apps/gophertype/pkg/glue"
  8. "git.kirsle.net/apps/gophertype/pkg/middleware"
  9. "git.kirsle.net/apps/gophertype/pkg/session"
  10. "github.com/gorilla/mux"
  11. )
  12. // SetupRouter sets up the HTTP router.
  13. func (s *Site) SetupRouter() error {
  14. router := mux.NewRouter()
  15. router.Use(session.Middleware)
  16. router.Use(authentication.Middleware)
  17. router.Use(middleware.CSRF)
  18. router.Use(middleware.AgeGate)
  19. console.Debug("Setting up HTTP Router")
  20. for _, route := range glue.GetControllers() {
  21. console.Debug("Register: %+v", route)
  22. if len(route.Methods) == 0 {
  23. route.Methods = []string{"GET"}
  24. }
  25. route.Methods = append(route.Methods, "HEAD")
  26. if len(route.Middleware) > 0 {
  27. console.Debug("%+v has middlewares!", route)
  28. handler := route.Middleware[0](http.HandlerFunc(route.Handler))
  29. router.Handle(route.Path, handler).Methods(route.Methods...)
  30. } else {
  31. router.HandleFunc(route.Path, route.Handler).Methods(route.Methods...)
  32. }
  33. }
  34. router.PathPrefix("/").HandlerFunc(controllers.CatchAllHandler)
  35. router.NotFoundHandler = http.HandlerFunc(controllers.CatchAllHandler)
  36. console.Debug("Walk the mux.Router:")
  37. router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
  38. tpl, err1 := route.GetPathTemplate()
  39. met, err2 := route.GetMethods()
  40. console.Debug("path:%s methods:%s path-err:%s met-err:%s", tpl, met, err1, err2)
  41. return nil
  42. })
  43. s.mux = router
  44. s.n.UseHandler(router)
  45. return nil
  46. }