Rework api

This commit is contained in:
2024-10-15 22:17:34 +02:00
parent 071619cfb3
commit 6cf141bfd9
7 changed files with 443 additions and 110 deletions

View File

@@ -2,15 +2,13 @@ package api
import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"strings"
"novamd/internal/db"
"novamd/internal/filesystem"
"novamd/internal/models"
"github.com/go-chi/chi/v5"
)
func ListFiles(fs *filesystem.FileSystem) http.HandlerFunc {
@@ -63,7 +61,7 @@ func GetFileContent(fs *filesystem.FileSystem) http.HandlerFunc {
return
}
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
filePath := chi.URLParam(r, "*")
content, err := fs.GetFileContent(userID, workspaceID, filePath)
if err != nil {
http.Error(w, "Failed to read file", http.StatusNotFound)
@@ -83,7 +81,7 @@ func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
return
}
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
filePath := chi.URLParam(r, "*")
content, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest)
@@ -108,7 +106,7 @@ func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
return
}
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
filePath := chi.URLParam(r, "*")
err = fs.DeleteFile(userID, workspaceID, filePath)
if err != nil {
http.Error(w, "Failed to delete file", http.StatusInternalServerError)
@@ -120,60 +118,25 @@ func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
}
}
func GetSettings(db *db.DB) http.HandlerFunc {
func GetLastOpenedFile(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, workspaceID, err := getUserAndWorkspaceIDs(r)
userID, _, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
settings, err := db.GetWorkspaceSettings(workspaceID)
user, err := db.GetUserByID(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, "Failed to get user", http.StatusInternalServerError)
return
}
respondJSON(w, settings)
respondJSON(w, map[string]string{"lastOpenedFile": user.LastOpenedFilePath})
}
}
func UpdateSettings(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var settings models.WorkspaceSettings
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
settings.WorkspaceID = workspaceID
if err := db.SaveWorkspaceSettings(&settings); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if settings.Settings.GitEnabled {
err := fs.SetupGitRepo(userID, workspaceID, settings.Settings.GitURL, settings.Settings.GitUser, settings.Settings.GitToken)
if err != nil {
http.Error(w, "Failed to setup git repo", http.StatusInternalServerError)
return
}
} else {
fs.DisableGitRepo(userID, workspaceID)
}
respondJSON(w, settings)
}
}
func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
func UpdateLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
@@ -182,7 +145,7 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
}
var requestBody struct {
Message string `json:"message"`
FilePath string `json:"filePath"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
@@ -190,54 +153,18 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
return
}
if requestBody.Message == "" {
http.Error(w, "Commit message is required", http.StatusBadRequest)
return
}
err = fs.StageCommitAndPush(userID, workspaceID, requestBody.Message)
// Validate that the file path is valid within the workspace
_, err = fs.ValidatePath(userID, workspaceID, requestBody.FilePath)
if err != nil {
http.Error(w, "Failed to stage, commit, and push changes: "+err.Error(), http.StatusInternalServerError)
http.Error(w, "Invalid file path", http.StatusBadRequest)
return
}
respondJSON(w, map[string]string{"message": "Changes staged, committed, and pushed successfully"})
}
}
func PullChanges(fs *filesystem.FileSystem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
if err := db.UpdateLastOpenedFile(userID, requestBody.FilePath); err != nil {
http.Error(w, "Failed to update last opened file", http.StatusInternalServerError)
return
}
err = fs.Pull(userID, workspaceID)
if err != nil {
http.Error(w, "Failed to pull changes: "+err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, map[string]string{"message": "Pulled changes from remote"})
respondJSON(w, map[string]string{"message": "Last opened file updated successfully"})
}
}
func getUserAndWorkspaceIDs(r *http.Request) (int, int, error) {
userID, err := strconv.Atoi(r.URL.Query().Get("userId"))
if err != nil {
return 0, 0, errors.New("invalid userId")
}
workspaceID, err := strconv.Atoi(r.URL.Query().Get("workspaceId"))
if err != nil {
return 0, 0, errors.New("invalid workspaceId")
}
return userID, workspaceID, nil
}
func respondJSON(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}

View File

@@ -0,0 +1,58 @@
package api
import (
"encoding/json"
"net/http"
"novamd/internal/filesystem"
)
func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var requestBody struct {
Message string `json:"message"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if requestBody.Message == "" {
http.Error(w, "Commit message is required", http.StatusBadRequest)
return
}
err = fs.StageCommitAndPush(userID, workspaceID, requestBody.Message)
if err != nil {
http.Error(w, "Failed to stage, commit, and push changes: "+err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, map[string]string{"message": "Changes staged, committed, and pushed successfully"})
}
}
func PullChanges(fs *filesystem.FileSystem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = fs.Pull(userID, workspaceID)
if err != nil {
http.Error(w, "Failed to pull changes: "+err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, map[string]string{"message": "Pulled changes from remote"})
}
}

View File

@@ -0,0 +1,37 @@
package api
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
func getUserID(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")
workspaceID, err := strconv.Atoi(workspaceIDStr)
if err != nil {
return userID, 0, errors.New("invalid workspaceId")
}
return userID, workspaceID, nil
}
func respondJSON(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
}

View File

