A web blog and personal homepage engine written in Go.
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.
 
 

51 lines
1.2 KiB

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