Rework request context handler

This commit is contained in:
2024-11-03 19:17:10 +01:00
parent dfd9544fba
commit c8cc854fd6
14 changed files with 217 additions and 217 deletions

View File

@@ -4,32 +4,34 @@ import (
"novamd/internal/auth"
"novamd/internal/db"
"novamd/internal/filesystem"
"novamd/internal/handlers"
"novamd/internal/middleware"
"github.com/go-chi/chi/v5"
)
func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem, authMiddleware *auth.Middleware, sessionService *auth.SessionService) {
handler := &BaseHandler{
handler := &handlers.Handler{
DB: db,
FS: fs,
}
// Public routes (no authentication required)
r.Group(func(r chi.Router) {
r.Post("/auth/login", Login(sessionService, db))
r.Post("/auth/refresh", RefreshToken(sessionService))
r.Post("/auth/login", handler.Login(sessionService))
r.Post("/auth/refresh", handler.RefreshToken(sessionService))
})
// Protected routes (authentication required)
r.Group(func(r chi.Router) {
// Apply authentication middleware to all routes in this group
r.Use(authMiddleware.Authenticate)
r.Use(WithHandlerContext(db))
r.Use(middleware.WithHandlerContext(db))
// Auth routes
r.Post("/auth/logout", Logout(sessionService))
r.Get("/auth/me", handler.GetCurrentUser(db))
r.Post("/auth/logout", handler.Logout(sessionService))
r.Get("/auth/me", handler.GetCurrentUser())
// Admin-only routes
r.Group(func(r chi.Router) {
@@ -41,35 +43,35 @@ func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem, authMiddlew
// Workspace routes
r.Route("/workspaces", func(r chi.Router) {
r.Get("/", handler.ListWorkspaces(db))
r.Post("/", handler.CreateWorkspace(db, fs))
r.Get("/last", handler.GetLastWorkspace(db))
r.Put("/last", handler.UpdateLastWorkspace(db))
r.Get("/", handler.ListWorkspaces())
r.Post("/", handler.CreateWorkspace())
r.Get("/last", handler.GetLastWorkspace())
r.Put("/last", handler.UpdateLastWorkspace())
// Single workspace routes
r.Route("/{workspaceId}", func(r chi.Router) {
r.Use(authMiddleware.RequireWorkspaceOwnership(db))
r.Use(authMiddleware.RequireWorkspaceAccess)
r.Get("/", handler.GetWorkspace(db))
r.Put("/", handler.UpdateWorkspace(db, fs))
r.Delete("/", handler.DeleteWorkspace(db))
r.Get("/", handler.GetWorkspace())
r.Put("/", handler.UpdateWorkspace())
r.Delete("/", handler.DeleteWorkspace())
// File routes
r.Route("/files", func(r chi.Router) {
r.Get("/", handler.ListFiles(fs))
r.Get("/last", handler.GetLastOpenedFile(db, fs))
r.Put("/last", handler.UpdateLastOpenedFile(db, fs))
r.Get("/lookup", handler.LookupFileByName(fs))
r.Get("/", handler.ListFiles())
r.Get("/last", handler.GetLastOpenedFile())
r.Put("/last", handler.UpdateLastOpenedFile())
r.Get("/lookup", handler.LookupFileByName())
r.Post("/*", handler.SaveFile(fs))
r.Get("/*", handler.GetFileContent(fs))
r.Delete("/*", handler.DeleteFile(fs))
r.Post("/*", handler.SaveFile())
r.Get("/*", handler.GetFileContent())
r.Delete("/*", handler.DeleteFile())
})
// Git routes
r.Route("/git", func(r chi.Router) {
r.Post("/commit", handler.StageCommitAndPush(fs))
r.Post("/pull", handler.PullChanges(fs))
r.Post("/commit", handler.StageCommitAndPush())
r.Post("/pull", handler.PullChanges())
})
})
})