Serve precompressed files

This commit is contained in:
2024-10-28 21:45:11 +01:00
parent d417b9f0c7
commit d82f7e2b1e
2 changed files with 78 additions and 32 deletions

View File

@@ -4,8 +4,6 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
@@ -32,20 +30,16 @@ func main() {
}
}()
// Workdir
// Initialize filesystem
workdir := os.Getenv("NOVAMD_WORKDIR")
if workdir == "" {
workdir = "./data"
}
fs := filesystem.New(workdir)
// User service
// Initialize user service
userService := user.NewUserService(database, fs)
// Admin user
_, err = userService.SetupAdminUser()
if err != nil {
if _, err := userService.SetupAdminUser(); err != nil {
log.Fatal(err)
}
@@ -54,44 +48,26 @@ func main() {
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// Set up API routes
// API routes
r.Route("/api/v1", func(r chi.Router) {
api.SetupRoutes(r, database, fs)
})
// Set up static file server with path validation
// Static file serving
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) {
requestedPath := r.URL.Path
fullPath := filepath.Join(staticPath, requestedPath)
cleanPath := filepath.Clean(fullPath)
if !strings.HasPrefix(cleanPath, staticPath) {
http.Error(w, "Invalid path", http.StatusBadRequest)
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)
},
)
// Handle all other routes with static file server
r.Get("/*", api.NewStaticHandler(staticPath).ServeHTTP)
// Start server
port := os.Getenv("NOVAMD_PORT")
if port == "" {
port = "8080"
}
log.Printf("Server starting on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, r))
}