RegisterSystemHandler provides system level handlers
(site *core.Site, pub publisher, cache *util.ParamCache, auth auth.Auth, shutdown func(), configFile string, remoteAccess *remote.Remote)
| 232 | |
| 233 | // RegisterSystemHandler provides system level handlers |
| 234 | func (s *HTTPd) RegisterSystemHandler(site *core.Site, pub publisher, cache *util.ParamCache, auth auth.Auth, shutdown func(), configFile string, remoteAccess *remote.Remote) { |
| 235 | router := s.Server.Handler.(*mux.Router) |
| 236 | |
| 237 | // api |
| 238 | api := router.PathPrefix("/api").Subrouter() |
| 239 | api.Use(jsonHandler) |
| 240 | api.Use(handlers.CompressHandler) |
| 241 | api.Use(handlers.CORS( |
| 242 | handlers.AllowedHeaders([]string{"Content-Type"}), |
| 243 | )) |
| 244 | |
| 245 | if site == nil { |
| 246 | // If site is nil, create a new empty site. Settings will be loaded during this process and |
| 247 | // site meter references and title can be updated using APIs. |
| 248 | var err error |
| 249 | site, err = core.NewSiteFromConfig(nil) |
| 250 | if err != nil { |
| 251 | // should not happen |
| 252 | panic(err) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | { // /api |
| 257 | routes := map[string]route{ |
| 258 | "state": {"GET", "/state", stateHandler(cache)}, |
| 259 | } |
| 260 | |
| 261 | for _, r := range routes { |
| 262 | api.Methods(r.Methods()...).Path(r.Pattern).Handler(r.HandlerFunc) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | { |
| 267 | // api/auth |
| 268 | api := api.PathPrefix("/auth").Subrouter() |
| 269 | |
| 270 | routes := map[string]route{ |
| 271 | "password": {"PUT", "/password", updatePasswordHandler(auth)}, |
| 272 | "auth": {"GET", "/status", authStatusHandler(auth)}, |
| 273 | "login": {"POST", "/login", loginHandler(auth)}, |
| 274 | "logout": {"POST", "/logout", logoutHandler}, |
| 275 | } |
| 276 | |
| 277 | for _, r := range routes { |
| 278 | api.Methods(r.Methods()...).Path(r.Pattern).Handler(r.HandlerFunc) |
| 279 | } |
| 280 | |
| 281 | // API key endpoints require an authenticated session. |
| 282 | ensureAuth := ensureAuthHandler(auth) |
| 283 | api.Methods("GET").Path("/apikey").Handler(ensureAuth(apiKeyStatusHandler(auth))) |
| 284 | api.Methods("POST").Path("/apikey").Handler(ensureAuth(regenerateApiKeyHandler(auth))) |
| 285 | } |
| 286 | |
| 287 | { // api/config |
| 288 | api := api.PathPrefix("/config").Subrouter() |
| 289 | api.Use(ensureAuthHandler(auth)) |
| 290 | |
| 291 | routes := map[string]route{ |
no test coverage detected