Some checks failed
Deploy to NAS / deploy (push) Failing after 26s
- Public feed (/) with infinite scroll via Intersection Observer - Self-registration (/register) - Admin area (/admin/entries, /admin/users) with user management - journal_entries: visibility (public/private) + hashtags fields - users: is_admin flag - DB schema updated (recreate DB to apply) - CI: run go test via docker run (golang:1.25-alpine) — fixes 'go not found' Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
"github.com/jacek/pamietnik/backend/internal/auth"
|
|
"github.com/jacek/pamietnik/backend/internal/db"
|
|
)
|
|
|
|
func NewRouter(
|
|
authStore *auth.Store,
|
|
tpStore *db.TrackpointStore,
|
|
stopStore *db.StopStore,
|
|
suggStore *db.SuggestionStore,
|
|
journalStore *db.JournalStore,
|
|
userStore *db.UserStore,
|
|
uploadDir string,
|
|
) http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.Logger)
|
|
r.Use(middleware.Recoverer)
|
|
|
|
webUI := NewWebUI(authStore, tpStore, stopStore, journalStore, userStore)
|
|
journalHandler := NewJournalHandler(journalStore, uploadDir)
|
|
authMW := RequireAuth(authStore)
|
|
|
|
// Health
|
|
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
})
|
|
r.Get("/readyz", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
})
|
|
|
|
// Ingest (session auth; Android API-Key auth TBD)
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMW)
|
|
r.Post("/v1/trackpoints", HandleSingleTrackpoint(tpStore))
|
|
r.Post("/v1/trackpoints:batch", HandleBatchTrackpoints(tpStore))
|
|
})
|
|
|
|
// Query API (session auth)
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMW)
|
|
r.Get("/v1/days", HandleListDays(tpStore))
|
|
r.Get("/v1/trackpoints", HandleListTrackpoints(tpStore))
|
|
r.Get("/v1/stops", HandleListStops(stopStore))
|
|
r.Get("/v1/suggestions", HandleListSuggestions(suggStore))
|
|
})
|
|
|
|
// Static assets (CSS etc.)
|
|
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS()))))
|
|
|
|
// Public routes (no auth required)
|
|
r.Get("/", webUI.HandleFeed)
|
|
r.Get("/feed", webUI.HandleFeedFragment)
|
|
r.Get("/register", webUI.HandleGetRegister)
|
|
r.Post("/register", webUI.HandlePostRegister)
|
|
r.Get("/login", webUI.HandleGetLogin)
|
|
r.Post("/login", webUI.HandlePostLogin)
|
|
r.Post("/logout", webUI.HandleLogout)
|
|
|
|
// Authenticated web routes
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMW)
|
|
r.Get("/days", webUI.HandleDaysList)
|
|
r.Get("/days/redirect", webUI.HandleDaysRedirect)
|
|
r.Get("/days/{date}", webUI.HandleDayDetail)
|
|
r.Post("/entries", journalHandler.HandleCreateEntry)
|
|
})
|
|
|
|
// Admin routes
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMW)
|
|
r.Use(requireAdmin)
|
|
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "/admin/entries", http.StatusSeeOther)
|
|
})
|
|
r.Get("/admin/entries", webUI.HandleAdminEntries)
|
|
r.Get("/admin/users", webUI.HandleAdminUsers)
|
|
r.Post("/admin/users", webUI.HandleAdminCreateUser)
|
|
r.Delete("/admin/users/{id}", webUI.HandleAdminDeleteUser)
|
|
})
|
|
|
|
// Serve uploaded images
|
|
r.Handle("/uploads/*", http.StripPrefix("/uploads/", http.FileServer(http.Dir(uploadDir))))
|
|
|
|
// SPA (Vite webapp) — served under /app/*
|
|
spaPrefix := "/app"
|
|
r.Handle(spaPrefix, http.RedirectHandler(spaPrefix+"/", http.StatusMovedPermanently))
|
|
r.Handle(spaPrefix+"/*", http.StripPrefix(spaPrefix, SPAHandler(spaPrefix)))
|
|
|
|
return r
|
|
}
|