- feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders - feat(cli): add check and audit commands - feat(api): add api framework, login, page routes and account, user, delegation and audit handlers - test(auth): cover passwords, sessions, rate limiter, authorization and audit builders - test(api): cover the api framework, sessions, client origin, headers and cross-origin refusal - test(api): cover account, user, delegation and audit handlers and the authorization matrix - test(ui): cover login, logout, page routes and the page authorization matrix
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type routeRegistrar func(s *WebServer, r *mux.Router)
|
|
|
|
type handlerMiddleware func(s *WebServer, next http.Handler) http.Handler
|
|
|
|
var appRouteRegistrars = []routeRegistrar{registerAppRoutes}
|
|
|
|
var appMiddlewares = []handlerMiddleware{requestLogMiddleware, securityHeadersMiddleware, crossOriginMiddleware}
|
|
|
|
func (s *WebServer) Handler() http.Handler {
|
|
r := mux.NewRouter()
|
|
s.Routes(r)
|
|
var h http.Handler = r
|
|
for i := len(appMiddlewares) - 1; i >= 0; i-- {
|
|
h = appMiddlewares[i](s, h)
|
|
}
|
|
return h
|
|
}
|
|
|
|
func (s *WebServer) Routes(r *mux.Router) {
|
|
r.HandleFunc("/health", s.handleHealth).Methods(http.MethodGet)
|
|
r.HandleFunc("/version", s.handleVersion).Methods(http.MethodGet)
|
|
for _, register := range appRouteRegistrars {
|
|
register(s, r)
|
|
}
|
|
}
|
|
|
|
const (
|
|
healthOK = `{"status":"ok"}`
|
|
healthUnavailable = `{"status":"unavailable"}`
|
|
)
|
|
|
|
func (s *WebServer) handleHealth(w http.ResponseWriter, r *http.Request) {
|
|
status, body := http.StatusOK, healthOK
|
|
ctx, cancel := context.WithTimeout(r.Context(), healthPingTimeout)
|
|
defer cancel()
|
|
if s.pinger == nil {
|
|
status, body = http.StatusServiceUnavailable, healthUnavailable
|
|
} else if err := s.pinger.PingContext(ctx); err != nil {
|
|
s.log.ErrorContext(r.Context(), "health check ping failed", "error", err)
|
|
status, body = http.StatusServiceUnavailable, healthUnavailable
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
w.WriteHeader(status)
|
|
_, _ = w.Write([]byte(body))
|
|
}
|