Fixed so that go serves static REACT files as well
This commit is contained in:
parent
998ae4295b
commit
188f09e5fe
|
@ -2,3 +2,4 @@
|
||||||
data/*
|
data/*
|
||||||
images/*
|
images/*
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
build/*
|
||||||
|
|
10
config.go
10
config.go
|
@ -16,6 +16,8 @@ type Config struct {
|
||||||
ImageStorageDir string `yaml:"image_storage_dir"`
|
ImageStorageDir string `yaml:"image_storage_dir"`
|
||||||
ListeningPort int `yaml:"listening_port"`
|
ListeningPort int `yaml:"listening_port"`
|
||||||
LogFile string `yaml:"log_file"`
|
LogFile string `yaml:"log_file"`
|
||||||
|
StaticFilesDir string `yaml:"static_files_dir"`
|
||||||
|
AllowedOrigins string `yaml:"allowed_origins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(configPath string) (*Config, error) {
|
func LoadConfig(configPath string) (*Config, error) {
|
||||||
|
@ -37,5 +39,13 @@ func LoadConfig(configPath string) (*Config, error) {
|
||||||
config.DatabasePath = dbPath
|
config.DatabasePath = dbPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.StaticFilesDir == "" {
|
||||||
|
config.StaticFilesDir = "build"
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.AllowedOrigins == "" {
|
||||||
|
config.AllowedOrigins = "http://localhost:3000"
|
||||||
|
}
|
||||||
|
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,3 +4,5 @@ jwt_secret: "super_secret_key"
|
||||||
image_storage_dir: "images"
|
image_storage_dir: "images"
|
||||||
listening_port: 8080
|
listening_port: 8080
|
||||||
log_file: "boxes.log"
|
log_file: "boxes.log"
|
||||||
|
static_files_dir: "/app/build/"
|
||||||
|
allowed_origins: "*"
|
||||||
|
|
36
main.go
36
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
@ -23,9 +24,18 @@ func main() {
|
||||||
var err error
|
var err error
|
||||||
config, err = LoadConfig(configFile)
|
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
|
// Get the allowed origins from the ALLOWED_ORIGINS environment variable
|
||||||
// If empty, defaults to http://localhost:3000
|
// 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
|
origins := []string{"http://localhost:3000"} // Default value
|
||||||
|
|
||||||
if allowedOrigins != "" {
|
if allowedOrigins != "" {
|
||||||
|
@ -52,12 +62,25 @@ 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) {
|
||||||
|
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!")
|
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")
|
||||||
|
@ -89,6 +112,15 @@ func main() {
|
||||||
managementRouter.Handle("/db", http.HandlerFunc(BackupDatabaseHandler)).Methods("GET", "OPTIONS")
|
managementRouter.Handle("/db", http.HandlerFunc(BackupDatabaseHandler)).Methods("GET", "OPTIONS")
|
||||||
managementRouter.Handle("/db", http.HandlerFunc(RestoreDatabaseHandler)).Methods("POST", "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
|
// Apply CORS middleware
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
AllowedOrigins: origins,
|
AllowedOrigins: origins,
|
||||||
|
@ -100,5 +132,5 @@ func main() {
|
||||||
|
|
||||||
// Start the server with CORS middleware
|
// Start the server with CORS middleware
|
||||||
fmt.Printf("Server listening on port %d\n", config.ListeningPort)
|
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