Set up static routing on be

This commit is contained in:
2024-09-27 22:00:48 +02:00
parent 0933132a42
commit 5cea9b3844
2 changed files with 22 additions and 4 deletions

View File

@@ -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")

View File

@@ -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))