@@ -8,21 +8,50 @@ import (
)
func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem) {
r.Route("/", func(r chi.Router) {
r.Route("/settings", func(r chi.Router) {
r.Get("/", GetSettings(db))
r.Post("/", UpdateSettings(db, fs))
})
r.Route("/files", func(r chi.Router) {
r.Get("/", ListFiles(fs))
r.Get("/*", GetFileContent(fs))
r.Post("/*", SaveFile(fs))
r.Delete("/*", DeleteFile(fs))
r.Get("/lookup", LookupFileByName(fs))
})
r.Route("/git", func(r chi.Router) {
r.Post("/commit", StageCommitAndPush(fs))
r.Post("/pull", PullChanges(fs))
r.Route("/api/v1", func(r chi.Router) {
// User routes
r.Route("/users/{userId}", func(r chi.Router) {
r.Get("/", GetUser(db))
// Workspace routes
r.Route("/workspaces", func(r chi.Router) {
r.Get("/", ListWorkspaces(db))
r.Post("/", CreateWorkspace(db))
r.Get("/last", GetLastWorkspace(db))
r.Put("/last", UpdateLastWorkspace(db))
r.Route("/{workspaceId}", func(r chi.Router) {
r.Get("/", GetWorkspace(db))
r.Put("/", UpdateWorkspace(db))
r.Delete("/", DeleteWorkspace(db))
// File routes
r.Route("/files", func(r chi.Router) {
r.Get("/", ListFiles(fs))
r.Get("/last", GetLastOpenedFile(db))
r.Put("/last", UpdateLastOpenedFile(db, fs))
r.Get("/lookup", LookupFileByName(fs)) // Moved here
r.Route("/*", func(r chi.Router) {
r.Post("/", SaveFile(fs))
r.Get("/", GetFileContent(fs))
r.Delete("/", DeleteFile(fs))
})
})
// Settings routes
r.Route("/settings", func(r chi.Router) {
r.Get("/", GetWorkspaceSettings(db))
r.Put("/", UpdateWorkspaceSettings(db, fs))
})
// Git routes
r.Route("/git", func(r chi.Router) {
r.Post("/commit", StageCommitAndPush(fs))
r.Post("/pull", PullChanges(fs))
})
})
})
})
})
}

View File

@@ -0,0 +1,25 @@
package api
import (
"net/http"
"novamd/internal/db"
)
func GetUser(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := getUserID(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
user, err := db.GetUserByID(userID)
if err != nil {
http.Error(w, "Failed to get user", http.StatusInternalServerError)
return
}
respondJSON(w, user)
}
}

View File

@@ -0,0 +1,246 @@
package api
import (
"encoding/json"
"net/http"
"novamd/internal/db"
"novamd/internal/filesystem"
"novamd/internal/models"
)
func ListWorkspaces(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := getUserID(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
workspaces, err := db.GetWorkspacesByUserID(userID)
if err != nil {
http.Error(w, "Failed to list workspaces", http.StatusInternalServerError)
return
}
respondJSON(w, workspaces)
}
}
func CreateWorkspace(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := getUserID(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var requestBody struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
workspace := &models.Workspace{
UserID: userID,
Name: requestBody.Name,
}
if err := db.CreateWorkspace(workspace); err != nil {
http.Error(w, "Failed to create workspace", http.StatusInternalServerError)
return
}
respondJSON(w, workspace)
}
}
func GetWorkspace(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
workspace, err := db.GetWorkspaceByID(workspaceID)
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, map[string]string{"name": workspace.Name})
}
}
func UpdateWorkspace(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var requestBody struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
workspace, err := db.GetWorkspaceByID(workspaceID)
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
}
workspace.Name = requestBody.Name
if err := db.UpdateWorkspace(workspace); err != nil {
http.Error(w, "Failed to update workspace", http.StatusInternalServerError)
return
}
respondJSON(w, map[string]string{"name": workspace.Name})
}
}
func DeleteWorkspace(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
workspace, err := db.GetWorkspaceByID(workspaceID)
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
}
if err := db.DeleteWorkspace(workspaceID); err != nil {
http.Error(w, "Failed to delete workspace", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Workspace deleted successfully"))
}
}
func GetLastWorkspace(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := getUserID(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
workspaceID, err := db.GetLastWorkspaceID(userID)
if err != nil {
http.Error(w, "Failed to get last workspace", http.StatusInternalServerError)
return
}
respondJSON(w, map[string]int{"lastWorkspaceId": workspaceID})
}
}
func UpdateLastWorkspace(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := getUserID(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var requestBody struct {
WorkspaceID int `json:"workspaceId"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if err := db.UpdateLastWorkspace(userID, requestBody.WorkspaceID); err != nil {
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
return
}
respondJSON(w, map[string]string{"message": "Last workspace updated successfully"})
}
}
func GetWorkspaceSettings(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
settings, err := db.GetWorkspaceSettings(workspaceID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, settings)
}
}
func UpdateWorkspaceSettings(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, workspaceID, err := getUserAndWorkspaceIDs(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var settings models.WorkspaceSettings
if err := json.NewDecoder(r.Body).Decode(&settings); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
settings.WorkspaceID = workspaceID
if err := db.SaveWorkspaceSettings(&settings); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if settings.Settings.GitEnabled {
err := fs.SetupGitRepo(userID, workspaceID, settings.Settings.GitURL, settings.Settings.GitUser, settings.Settings.GitToken)
if err != nil {
http.Error(w, "Failed to setup git repo", http.StatusInternalServerError)
return
}
} else {
fs.DisableGitRepo(userID, workspaceID)
}
respondJSON(w, settings)
}
}

View File

@@ -46,3 +46,14 @@ func (db *DB) GetWorkspacesByUserID(userID int) ([]*models.Workspace, error) {
}
return workspaces, nil
}
func (db *DB) UpdateWorkspace(workspace *models.Workspace) error {
_, err := db.Exec("UPDATE workspaces SET name = ? WHERE id = ? AND user_id = ?",
workspace.Name, workspace.ID, workspace.UserID)
return err
}
func (db *DB) DeleteWorkspace(id int) error {
_, err := db.Exec("DELETE FROM workspaces WHERE id = ?", id)
return err
}