Skip to content

Commit 5e85575

Browse files
committed
added flag and env var to specify UI cors enabled domains
1 parent a6f4f48 commit 5e85575

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ JWT_SECRET=
2323
# CLEANUP_INTERVAL=24h
2424
# RATE_LIMIT=100
2525
# API_TIMEOUT=30s
26+
# ALLOWED_UI_DOMAINS=https://example.com,https://app.example.com
2627

2728
# Frontend
2829
API_URL=http://127.0.0.1:8080

data-server/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ JWT_SECRET=your-secret-jwt-key-change-in-production
4949
# API Configuration (optional)
5050
# RATE_LIMIT=100
5151
# API_TIMEOUT=30s
52+
# ALLOWED_UI_DOMAINS=https://example.com,https://app.example.com
5253

5354
# Logging (optional)
5455
# LOG_LEVEL=info

data-server/internal/api/middleware/cors.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import (
77
"github.com/gin-gonic/gin"
88
)
99

10-
// CORS returns a configured CORS middleware
11-
func CORS() gin.HandlerFunc {
10+
// CORS returns a configured CORS middleware.
11+
// allowedOrigins should contain the list of permitted origins; pass ["*"] to allow all.
12+
func CORS(allowedOrigins []string) gin.HandlerFunc {
13+
// AllowCredentials cannot be true when origin is wildcard
14+
allowCredentials := !(len(allowedOrigins) == 1 && allowedOrigins[0] == "*")
15+
1216
config := cors.Config{
13-
AllowOrigins: []string{"*"}, // In production, specify actual origins
17+
AllowOrigins: allowedOrigins,
1418
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
1519
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
1620
ExposeHeaders: []string{"Content-Length"},
17-
AllowCredentials: true,
21+
AllowCredentials: allowCredentials,
1822
MaxAge: 12 * time.Hour,
1923
}
2024

data-server/internal/api/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func SetupRouter(cfg *config.Config, database db.Database, jwtManager *auth.JWTM
3030

3131
// Global middleware
3232
router.Use(gin.Recovery())
33-
router.Use(middleware.CORS())
33+
router.Use(middleware.CORS(cfg.API.AllowedOrigins))
3434

3535
// Rate limiter
3636
rateLimiter := middleware.NewRateLimiter(cfg.API.RateLimit)

data-server/internal/config/config.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"strconv"
8+
"strings"
89
"time"
910
)
1011

@@ -73,8 +74,9 @@ type RetentionConfig struct {
7374

7475
// APIConfig holds API-related configuration
7576
type APIConfig struct {
76-
RateLimit int
77-
Timeout time.Duration
77+
RateLimit int
78+
Timeout time.Duration
79+
AllowedOrigins []string
7880
}
7981

8082
// LoggingConfig holds logging configuration
@@ -132,6 +134,8 @@ func Load() (*Config, error) {
132134
// API
133135
flag.IntVar(&cfg.API.RateLimit, "rate-limit", getEnvInt("RATE_LIMIT", 100), "Requests per minute per API key")
134136
flag.DurationVar(&cfg.API.Timeout, "timeout", getEnvDuration("API_TIMEOUT", 30*time.Second), "API request timeout")
137+
var allowedUIDomains string
138+
flag.StringVar(&allowedUIDomains, "allowed-ui-domains", getEnv("ALLOWED_UI_DOMAINS", ""), "Comma-separated list of allowed UI origins for CORS (empty = allow all)")
135139

136140
// Logging
137141
flag.StringVar(&cfg.Logging.Level, "log-level", getEnv("LOG_LEVEL", "info"), "Log level: debug, info, warn, error")
@@ -141,6 +145,17 @@ func Load() (*Config, error) {
141145

142146
flag.Parse()
143147

148+
// Parse allowed UI domains for CORS
149+
if allowedUIDomains != "" {
150+
parts := strings.Split(allowedUIDomains, ",")
151+
for i, p := range parts {
152+
parts[i] = strings.TrimSpace(p)
153+
}
154+
cfg.API.AllowedOrigins = parts
155+
} else {
156+
cfg.API.AllowedOrigins = []string{"*"}
157+
}
158+
144159
// Validate required fields
145160
if err := cfg.Validate(); err != nil {
146161
return nil, err

0 commit comments

Comments
 (0)