trying to fix routes
This commit is contained in:
parent
c604251b7a
commit
4d9d722940
18
auth.go
18
auth.go
|
@ -108,7 +108,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(LoginResponse{Token: tokenString})
|
json.NewEncoder(w).Encode(LoginResponse{Token: tokenString})
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthMiddleware is a middleware function that checks for a valid JWT token in the request header and enables CORS.
|
// authMiddleware is a middleware function that checks for a valid JWT token in the request header and enables CORS.
|
||||||
func AuthMiddleware(next http.Handler) http.Handler {
|
func AuthMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log := GetLogger()
|
log := GetLogger()
|
||||||
|
@ -124,11 +124,21 @@ func AuthMiddleware(next http.Handler) http.Handler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the token from the request header
|
// Check if this is a browser requesting HTML
|
||||||
|
acceptHeader := r.Header.Get("Accept")
|
||||||
|
isBrowserRequest := strings.Contains(acceptHeader, "text/html")
|
||||||
|
|
||||||
|
// If it's a browser request for HTML, always serve the React app
|
||||||
|
if isBrowserRequest {
|
||||||
|
http.Redirect(w, r, "/index.html", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// From here on, we're dealing with actual API requests
|
||||||
tokenString := r.Header.Get("Authorization")
|
tokenString := r.Header.Get("Authorization")
|
||||||
if tokenString == "" {
|
if tokenString == "" {
|
||||||
if log != nil {
|
if log != nil {
|
||||||
log.Warn("Request rejected: missing Authorization header")
|
log.Warn("Request rejected: missing Authorization header for path: %s", r.URL.Path)
|
||||||
}
|
}
|
||||||
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
|
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
@ -178,4 +188,4 @@ func AuthMiddleware(next http.Handler) http.Handler {
|
||||||
// Call the next handler in the chain
|
// Call the next handler in the chain
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
10
main.go
10
main.go
|
@ -22,19 +22,21 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log := GetLogger()
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
var err error
|
var err error
|
||||||
config, err = loadAndValidateConfig()
|
config, err = loadAndValidateConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to load config: %v", err)
|
log.Fatalf("Failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("Config loaded successfully in main(), DB path %s\n", config.DatabasePath)
|
|
||||||
|
|
||||||
// Set up logging BEFORE logging config details
|
// Set up logging BEFORE logging config details
|
||||||
if err := setupLogging(config.LogFile); err != nil {
|
if err := setupLogging(config.LogFile); err != nil {
|
||||||
log.Fatalf("Failed to set up logging: %v", err)
|
log.Fatalf("Failed to set up logging: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log := GetLogger()
|
||||||
|
log.Printf("Config loaded successfully in main(), DB path %s\n", config.DatabasePath)
|
||||||
|
|
||||||
// Now that logging is set up, log the config details
|
// Now that logging is set up, log the config details
|
||||||
logConfigDetails(config)
|
logConfigDetails(config)
|
||||||
|
@ -65,6 +67,10 @@ func main() {
|
||||||
|
|
||||||
func loadAndValidateConfig() (*Config, error) {
|
func loadAndValidateConfig() (*Config, error) {
|
||||||
configFile := os.Getenv("BOXES_API_CONFIG")
|
configFile := os.Getenv("BOXES_API_CONFIG")
|
||||||
|
if configFile == "" {
|
||||||
|
fmt.Println("BOXES_API_CONFIG not set") // print because logger isn't alive yet.
|
||||||
|
configFile = "./config/config.yaml"
|
||||||
|
}
|
||||||
config, err := LoadConfig(configFile)
|
config, err := LoadConfig(configFile)
|
||||||
if err != nil || config == nil {
|
if err != nil || config == nil {
|
||||||
return nil, fmt.Errorf("failed to load config: %v", err)
|
return nil, fmt.Errorf("failed to load config: %v", err)
|
||||||
|
|
Loading…
Reference in New Issue