Fixed so that go serves static REACT files as well
This commit is contained in:
parent
998ae4295b
commit
188f09e5fe
|
@ -2,3 +2,4 @@
|
|||
data/*
|
||||
images/*
|
||||
.DS_Store
|
||||
build/*
|
||||
|
|
10
config.go
10
config.go
|
@ -16,6 +16,8 @@ type Config struct {
|
|||
ImageStorageDir string `yaml:"image_storage_dir"`
|
||||
ListeningPort int `yaml:"listening_port"`
|
||||
LogFile string `yaml:"log_file"`
|
||||
StaticFilesDir string `yaml:"static_files_dir"`
|
||||
AllowedOrigins string `yaml:"allowed_origins"`
|
||||
}
|
||||
|
||||
func LoadConfig(configPath string) (*Config, error) {
|
||||
|
@ -37,5 +39,13 @@ func LoadConfig(configPath string) (*Config, error) {
|
|||
config.DatabasePath = dbPath
|
||||
}
|
||||
|
||||
if config.StaticFilesDir == "" {
|
||||
config.StaticFilesDir = "build"
|
||||
}
|
||||
|
||||
if config.AllowedOrigins == "" {
|
||||
config.AllowedOrigins = "http://localhost:3000"
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
|
|
@ -4,3 +4,5 @@ jwt_secret: "super_secret_key"
|
|||
image_storage_dir: "images"
|
||||
listening_port: 8080
|
||||
log_file: "boxes.log"
|
||||
static_files_dir: "/app/build/"
|
||||
allowed_origins: "*"
|
||||
|
|
36
main.go
36
main.go
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
@ -23,9 +24,18 @@ func main() {
|
|||
var err error
|
||||
config, err = LoadConfig(configFile)
|
||||
|
||||
// get the static files directory
|
||||
staticPath := config.StaticFilesDir
|
||||
|
||||
// Add this before setting up the handler
|
||||
if _, err := os.Stat(staticPath); os.IsNotExist(err) {
|
||||
log.Fatalf("Static directory does not exist: %s", staticPath)
|
||||
}
|
||||
|
||||
// Get the allowed origins from the ALLOWED_ORIGINS environment variable
|
||||
// If empty, defaults to http://localhost:3000
|
||||
allowedOrigins := os.Getenv("BOXES_API_ALLOWED_ORIGINS")
|
||||
allowedOrigins := config.AllowedOrigins
|
||||
fmt.Println("Allowed origins: ", allowedOrigins)
|
||||
origins := []string{"http://localhost:3000"} // Default value
|
||||
|
||||
if allowedOrigins != "" {
|
||||
|
@ -52,12 +62,25 @@ 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)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
http.FileServer(http.Dir(staticPath)).ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
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")
|
||||
|
@ -89,6 +112,15 @@ func main() {
|
|||
managementRouter.Handle("/db", http.HandlerFunc(BackupDatabaseHandler)).Methods("GET", "OPTIONS")
|
||||
managementRouter.Handle("/db", http.HandlerFunc(RestoreDatabaseHandler)).Methods("POST", "OPTIONS")
|
||||
|
||||
// Define a route for serving static files
|
||||
fmt.Println("Serving static files from:", staticPath)
|
||||
//staticHandler := http.FileServer(http.Dir(staticPath))
|
||||
fmt.Println("Registering route for serving static files, no StripPrefix")
|
||||
//staticRouter.HandleFunc("/", customHandler).Methods("GET", "OPTIONS")
|
||||
// perplexity recommends:
|
||||
staticRouter.PathPrefix("/").Handler(http.StripPrefix("/", customHandler))
|
||||
|
||||
|
||||
// Apply CORS middleware
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: origins,
|
||||
|
@ -100,5 +132,5 @@ func main() {
|
|||
|
||||
// Start the server with CORS middleware
|
||||
fmt.Printf("Server listening on port %d\n", config.ListeningPort)
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", config.ListeningPort), c.Handler(router))
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", config.ListeningPort), c.Handler(baseRouter))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue