RegisterAPI registers core API routes on the given mux. authMiddleware wraps protected routes; pass a no-op when auth is disabled.
(mux *http.ServeMux, store event.Store, previews *event.PreviewRegistry, es *EventService, version string, db *sql.DB, enabledEvents []string, authSettings AuthSettings, authMiddleware func(http.Handler) http.Handler)
| 17 | // RegisterAPI registers core API routes on the given mux. |
| 18 | // authMiddleware wraps protected routes; pass a no-op when auth is disabled. |
| 19 | func RegisterAPI(mux *http.ServeMux, store event.Store, previews *event.PreviewRegistry, es *EventService, version string, db *sql.DB, enabledEvents []string, authSettings AuthSettings, authMiddleware func(http.Handler) http.Handler) { |
| 20 | // Public routes (no auth required). |
| 21 | mux.HandleFunc("GET /api/version", func(w http.ResponseWriter, r *http.Request) { |
| 22 | writeJSON(w, map[string]string{"version": version}) |
| 23 | }) |
| 24 | |
| 25 | mux.HandleFunc("GET /api/settings", func(w http.ResponseWriter, r *http.Request) { |
| 26 | writeJSON(w, map[string]any{ |
| 27 | "auth": map[string]any{ |
| 28 | "enabled": authSettings.Enabled, |
| 29 | "login_url": authSettings.LoginURL, |
| 30 | }, |
| 31 | "version": version, |
| 32 | "events": enabledEvents, |
| 33 | }) |
| 34 | }) |
| 35 | |
| 36 | // Protected routes (require auth when enabled). |
| 37 | protect := func(pattern string, handler http.HandlerFunc) { |
| 38 | mux.Handle(pattern, authMiddleware(handler)) |
| 39 | } |
| 40 | |
| 41 | // List events. |
| 42 | protect("GET /api/events", func(w http.ResponseWriter, r *http.Request) { |
| 43 | opts := event.FindOptions{ |
| 44 | Type: r.URL.Query().Get("type"), |
| 45 | Project: r.URL.Query().Get("project"), |
| 46 | } |
| 47 | events, err := store.FindAll(r.Context(), opts) |
| 48 | if err != nil { |
| 49 | writeError(w, err.Error(), http.StatusInternalServerError) |
| 50 | return |
| 51 | } |
| 52 | if events == nil { |
| 53 | events = []event.Event{} |
| 54 | } |
| 55 | writeJSON(w, map[string]any{"data": events, "meta": map[string]any{}}) |
| 56 | }) |
| 57 | |
| 58 | // List event previews. |
| 59 | protect("GET /api/events/preview", func(w http.ResponseWriter, r *http.Request) { |
| 60 | opts := event.FindOptions{ |
| 61 | Type: r.URL.Query().Get("type"), |
| 62 | Project: r.URL.Query().Get("project"), |
| 63 | } |
| 64 | events, err := store.FindAll(r.Context(), opts) |
| 65 | if err != nil { |
| 66 | writeError(w, err.Error(), http.StatusInternalServerError) |
| 67 | return |
| 68 | } |
| 69 | result := make([]event.Preview, 0, len(events)) |
| 70 | for _, ev := range events { |
| 71 | result = append(result, previews.BuildPreview(ev)) |
| 72 | } |
| 73 | writeJSON(w, map[string]any{"data": result, "meta": map[string]any{}}) |
| 74 | }) |
| 75 | |
| 76 | // Get single event. |
nothing calls this directly
no test coverage detected