f8f209dcba
- Migration 005: schedule_entries table (date TEXT UNIQUE, one person per day)
- POST /api/schedule — assign user to one or more dates in a single
transaction; any date conflict rejects the whole request (409)
- GET /api/schedule — list all entries ordered by date, optional ?from/?to
- GET /api/schedule/current — today's on-call user (UTC date), 404 if none
- DELETE /api/schedule/{id} — remove an entry (204)
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
)
|
|
|
|
func NewRouter(db *sql.DB) http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.Logger)
|
|
r.Use(middleware.Recoverer)
|
|
|
|
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
respond(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
})
|
|
|
|
// Unauthenticated: bootstrap and Alertmanager webhook receiver.
|
|
r.Post("/api/bootstrap", handleBootstrap(db))
|
|
r.Post("/api/alertmanager/webhook", handleAlertmanagerWebhook(db))
|
|
|
|
// All other /api routes require a valid API key.
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(AuthMiddleware(db))
|
|
|
|
r.Get("/api/users", handleListUsers(db))
|
|
r.Post("/api/users", handleCreateUser(db))
|
|
r.Delete("/api/users/{id}", handleDeleteUser(db))
|
|
r.Post("/api/users/{id}/api-keys", handleCreateAPIKey(db))
|
|
r.Delete("/api/users/{id}/api-keys/{keyID}", handleDeleteAPIKey(db))
|
|
|
|
r.Get("/api/alerts", handleListAlerts(db))
|
|
r.Get("/api/alerts/{id}", handleGetAlert(db))
|
|
r.Post("/api/alerts/{id}/acknowledge", handleAcknowledge(db))
|
|
r.Delete("/api/alerts/{id}/acknowledge", handleUnacknowledge(db))
|
|
r.Get("/api/alerts/{id}/comments", handleListComments(db))
|
|
r.Post("/api/alerts/{id}/comments", handleCreateComment(db))
|
|
r.Delete("/api/alerts/{id}/comments/{commentID}", handleDeleteComment(db))
|
|
|
|
r.Post("/api/schedule", handleCreateSchedule(db))
|
|
r.Get("/api/schedule/current", handleCurrentSchedule(db)) // must be before /{id}
|
|
r.Get("/api/schedule", handleListSchedule(db))
|
|
r.Delete("/api/schedule/{id}", handleDeleteSchedule(db))
|
|
})
|
|
|
|
return r
|
|
}
|