NewRouter creates and returns a new router
(app *app.App, rc RouteConfig)
| 105 | |
| 106 | // NewRouter creates and returns a new router |
| 107 | func NewRouter(app *app.App, rc RouteConfig) (http.Handler, error) { |
| 108 | if err := app.Validate(); err != nil { |
| 109 | return nil, errors.Wrap(err, "validating the app parameters") |
| 110 | } |
| 111 | |
| 112 | router := mux.NewRouter().StrictSlash(true) |
| 113 | |
| 114 | webRouter := router.PathPrefix("/").Subrouter() |
| 115 | apiRouter := router.PathPrefix("/api").Subrouter() |
| 116 | registerRoutes(webRouter, mw.WebMw, app, rc.WebRoutes) |
| 117 | registerRoutes(apiRouter, mw.APIMw, app, rc.APIRoutes) |
| 118 | |
| 119 | router.PathPrefix("/api/v1").Handler(mw.ApplyLimit(mw.NotSupported, true)) |
| 120 | router.PathPrefix("/api/v2").Handler(mw.ApplyLimit(mw.NotSupported, true)) |
| 121 | |
| 122 | // static |
| 123 | staticFs, err := assets.GetStaticFS() |
| 124 | if err != nil { |
| 125 | return nil, errors.Wrap(err, "getting the filesystem for static files") |
| 126 | } |
| 127 | |
| 128 | staticHandler := http.StripPrefix("/static/", http.FileServer(http.FS(staticFs))) |
| 129 | router.PathPrefix("/static/").Handler(staticHandler) |
| 130 | |
| 131 | router.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) { |
| 132 | w.Write([]byte("User-agent: *\nAllow: /")) |
| 133 | }) |
| 134 | |
| 135 | // catch-all |
| 136 | router.PathPrefix("/").HandlerFunc(rc.Controllers.Static.NotFound) |
| 137 | |
| 138 | return mw.Global(router), nil |
| 139 | } |
no test coverage detected