diff --git a/config/config.yaml b/config/config.yaml index 7bd80ea..8ff4cd0 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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: "*" diff --git a/docker-compose.yml b/docker-compose.yml index 9b15883..12b4bff 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/main.go b/main.go index b50772a..8abb29f 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,8 @@ import ( "log" "net/http" "os" - "strings" "path/filepath" + "strings" "github.com/gorilla/mux" "github.com/jinzhu/gorm" @@ -25,9 +25,9 @@ func main() { config, err = LoadConfig(configFile) // get the static files directory - staticPath := config.StaticFilesDir + staticPath := config.StaticFilesDir - // Add this before setting up the handler + // Add this before setting up the handler if _, err := os.Stat(staticPath); os.IsNotExist(err) { log.Fatalf("Static directory does not exist: %s", staticPath) } @@ -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") @@ -115,13 +117,32 @@ func main() { // Define a route for serving static files fmt.Println("Serving static files from:", staticPath) - //staticHandler := http.FileServer(http.Dir(staticPath)) + //staticHandler := http.FileServer(http.Dir(staticPath)) fmt.Println("Registering route for serving static files, no StripPrefix") - //staticRouter.HandleFunc("/", customHandler).Methods("GET", "OPTIONS") + //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{ AllowedOrigins: origins, diff --git a/scripts/getboxes.bash b/scripts/getboxes.bash index c466333..476a878 100644 --- a/scripts/getboxes.bash +++ b/scripts/getboxes.bash @@ -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"