Scaffold full-stack volunteer scheduling application
Go backend with domain-based packages (volunteer, schedule, timeoff, checkin, notification), SQLite storage, JWT auth, and chi router. React TypeScript frontend with routing, auth context, and pages for all core features. Multi-stage Dockerfile and docker-compose included. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
49
internal/server/middleware/auth.go
Normal file
49
internal/server/middleware/auth.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.unsupervised.ca/walkies/internal/auth"
|
||||
"git.unsupervised.ca/walkies/internal/respond"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const claimsKey contextKey = "claims"
|
||||
|
||||
func Authenticate(authSvc *auth.Service) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
header := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(header, "Bearer ") {
|
||||
respond.Error(w, http.StatusUnauthorized, "missing or invalid authorization header")
|
||||
return
|
||||
}
|
||||
claims, err := authSvc.Parse(strings.TrimPrefix(header, "Bearer "))
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusUnauthorized, "invalid token")
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), claimsKey, claims)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func RequireAdmin(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
claims := ClaimsFromContext(r.Context())
|
||||
if claims == nil || claims.Role != "admin" {
|
||||
respond.Error(w, http.StatusForbidden, "admin access required")
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func ClaimsFromContext(ctx context.Context) *auth.Claims {
|
||||
claims, _ := ctx.Value(claimsKey).(*auth.Claims)
|
||||
return claims
|
||||
}
|
||||
83
internal/server/server.go
Normal file
83
internal/server/server.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
|
||||
"git.unsupervised.ca/walkies/internal/auth"
|
||||
"git.unsupervised.ca/walkies/internal/checkin"
|
||||
"git.unsupervised.ca/walkies/internal/notification"
|
||||
"git.unsupervised.ca/walkies/internal/schedule"
|
||||
"git.unsupervised.ca/walkies/internal/server/middleware"
|
||||
"git.unsupervised.ca/walkies/internal/timeoff"
|
||||
"git.unsupervised.ca/walkies/internal/volunteer"
|
||||
)
|
||||
|
||||
func New(db *sql.DB, jwtSecret string, staticDir string) http.Handler {
|
||||
authSvc := auth.NewService(db, jwtSecret)
|
||||
|
||||
volunteerStore := volunteer.NewStore(db)
|
||||
volunteerHandler := volunteer.NewHandler(volunteerStore, authSvc)
|
||||
|
||||
scheduleStore := schedule.NewStore(db)
|
||||
scheduleHandler := schedule.NewHandler(scheduleStore)
|
||||
|
||||
timeoffStore := timeoff.NewStore(db)
|
||||
timeoffHandler := timeoff.NewHandler(timeoffStore)
|
||||
|
||||
checkinStore := checkin.NewStore(db)
|
||||
checkinHandler := checkin.NewHandler(checkinStore)
|
||||
|
||||
notificationStore := notification.NewStore(db)
|
||||
notificationHandler := notification.NewHandler(notificationStore)
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(chimiddleware.Logger)
|
||||
r.Use(chimiddleware.Recoverer)
|
||||
r.Use(chimiddleware.RealIP)
|
||||
|
||||
// API routes
|
||||
r.Route("/api/v1", func(r chi.Router) {
|
||||
// Public auth endpoints
|
||||
r.Post("/auth/register", volunteerHandler.Register)
|
||||
r.Post("/auth/login", volunteerHandler.Login)
|
||||
|
||||
// Protected routes
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(middleware.Authenticate(authSvc))
|
||||
|
||||
// Volunteers
|
||||
r.Get("/volunteers", volunteerHandler.List)
|
||||
r.Get("/volunteers/{id}", volunteerHandler.Get)
|
||||
r.With(middleware.RequireAdmin).Put("/volunteers/{id}", volunteerHandler.Update)
|
||||
|
||||
// Schedules
|
||||
r.Get("/schedules", scheduleHandler.List)
|
||||
r.Post("/schedules", scheduleHandler.Create)
|
||||
r.With(middleware.RequireAdmin).Put("/schedules/{id}", scheduleHandler.Update)
|
||||
r.With(middleware.RequireAdmin).Delete("/schedules/{id}", scheduleHandler.Delete)
|
||||
|
||||
// Time off
|
||||
r.Get("/timeoff", timeoffHandler.List)
|
||||
r.Post("/timeoff", timeoffHandler.Create)
|
||||
r.With(middleware.RequireAdmin).Put("/timeoff/{id}/review", timeoffHandler.Review)
|
||||
|
||||
// Check-in / check-out
|
||||
r.Post("/checkin", checkinHandler.CheckIn)
|
||||
r.Post("/checkout", checkinHandler.CheckOut)
|
||||
r.Get("/checkin/history", checkinHandler.History)
|
||||
|
||||
// Notifications
|
||||
r.Get("/notifications", notificationHandler.List)
|
||||
r.Put("/notifications/{id}/read", notificationHandler.MarkRead)
|
||||
})
|
||||
})
|
||||
|
||||
// Serve static React app for all other routes
|
||||
r.Handle("/*", http.FileServer(http.Dir(staticDir)))
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user