mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Reorganize handlers
This commit is contained in:
@@ -127,17 +127,15 @@ func RefreshToken(authService *auth.SessionService) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCurrentUser returns the currently authenticated user
|
// GetCurrentUser returns the currently authenticated user
|
||||||
func GetCurrentUser(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) GetCurrentUser(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get user claims from context (set by auth middleware)
|
ctx, ok := h.getContext(w, r)
|
||||||
claims, err := auth.GetUserFromContext(r.Context())
|
if !ok {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user from database
|
// Get user from database
|
||||||
user, err := db.GetUserByID(claims.UserID)
|
user, err := db.GetUserByID(ctx.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "User not found", http.StatusNotFound)
|
http.Error(w, "User not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
|||||||
85
backend/internal/api/context.go
Normal file
85
backend/internal/api/context.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
// api/context.go
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"novamd/internal/auth"
|
||||||
|
"novamd/internal/db"
|
||||||
|
"novamd/internal/filesystem"
|
||||||
|
"novamd/internal/models"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HandlerContext struct {
|
||||||
|
UserID int
|
||||||
|
UserRole string
|
||||||
|
Workspace *models.Workspace
|
||||||
|
}
|
||||||
|
|
||||||
|
type contextKey string
|
||||||
|
|
||||||
|
const handlerContextKey contextKey = "handlerContext"
|
||||||
|
|
||||||
|
// Middleware to populate handler context
|
||||||
|
func WithHandlerContext(db *db.DB) func(http.Handler) http.Handler {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Get user claims from auth context
|
||||||
|
claims, err := auth.GetUserFromContext(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get workspace name from URL if it exists
|
||||||
|
workspaceName := chi.URLParam(r, "workspaceName")
|
||||||
|
|
||||||
|
var workspace *models.Workspace
|
||||||
|
// Only look up workspace if name is provided
|
||||||
|
if workspaceName != "" {
|
||||||
|
workspace, err = db.GetWorkspaceByName(claims.UserID, workspaceName)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Workspace not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create handler context
|
||||||
|
ctx := &HandlerContext{
|
||||||
|
UserID: claims.UserID,
|
||||||
|
UserRole: claims.Role,
|
||||||
|
Workspace: workspace,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to request context
|
||||||
|
reqCtx := context.WithValue(r.Context(), handlerContextKey, ctx)
|
||||||
|
next.ServeHTTP(w, r.WithContext(reqCtx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to get handler context
|
||||||
|
func GetHandlerContext(r *http.Request) *HandlerContext {
|
||||||
|
ctx := r.Context().Value(handlerContextKey)
|
||||||
|
if ctx == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ctx.(*HandlerContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
type BaseHandler struct {
|
||||||
|
DB *db.DB
|
||||||
|
FS *filesystem.FileSystem
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to get context and handle errors
|
||||||
|
func (h *BaseHandler) getContext(w http.ResponseWriter, r *http.Request) (*HandlerContext, bool) {
|
||||||
|
ctx := GetHandlerContext(r)
|
||||||
|
if ctx == nil {
|
||||||
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
return ctx, true
|
||||||
|
}
|
||||||
@@ -11,15 +11,14 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListFiles(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) ListFiles(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := fs.ListFilesRecursively(userID, workspaceID)
|
files, err := fs.ListFilesRecursively(ctx.UserID, ctx.Workspace.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to list files", http.StatusInternalServerError)
|
http.Error(w, "Failed to list files", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -29,11 +28,10 @@ func ListFiles(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LookupFileByName(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) LookupFileByName(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +41,7 @@ func LookupFileByName(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePaths, err := fs.FindFileByName(userID, workspaceID, filename)
|
filePaths, err := fs.FindFileByName(ctx.UserID, ctx.Workspace.ID, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "File not found", http.StatusNotFound)
|
http.Error(w, "File not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
@@ -53,16 +51,15 @@ func LookupFileByName(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFileContent(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) GetFileContent(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := chi.URLParam(r, "*")
|
filePath := chi.URLParam(r, "*")
|
||||||
content, err := fs.GetFileContent(userID, workspaceID, filePath)
|
content, err := fs.GetFileContent(ctx.UserID, ctx.Workspace.ID, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to read file", http.StatusNotFound)
|
http.Error(w, "Failed to read file", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
@@ -73,11 +70,10 @@ func GetFileContent(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +84,7 @@ func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = fs.SaveFile(userID, workspaceID, filePath, content)
|
err = fs.SaveFile(ctx.UserID, ctx.Workspace.ID, filePath, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -98,16 +94,15 @@ func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := chi.URLParam(r, "*")
|
filePath := chi.URLParam(r, "*")
|
||||||
err = fs.DeleteFile(userID, workspaceID, filePath)
|
err := fs.DeleteFile(ctx.UserID, ctx.Workspace.ID, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to delete file", http.StatusInternalServerError)
|
http.Error(w, "Failed to delete file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -118,29 +113,32 @@ func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLastOpenedFile(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) GetLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath, err := db.GetLastOpenedFile(workspaceID)
|
filePath, err := db.GetLastOpenedFile(ctx.Workspace.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get last opened file", http.StatusInternalServerError)
|
http.Error(w, "Failed to get last opened file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err := fs.ValidatePath(ctx.UserID, ctx.Workspace.ID, filePath); err != nil {
|
||||||
|
http.Error(w, "Invalid file path", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
respondJSON(w, map[string]string{"lastOpenedFilePath": filePath})
|
respondJSON(w, map[string]string{"lastOpenedFilePath": filePath})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) UpdateLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,13 +153,13 @@ func UpdateLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc
|
|||||||
|
|
||||||
// Validate the file path exists in the workspace
|
// Validate the file path exists in the workspace
|
||||||
if requestBody.FilePath != "" {
|
if requestBody.FilePath != "" {
|
||||||
if _, err := fs.ValidatePath(userID, workspaceID, requestBody.FilePath); err != nil {
|
if _, err := fs.ValidatePath(ctx.UserID, ctx.Workspace.ID, requestBody.FilePath); err != nil {
|
||||||
http.Error(w, "Invalid file path", http.StatusBadRequest)
|
http.Error(w, "Invalid file path", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.UpdateLastOpenedFile(workspaceID, requestBody.FilePath); err != nil {
|
if err := db.UpdateLastOpenedFile(ctx.Workspace.ID, requestBody.FilePath); err != nil {
|
||||||
http.Error(w, "Failed to update last opened file", http.StatusInternalServerError)
|
http.Error(w, "Failed to update last opened file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,10 @@ import (
|
|||||||
"novamd/internal/filesystem"
|
"novamd/internal/filesystem"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +28,7 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = fs.StageCommitAndPush(userID, workspaceID, requestBody.Message)
|
err := fs.StageCommitAndPush(ctx.UserID, ctx.Workspace.ID, requestBody.Message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to stage, commit, and push changes: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to stage, commit, and push changes: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -39,15 +38,14 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PullChanges(fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) PullChanges(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = fs.Pull(userID, workspaceID)
|
err := fs.Pull(ctx.UserID, ctx.Workspace.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to pull changes: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to pull changes: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,24 +9,14 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getUserID(r *http.Request) (int, error) {
|
func getWorkspaceID(r *http.Request) (int, error) {
|
||||||
userIDStr := chi.URLParam(r, "userId")
|
|
||||||
return strconv.Atoi(userIDStr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUserAndWorkspaceIDs(r *http.Request) (int, int, error) {
|
|
||||||
userID, err := getUserID(r)
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, errors.New("invalid userId")
|
|
||||||
}
|
|
||||||
|
|
||||||
workspaceIDStr := chi.URLParam(r, "workspaceId")
|
workspaceIDStr := chi.URLParam(r, "workspaceId")
|
||||||
workspaceID, err := strconv.Atoi(workspaceIDStr)
|
workspaceID, err := strconv.Atoi(workspaceIDStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return userID, 0, errors.New("invalid workspaceId")
|
return 0, errors.New("invalid workspaceId")
|
||||||
}
|
}
|
||||||
|
|
||||||
return userID, workspaceID, nil
|
return workspaceID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondJSON(w http.ResponseWriter, data interface{}) {
|
func respondJSON(w http.ResponseWriter, data interface{}) {
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem, authMiddleware *auth.Middleware, sessionService *auth.SessionService) {
|
func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem, authMiddleware *auth.Middleware, sessionService *auth.SessionService) {
|
||||||
|
|
||||||
|
handler := &BaseHandler{
|
||||||
|
DB: db,
|
||||||
|
FS: fs,
|
||||||
|
}
|
||||||
|
|
||||||
// Public routes (no authentication required)
|
// Public routes (no authentication required)
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Post("/auth/login", Login(sessionService, db))
|
r.Post("/auth/login", Login(sessionService, db))
|
||||||
@@ -19,59 +25,51 @@ func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem, authMiddlew
|
|||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
// Apply authentication middleware to all routes in this group
|
// Apply authentication middleware to all routes in this group
|
||||||
r.Use(authMiddleware.Authenticate)
|
r.Use(authMiddleware.Authenticate)
|
||||||
|
r.Use(WithHandlerContext(db))
|
||||||
|
|
||||||
// Auth routes
|
// Auth routes
|
||||||
r.Post("/auth/logout", Logout(sessionService))
|
r.Post("/auth/logout", Logout(sessionService))
|
||||||
r.Get("/auth/me", GetCurrentUser(db))
|
r.Get("/auth/me", handler.GetCurrentUser(db))
|
||||||
|
|
||||||
// Admin-only routes
|
// Admin-only routes
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(authMiddleware.RequireRole("admin"))
|
r.Use(authMiddleware.RequireRole("admin"))
|
||||||
|
|
||||||
// TODO: Implement
|
|
||||||
// r.Get("/admin/users", ListUsers(db))
|
// r.Get("/admin/users", ListUsers(db))
|
||||||
// r.Post("/admin/users", CreateUser(db))
|
// r.Post("/admin/users", CreateUser(db))
|
||||||
// r.Delete("/admin/users/{userId}", DeleteUser(db))
|
// r.Delete("/admin/users/{userId}", DeleteUser(db))
|
||||||
})
|
})
|
||||||
|
|
||||||
// User routes - protected by resource ownership
|
// Workspace routes
|
||||||
r.Route("/users/{userId}", func(r chi.Router) {
|
r.Route("/workspaces", func(r chi.Router) {
|
||||||
r.Use(authMiddleware.RequireResourceOwnership)
|
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("/", GetUser(db))
|
// Single workspace routes
|
||||||
|
r.Route("/{workspaceId}", func(r chi.Router) {
|
||||||
|
r.Use(authMiddleware.RequireWorkspaceOwnership(db))
|
||||||
|
|
||||||
// Workspace routes
|
r.Get("/", handler.GetWorkspace(db))
|
||||||
r.Route("/workspaces", func(r chi.Router) {
|
r.Put("/", handler.UpdateWorkspace(db, fs))
|
||||||
r.Get("/", ListWorkspaces(db))
|
r.Delete("/", handler.DeleteWorkspace(db))
|
||||||
r.Post("/", CreateWorkspace(db, fs))
|
|
||||||
r.Get("/last", GetLastWorkspace(db))
|
|
||||||
r.Put("/last", UpdateLastWorkspace(db))
|
|
||||||
|
|
||||||
r.Route("/{workspaceId}", func(r chi.Router) {
|
// File routes
|
||||||
// Add workspace ownership check
|
r.Route("/files", func(r chi.Router) {
|
||||||
r.Use(authMiddleware.RequireWorkspaceOwnership(db))
|
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("/", GetWorkspace(db))
|
r.Post("/*", handler.SaveFile(fs))
|
||||||
r.Put("/", UpdateWorkspace(db, fs))
|
r.Get("/*", handler.GetFileContent(fs))
|
||||||
r.Delete("/", DeleteWorkspace(db))
|
r.Delete("/*", handler.DeleteFile(fs))
|
||||||
|
})
|
||||||
|
|
||||||
// File routes
|
// Git routes
|
||||||
r.Route("/files", func(r chi.Router) {
|
r.Route("/git", func(r chi.Router) {
|
||||||
r.Get("/", ListFiles(fs))
|
r.Post("/commit", handler.StageCommitAndPush(fs))
|
||||||
r.Get("/last", GetLastOpenedFile(db))
|
r.Post("/pull", handler.PullChanges(fs))
|
||||||
r.Put("/last", UpdateLastOpenedFile(db, fs))
|
|
||||||
r.Get("/lookup", LookupFileByName(fs))
|
|
||||||
|
|
||||||
r.Post("/*", SaveFile(fs))
|
|
||||||
r.Get("/*", GetFileContent(fs))
|
|
||||||
r.Delete("/*", DeleteFile(fs))
|
|
||||||
})
|
|
||||||
|
|
||||||
// Git routes
|
|
||||||
r.Route("/git", func(r chi.Router) {
|
|
||||||
r.Post("/commit", StageCommitAndPush(fs))
|
|
||||||
r.Post("/pull", PullChanges(fs))
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,15 +6,14 @@ import (
|
|||||||
"novamd/internal/db"
|
"novamd/internal/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetUser(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) GetUser(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := getUserID(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := db.GetUserByID(userID)
|
user, err := db.GetUserByID(ctx.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get user", http.StatusInternalServerError)
|
http.Error(w, "Failed to get user", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,15 +9,14 @@ import (
|
|||||||
"novamd/internal/models"
|
"novamd/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListWorkspaces(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) ListWorkspaces(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := getUserID(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaces, err := db.GetWorkspacesByUserID(userID)
|
workspaces, err := db.GetWorkspacesByUserID(ctx.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to list workspaces", http.StatusInternalServerError)
|
http.Error(w, "Failed to list workspaces", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -27,11 +26,10 @@ func ListWorkspaces(db *db.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
func (h *BaseHandler) CreateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := getUserID(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +39,7 @@ func CreateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
workspace.UserID = userID
|
workspace.UserID = ctx.UserID
|
||||||
if err := db.CreateWorkspace(&workspace); err != nil {
|
if err := db.CreateWorkspace(&workspace); err != nil {
|
||||||
http.Error(w, "Failed to create workspace", http.StatusInternalServerError)
|
http.Error(w, "Failed to create workspace", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -56,34 +54,37 @@ func CreateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetWorkspace(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) GetWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
workspace, err := db.GetWorkspaceByID(workspaceID)
|
respondJSON(w, ctx.Workspace)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Workspace not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if workspace.UserID != userID {
|
|
||||||
http.Error(w, "Unauthorized access to workspace", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
respondJSON(w, workspace)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
func gitSettingsChanged(new, old *models.Workspace) bool {
|
||||||
|
// Check if Git was enabled/disabled
|
||||||
|
if new.GitEnabled != old.GitEnabled {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// If Git is enabled, check if any settings changed
|
||||||
|
if new.GitEnabled {
|
||||||
|
return new.GitURL != old.GitURL ||
|
||||||
|
new.GitUser != old.GitUser ||
|
||||||
|
new.GitToken != old.GitToken
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *BaseHandler) UpdateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,8 +95,8 @@ func UpdateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set IDs from the request
|
// Set IDs from the request
|
||||||
workspace.ID = workspaceID
|
workspace.ID = ctx.Workspace.ID
|
||||||
workspace.UserID = userID
|
workspace.UserID = ctx.UserID
|
||||||
|
|
||||||
// Validate the workspace
|
// Validate the workspace
|
||||||
if err := workspace.Validate(); err != nil {
|
if err := workspace.Validate(); err != nil {
|
||||||
@@ -103,31 +104,22 @@ func UpdateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current workspace for comparison
|
|
||||||
currentWorkspace, err := db.GetWorkspaceByID(workspaceID)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Workspace not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentWorkspace.UserID != userID {
|
|
||||||
http.Error(w, "Unauthorized access to workspace", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle Git repository setup/teardown if Git settings changed
|
// Handle Git repository setup/teardown if Git settings changed
|
||||||
if workspace.GitEnabled != currentWorkspace.GitEnabled ||
|
if gitSettingsChanged(&workspace, ctx.Workspace) {
|
||||||
(workspace.GitEnabled && (workspace.GitURL != currentWorkspace.GitURL ||
|
|
||||||
workspace.GitUser != currentWorkspace.GitUser ||
|
|
||||||
workspace.GitToken != currentWorkspace.GitToken)) {
|
|
||||||
if workspace.GitEnabled {
|
if workspace.GitEnabled {
|
||||||
err = fs.SetupGitRepo(userID, workspaceID, workspace.GitURL, workspace.GitUser, workspace.GitToken)
|
if err := fs.SetupGitRepo(
|
||||||
if err != nil {
|
ctx.UserID,
|
||||||
|
ctx.Workspace.ID,
|
||||||
|
workspace.GitURL,
|
||||||
|
workspace.GitUser,
|
||||||
|
workspace.GitToken,
|
||||||
|
); err != nil {
|
||||||
http.Error(w, "Failed to setup git repo: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to setup git repo: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fs.DisableGitRepo(userID, workspaceID)
|
fs.DisableGitRepo(ctx.UserID, ctx.Workspace.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,16 +132,15 @@ func UpdateWorkspace(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteWorkspace(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) DeleteWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is the user's last workspace
|
// Check if this is the user's last workspace
|
||||||
workspaces, err := db.GetWorkspacesByUserID(userID)
|
workspaces, err := db.GetWorkspacesByUserID(ctx.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get workspaces", http.StatusInternalServerError)
|
http.Error(w, "Failed to get workspaces", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -163,7 +154,7 @@ func DeleteWorkspace(db *db.DB) http.HandlerFunc {
|
|||||||
// Find another workspace to set as last
|
// Find another workspace to set as last
|
||||||
var nextWorkspaceID int
|
var nextWorkspaceID int
|
||||||
for _, ws := range workspaces {
|
for _, ws := range workspaces {
|
||||||
if ws.ID != workspaceID {
|
if ws.ID != ctx.Workspace.ID {
|
||||||
nextWorkspaceID = ws.ID
|
nextWorkspaceID = ws.ID
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -178,14 +169,14 @@ func DeleteWorkspace(db *db.DB) http.HandlerFunc {
|
|||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Update last workspace ID first
|
// Update last workspace ID first
|
||||||
err = db.UpdateLastWorkspaceTx(tx, userID, nextWorkspaceID)
|
err = db.UpdateLastWorkspaceTx(tx, ctx.UserID, nextWorkspaceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
|
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the workspace
|
// Delete the workspace
|
||||||
err = db.DeleteWorkspaceTx(tx, workspaceID)
|
err = db.DeleteWorkspaceTx(tx, ctx.Workspace.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to delete workspace", http.StatusInternalServerError)
|
http.Error(w, "Failed to delete workspace", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -202,15 +193,14 @@ func DeleteWorkspace(db *db.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLastWorkspace(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) GetLastWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := getUserID(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaceID, err := db.GetLastWorkspaceID(userID)
|
workspaceID, err := db.GetLastWorkspaceID(ctx.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get last workspace", http.StatusInternalServerError)
|
http.Error(w, "Failed to get last workspace", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -220,11 +210,10 @@ func GetLastWorkspace(db *db.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateLastWorkspace(db *db.DB) http.HandlerFunc {
|
func (h *BaseHandler) UpdateLastWorkspace(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := getUserID(r)
|
ctx, ok := h.getContext(w, r)
|
||||||
if err != nil {
|
if !ok {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +226,7 @@ func UpdateLastWorkspace(db *db.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.UpdateLastWorkspace(userID, requestBody.WorkspaceID); err != nil {
|
if err := db.UpdateLastWorkspace(ctx.UserID, requestBody.WorkspaceID); err != nil {
|
||||||
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
|
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"novamd/internal/db"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type contextKey string
|
type contextKey string
|
||||||
@@ -96,27 +92,24 @@ func (m *Middleware) RequireRole(role string) func(http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequireResourceOwnership ensures users can only access their own resources
|
func (m *Middleware) RequireWorkspaceAccess(next http.Handler) http.Handler {
|
||||||
func (m *Middleware) RequireResourceOwnership(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get requesting user from context (set by auth middleware)
|
// Get our handler context
|
||||||
claims, err := GetUserFromContext(r.Context())
|
ctx := context.GetHandlerContext(r)
|
||||||
if err != nil {
|
if ctx == nil {
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get requested user ID from URL
|
// If no workspace in context, allow the request (might be a non-workspace endpoint)
|
||||||
userIDStr := chi.URLParam(r, "userId")
|
if ctx.Workspace == nil {
|
||||||
requestedUserID, err := strconv.Atoi(userIDStr)
|
next.ServeHTTP(w, r)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid user ID", http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow if user is accessing their own resources or is an admin
|
// Check if user has access (either owner or admin)
|
||||||
if claims.UserID != requestedUserID && claims.Role != "admin" {
|
if ctx.Workspace.UserID != ctx.UserID && ctx.UserRole != "admin" {
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,52 +117,6 @@ func (m *Middleware) RequireResourceOwnership(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequireWorkspaceOwnership ensures users can only access workspaces they own
|
|
||||||
type WorkspaceGetter interface {
|
|
||||||
GetWorkspaceByID(id int) (*Workspace, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Workspace struct {
|
|
||||||
ID int
|
|
||||||
UserID int
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequireWorkspaceOwnership ensures users can only access workspaces they own
|
|
||||||
func (m *Middleware) RequireWorkspaceOwnership(db *db.DB) func(http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Get requesting user from context
|
|
||||||
claims, err := GetUserFromContext(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get workspace ID from URL
|
|
||||||
workspaceID, err := strconv.Atoi(chi.URLParam(r, "workspaceId"))
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid workspace ID", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get workspace from database
|
|
||||||
workspace, err := db.GetWorkspaceByID(workspaceID)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Workspace not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if user owns the workspace or is admin
|
|
||||||
if workspace.UserID != claims.UserID && claims.Role != "admin" {
|
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserFromContext retrieves user claims from the request context
|
// GetUserFromContext retrieves user claims from the request context
|
||||||
func GetUserFromContext(ctx context.Context) (*UserClaims, error) {
|
func GetUserFromContext(ctx context.Context) (*UserClaims, error) {
|
||||||
claims, ok := ctx.Value(UserContextKey).(UserClaims)
|
claims, ok := ctx.Value(UserContextKey).(UserClaims)
|
||||||
|
|||||||
@@ -72,6 +72,38 @@ func (db *DB) GetWorkspaceByID(id int) (*models.Workspace, error) {
|
|||||||
return workspace, nil
|
return workspace, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetWorkspaceByName(userID int, workspaceName string) (*models.Workspace, error) {
|
||||||
|
workspace := &models.Workspace{}
|
||||||
|
var encryptedToken string
|
||||||
|
|
||||||
|
err := db.QueryRow(`
|
||||||
|
SELECT
|
||||||
|
id, user_id, name, created_at,
|
||||||
|
theme, auto_save,
|
||||||
|
git_enabled, git_url, git_user, git_token,
|
||||||
|
git_auto_commit, git_commit_msg_template
|
||||||
|
FROM workspaces
|
||||||
|
WHERE user_id = ? AND name = ?`,
|
||||||
|
userID, workspaceName,
|
||||||
|
).Scan(
|
||||||
|
&workspace.ID, &workspace.UserID, &workspace.Name, &workspace.CreatedAt,
|
||||||
|
&workspace.Theme, &workspace.AutoSave,
|
||||||
|
&workspace.GitEnabled, &workspace.GitURL, &workspace.GitUser, &encryptedToken,
|
||||||
|
&workspace.GitAutoCommit, &workspace.GitCommitMsgTemplate,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt token
|
||||||
|
workspace.GitToken, err = db.decryptToken(encryptedToken)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to decrypt token: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return workspace, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db *DB) UpdateWorkspace(workspace *models.Workspace) error {
|
func (db *DB) UpdateWorkspace(workspace *models.Workspace) error {
|
||||||
// Encrypt token before storing
|
// Encrypt token before storing
|
||||||
encryptedToken, err := db.encryptToken(workspace.GitToken)
|
encryptedToken, err := db.encryptToken(workspace.GitToken)
|
||||||
|
|||||||
Reference in New Issue
Block a user