From 40d11b12cab0cf2e3eaa722ab376d64232343a08 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 27 Jul 2025 19:05:15 +0200 Subject: [PATCH] Add cors middleware --- cmd/server/main.go | 2 +- go.mod | 1 + go.sum | 2 ++ pkg/config.go | 8 ++++++-- pkg/handlers.go | 4 +++- pkg/routes.go | 11 +++++++++++ 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 11d3837..dc58eaa 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -32,7 +32,7 @@ func main() { instanceManager := llamactl.NewInstanceManager(config.Instances) // Create a new handler with the instance manager - handler := llamactl.NewHandler(instanceManager) + handler := llamactl.NewHandler(instanceManager, config) // Setup the router with the handler r := llamactl.SetupRouter(handler) diff --git a/go.mod b/go.mod index 836367b..43f154a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.24.5 require ( github.com/go-chi/chi/v5 v5.2.2 + github.com/go-chi/cors v1.2.2 github.com/swaggo/http-swagger v1.3.4 github.com/swaggo/swag v1.16.5 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 05d17b6..8e3b3d0 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618= github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= +github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE= +github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/go-openapi/jsonpointer v0.21.1 h1:whnzv/pNXtK2FbX/W9yJfRmE2gsmkfahjMKB0fZvcic= github.com/go-openapi/jsonpointer v0.21.1/go.mod h1:50I1STOfbY1ycR8jGz8DaMeLCdXiI6aDteEdRNNzpdk= github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ= diff --git a/pkg/config.go b/pkg/config.go index 748492a..7e7256a 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -23,6 +23,9 @@ type ServerConfig struct { // Server port to bind to Port int `yaml:"port"` + + // Allowed origins for CORS (e.g., "http://localhost:3000") + AllowedOrigins []string `yaml:"allowed_origins"` } // InstancesConfig contains instance management configuration @@ -57,8 +60,9 @@ func LoadConfig(configPath string) (Config, error) { // 1. Start with defaults cfg := Config{ Server: ServerConfig{ - Host: "", - Port: 8080, + Host: "0.0.0.0", + Port: 8080, + AllowedOrigins: []string{"*"}, // Default to allow all origins }, Instances: InstancesConfig{ PortRange: [2]int{8000, 9000}, diff --git a/pkg/handlers.go b/pkg/handlers.go index d19aa1a..afaee99 100644 --- a/pkg/handlers.go +++ b/pkg/handlers.go @@ -15,11 +15,13 @@ import ( type Handler struct { InstanceManager InstanceManager + config Config } -func NewHandler(im InstanceManager) *Handler { +func NewHandler(im InstanceManager, config Config) *Handler { return &Handler{ InstanceManager: im, + config: config, } } diff --git a/pkg/routes.go b/pkg/routes.go index 7f86f1c..4685b85 100644 --- a/pkg/routes.go +++ b/pkg/routes.go @@ -5,6 +5,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/cors" httpSwagger "github.com/swaggo/http-swagger" _ "llamactl/docs" @@ -15,6 +16,16 @@ func SetupRouter(handler *Handler) *chi.Mux { r := chi.NewRouter() r.Use(middleware.Logger) + // Add CORS middleware + r.Use(cors.Handler(cors.Options{ + AllowedOrigins: handler.config.Server.AllowedOrigins, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: false, + MaxAge: 300, + })) + r.Get("/swagger/*", httpSwagger.Handler( httpSwagger.URL("/swagger/doc.json"), ))