Fixed static routing ... again ... I think
This commit is contained in:
parent
7c34878c19
commit
36ce2e9de8
|
@ -1,8 +1,8 @@
|
||||||
database_path: "data/boxes.db"
|
database_path: "/app/data/boxes.db"
|
||||||
test_database_path: "data/test_database.db"
|
test_database_path: "/app/data/test_database.db"
|
||||||
jwt_secret: "super_secret_key"
|
jwt_secret: "super_secret_key"
|
||||||
image_storage_dir: "/Users/stwhite/CODE/boxes/images"
|
image_storage_dir: "/app/images/"
|
||||||
listening_port: 8080
|
listening_port: 8080
|
||||||
log_file: "boxes.log"
|
log_file: "boxes.log"
|
||||||
static_files_dir: "/Users/stwhite/CODE/boxes/build"
|
static_files_dir: "/app/build/"
|
||||||
allowed_origins: "*"
|
allowed_origins: "*"
|
||||||
|
|
|
@ -9,10 +9,10 @@ services:
|
||||||
environment:
|
environment:
|
||||||
BOXES_API_CONFIG: "/app/config/config.yaml" # Set the CONFIG environment variable
|
BOXES_API_CONFIG: "/app/config/config.yaml" # Set the CONFIG environment variable
|
||||||
volumes:
|
volumes:
|
||||||
- /home/stwhite/dockerboxes/boxes-api/data:/app/data # Mount host data directory
|
- ./data:/app/data # Mount host data directory
|
||||||
- /home/stwhite/dockerboxes/boxes-api/images:/app/images # Mount host images directory
|
- ./images:/app/images # Mount host images directory
|
||||||
- /home/stwhite/dockerboxes/boxes-api/config:/app/config # Mount host config directory
|
- ./config:/app/config # Mount host config directory
|
||||||
- /home/stwhite/dockerboxes/boxes-api/build:/app/build
|
- ./build:/app/build
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
|
|
||||||
|
|
39
main.go
39
main.go
|
@ -5,8 +5,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
@ -62,25 +62,27 @@ func main() {
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Modify your custom handler to include more detailed logging
|
|
||||||
customHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
customHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("Attempting to serve: %s from directory: %s", r.URL.Path, staticPath)
|
// Don't handle /static/ paths here - let the static file server handle those
|
||||||
fullPath := filepath.Join(staticPath, r.URL.Path)
|
if strings.HasPrefix(r.URL.Path, "/static/") {
|
||||||
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
|
||||||
log.Printf("File not found: %s", fullPath)
|
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
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!")
|
fmt.Println("Default user 'boxuser' created successfully!")
|
||||||
|
|
||||||
// Create the router
|
// Create the router
|
||||||
baseRouter := mux.NewRouter()
|
baseRouter := mux.NewRouter()
|
||||||
|
|
||||||
router := baseRouter.PathPrefix("/api/v1").Subrouter()
|
router := baseRouter.PathPrefix("/api/v1").Subrouter()
|
||||||
staticRouter := baseRouter.PathPrefix("/").Subrouter()
|
|
||||||
|
|
||||||
// Define your routes
|
// Define your routes
|
||||||
router.Handle("/login", http.HandlerFunc(LoginHandler)).Methods("POST", "OPTIONS")
|
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")
|
fmt.Println("Registering route for serving static files, no StripPrefix")
|
||||||
//staticRouter.HandleFunc("/", customHandler).Methods("GET", "OPTIONS")
|
//staticRouter.HandleFunc("/", customHandler).Methods("GET", "OPTIONS")
|
||||||
// perplexity recommends:
|
// 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
|
// Apply CORS middleware
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# API base URL
|
# API base URL
|
||||||
API_BASE_URL="http://10.0.0.66:8080"
|
API_BASE_URL="http://localhost:8080/api/v1"
|
||||||
|
|
||||||
# Login credentials
|
# Login credentials
|
||||||
USERNAME="boxuser"
|
USERNAME="boxuser"
|
||||||
|
|
Loading…
Reference in New Issue