Refactor allowedOrigins creation

This commit is contained in:
2025-10-17 00:01:16 +02:00
parent 91c9aba1ab
commit 975b72e89f
2 changed files with 11 additions and 3 deletions

View File

@@ -56,7 +56,7 @@ func main() {
app := handlers.NewApp(db, handlers.NewBroker())
// Create server
srv := NewServer(app, "0.0.0.0:7153", []string{"*"})
srv := NewServer(app, "0.0.0.0:7153", "*")
// Start background cleanup service
go handlers.StartDailyCleanup(app)

View File

@@ -2,6 +2,7 @@ package main
import (
"net/http"
"strings"
"menu/handlers"
@@ -10,7 +11,7 @@ import (
"github.com/go-chi/cors"
)
func NewServer(app *handlers.App, address string, allowedOrigins []string) *http.Server {
func NewServer(app *handlers.App, address string, allowedOrigins string) *http.Server {
r := chi.NewRouter()
// Middleware
@@ -18,7 +19,7 @@ func NewServer(app *handlers.App, address string, allowedOrigins []string) *http
r.Use(middleware.Recoverer)
r.Use(middleware.Logger)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: allowedOrigins,
AllowedOrigins: makeAllowedOrigins(allowedOrigins),
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: true,
@@ -72,3 +73,10 @@ func NewServer(app *handlers.App, address string, allowedOrigins []string) *http
Handler: r,
}
}
func makeAllowedOrigins(origins string) []string {
if origins == "" {
origins = "*"
}
return strings.Split(strings.TrimSpace(origins), ",")
}