From 5cea9b3844bb7ed64395e5652e8a9908e1480373 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Fri, 27 Sep 2024 22:00:48 +0200 Subject: [PATCH] Set up static routing on be --- backend/cmd/server/main.go | 24 +++++++++++++++++++++--- backend/internal/api/routes.go | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 0941c44..ac02b46 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -4,6 +4,7 @@ import ( "log" "net/http" "os" + "path/filepath" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" @@ -14,7 +15,6 @@ import ( ) func main() { - // Initialize database dbPath := os.Getenv("NOVAMD_DB_PATH") if dbPath == "" { @@ -38,8 +38,26 @@ func main() { r.Use(middleware.Logger) r.Use(middleware.Recoverer) - // Set up routes - api.SetupRoutes(r, database, fs) + // Set up API routes + r.Route("/api/v1", func(r chi.Router) { + api.SetupRoutes(r, database, fs) + }) + + // Set up static file server + staticPath := os.Getenv("NOVAMD_STATIC_PATH") + if staticPath == "" { + staticPath = "../frontend/dist" + } + fileServer := http.FileServer(http.Dir(staticPath)) + r.Get("/*", func(w http.ResponseWriter, r *http.Request) { + filePath := filepath.Join(staticPath, r.URL.Path) + _, err := os.Stat(filePath) + if os.IsNotExist(err) { + http.ServeFile(w, r, filepath.Join(staticPath, "index.html")) + return + } + fileServer.ServeHTTP(w, r) + }) // Start server port := os.Getenv("NOVAMD_PORT") diff --git a/backend/internal/api/routes.go b/backend/internal/api/routes.go index 95c7855..a5bd680 100644 --- a/backend/internal/api/routes.go +++ b/backend/internal/api/routes.go @@ -8,7 +8,7 @@ import ( ) func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem) { - r.Route("/api/v1", func(r chi.Router) { + r.Route("/", func(r chi.Router) { r.Route("/settings", func(r chi.Router) { r.Get("/", GetSettings(db)) r.Post("/", UpdateSettings(db))