This commit is contained in:
2024-10-15 22:23:09 +02:00
parent 6cf141bfd9
commit 403ded509a
2 changed files with 24 additions and 27 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
@@ -36,17 +37,7 @@ func main() {
workdir = "./data" workdir = "./data"
} }
settings, err := database.GetSettings(1) // Assuming user ID 1 for now fs := filesystem.New(workdir)
if err != nil {
log.Print("Settings not found, using default settings")
}
fs := filesystem.New(workdir, &settings)
if settings.Settings.GitEnabled {
if err := fs.InitializeGitRepo(); err != nil {
log.Fatal(err)
}
}
// Set up router // Set up router
r := chi.NewRouter() r := chi.NewRouter()
@@ -64,21 +55,27 @@ func main() {
staticPath = "../frontend/dist" staticPath = "../frontend/dist"
} }
fileServer := http.FileServer(http.Dir(staticPath)) fileServer := http.FileServer(http.Dir(staticPath))
r.Get("/*", func(w http.ResponseWriter, r *http.Request) { r.Get(
requestedPath := r.URL.Path "/*",
validatedPath, err := filesystem.ValidatePath(staticPath, requestedPath) func(w http.ResponseWriter, r *http.Request) {
if err != nil { requestedPath := r.URL.Path
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
_, err = os.Stat(validatedPath) fullPath := filepath.Join(staticPath, requestedPath)
if os.IsNotExist(err) { cleanPath := filepath.Clean(fullPath)
http.ServeFile(w, r, filepath.Join(staticPath, "index.html"))
return if !strings.HasPrefix(cleanPath, staticPath) {
} http.Error(w, "Invalid path", http.StatusBadRequest)
http.StripPrefix("/", fileServer).ServeHTTP(w, r) return
}) }
_, err = os.Stat(cleanPath)
if os.IsNotExist(err) {
http.ServeFile(w, r, filepath.Join(staticPath, "index.html"))
return
}
http.StripPrefix("/", fileServer).ServeHTTP(w, r)
},
)
// Start server // Start server
port := os.Getenv("NOVAMD_PORT") port := os.Getenv("NOVAMD_PORT")
@@ -87,4 +84,4 @@ func main() {
} }
log.Printf("Server starting on port %s", port) log.Printf("Server starting on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, r)) log.Fatal(http.ListenAndServe(":"+port, r))
} }

View File

@@ -194,4 +194,4 @@ func (fs *FileSystem) getGitRepo(userID, workspaceID int) (*gitutils.GitRepo, bo
} }
repo, ok := userRepos[workspaceID] repo, ok := userRepos[workspaceID]
return repo, ok return repo, ok
} }