Fixed static routing ... again ... I think

This commit is contained in:
Steve White 2024-10-23 21:47:58 -05:00
parent 7c34878c19
commit 36ce2e9de8
4 changed files with 44 additions and 23 deletions

View File

@ -1,8 +1,8 @@
database_path: "data/boxes.db"
test_database_path: "data/test_database.db"
database_path: "/app/data/boxes.db"
test_database_path: "/app/data/test_database.db"
jwt_secret: "super_secret_key"
image_storage_dir: "/Users/stwhite/CODE/boxes/images"
image_storage_dir: "/app/images/"
listening_port: 8080
log_file: "boxes.log"
static_files_dir: "/Users/stwhite/CODE/boxes/build"
static_files_dir: "/app/build/"
allowed_origins: "*"

View File

@ -9,10 +9,10 @@ services:
environment:
BOXES_API_CONFIG: "/app/config/config.yaml" # Set the CONFIG environment variable
volumes:
- /home/stwhite/dockerboxes/boxes-api/data:/app/data # Mount host data directory
- /home/stwhite/dockerboxes/boxes-api/images:/app/images # Mount host images directory
- /home/stwhite/dockerboxes/boxes-api/config:/app/config # Mount host config directory
- /home/stwhite/dockerboxes/boxes-api/build:/app/build
- ./data:/app/data # Mount host data directory
- ./images:/app/images # Mount host images directory
- ./config:/app/config # Mount host config directory
- ./build:/app/build
networks:
- app-network

39
main.go
View File

@ -5,8 +5,8 @@ import (
"log"
"net/http"
"os"
"strings"
"path/filepath"
"strings"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
@ -62,25 +62,27 @@ func main() {
}
defer db.Close()
// Modify your custom handler to include more detailed logging
customHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Attempting to serve: %s from directory: %s", r.URL.Path, staticPath)
fullPath := filepath.Join(staticPath, r.URL.Path)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
log.Printf("File not found: %s", fullPath)
// Don't handle /static/ paths here - let the static file server handle those
if strings.HasPrefix(r.URL.Path, "/static/") {
http.NotFound(w, r)
return
}
http.FileServer(http.Dir(staticPath)).ServeHTTP(w, r)
// For all other routes, serve index.html
indexPath := filepath.Join(staticPath, "index.html")
http.ServeFile(w, r, indexPath)
})
// Register the catch-all handler for non-static routes
staticRouter.PathPrefix("/").Handler(customHandler)
fmt.Println("Default user 'boxuser' created successfully!")
// Create the router
baseRouter := mux.NewRouter()
router := baseRouter.PathPrefix("/api/v1").Subrouter()
staticRouter := baseRouter.PathPrefix("/").Subrouter()
// Define your routes
router.Handle("/login", http.HandlerFunc(LoginHandler)).Methods("POST", "OPTIONS")
@ -119,8 +121,27 @@ func main() {
fmt.Println("Registering route for serving static files, no StripPrefix")
//staticRouter.HandleFunc("/", customHandler).Methods("GET", "OPTIONS")
// perplexity recommends:
staticRouter.PathPrefix("/").Handler(http.StripPrefix("/", customHandler))
//staticRouter.PathPrefix("/").Handler(http.StripPrefix("/", customHandler))
// Replace your existing static route with this
// Create a dedicated file server for static files
fileServer := http.FileServer(http.Dir(staticPath))
// Register the static file handler with explicit path stripping
//staticRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", staticFS))
baseRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fileServer))
baseRouter.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Don't handle /static/ paths here
if strings.HasPrefix(r.URL.Path, "/static/") {
http.NotFound(w, r)
return
}
// For all other routes, serve index.html
indexPath := filepath.Join(staticPath, "index.html")
http.ServeFile(w, r, indexPath)
}))
// Apply CORS middleware
c := cors.New(cors.Options{

View File

@ -1,7 +1,7 @@
#!/bin/bash
# API base URL
API_BASE_URL="http://10.0.0.66:8080"
API_BASE_URL="http://localhost:8080/api/v1"
# Login credentials
USERNAME="boxuser"