Refactor allowedOrigins creation
This commit is contained in:
@@ -56,7 +56,7 @@ func main() {
|
|||||||
app := handlers.NewApp(db, handlers.NewBroker())
|
app := handlers.NewApp(db, handlers.NewBroker())
|
||||||
|
|
||||||
// Create server
|
// Create server
|
||||||
srv := NewServer(app, "0.0.0.0:7153", []string{"*"})
|
srv := NewServer(app, "0.0.0.0:7153", "*")
|
||||||
|
|
||||||
// Start background cleanup service
|
// Start background cleanup service
|
||||||
go handlers.StartDailyCleanup(app)
|
go handlers.StartDailyCleanup(app)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"menu/handlers"
|
"menu/handlers"
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ import (
|
|||||||
"github.com/go-chi/cors"
|
"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()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
@@ -18,7 +19,7 @@ func NewServer(app *handlers.App, address string, allowedOrigins []string) *http
|
|||||||
r.Use(middleware.Recoverer)
|
r.Use(middleware.Recoverer)
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Use(cors.Handler(cors.Options{
|
r.Use(cors.Handler(cors.Options{
|
||||||
AllowedOrigins: allowedOrigins,
|
AllowedOrigins: makeAllowedOrigins(allowedOrigins),
|
||||||
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
|
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
|
||||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
@@ -72,3 +73,10 @@ func NewServer(app *handlers.App, address string, allowedOrigins []string) *http
|
|||||||
Handler: r,
|
Handler: r,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeAllowedOrigins(origins string) []string {
|
||||||
|
if origins == "" {
|
||||||
|
origins = "*"
|
||||||
|
}
|
||||||
|
return strings.Split(strings.TrimSpace(origins), ",")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user