-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathconfig.go
More file actions
102 lines (89 loc) · 2.65 KB
/
Copy pathconfig.go
File metadata and controls
102 lines (89 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"log"
"net/url"
"os"
"strings"
"time"
)
var defaultAllowedOrigins = []string{"http://localhost:3001"}
const (
receiptStoreModeRedis = "redis"
receiptStoreModeMemory = "memory"
)
func getAllowedOrigins() []string {
raw := strings.TrimSpace(os.Getenv("ALLOWED_ORIGINS"))
if raw == "" {
return defaultAllowedOrigins
}
origins := make([]string, 0)
for _, entry := range strings.Split(raw, ",") {
origin := strings.TrimSpace(entry)
if origin == "" {
continue
}
if isValidAllowedOrigin(origin) {
origins = append(origins, origin)
} else {
log.Printf("Warning: ignoring invalid ALLOWED_ORIGINS entry: %q", origin)
}
}
if len(origins) == 0 {
return defaultAllowedOrigins
}
return origins
}
func isValidAllowedOrigin(origin string) bool {
parsed, err := url.Parse(origin)
if err != nil {
return false
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return false
}
if parsed.Host == "" || parsed.User != nil {
return false
}
return parsed.Path == "" && parsed.RawQuery == "" && parsed.Fragment == ""
}
func getReceiptStoreMode() string {
mode := strings.ToLower(strings.TrimSpace(os.Getenv("RECEIPT_STORE")))
if mode == "" {
return receiptStoreModeRedis
}
return mode
}
func validateReceiptStoreMode() error {
switch getReceiptStoreMode() {
case receiptStoreModeRedis, receiptStoreModeMemory:
return nil
default:
return fmt.Errorf("RECEIPT_STORE must be %q or %q", receiptStoreModeRedis, receiptStoreModeMemory)
}
}
func isRedisRequired() bool {
return getCacheEnabled() || getReceiptStoreMode() == receiptStoreModeRedis
}
// getPositiveTimeout returns the configured timeout in seconds, but ensures a
// sensible default if the provided value is non-positive.
func getPositiveTimeout(envKey string, defaultSeconds int) time.Duration {
seconds := getEnvAsInt(envKey, defaultSeconds)
if seconds <= 0 {
seconds = defaultSeconds
}
return time.Duration(seconds) * time.Second
}
// Timeout helpers (configurable via env vars)
func getRequestTimeout() time.Duration { return getPositiveTimeout("REQUEST_TIMEOUT_SECONDS", 60) }
func getAITimeout() time.Duration { return getPositiveTimeout("AI_REQUEST_TIMEOUT_SECONDS", 30) }
func getVerifierTimeout() time.Duration { return getPositiveTimeout("VERIFIER_TIMEOUT_SECONDS", 2) }
func getHealthCheckTimeout() time.Duration {
return getPositiveTimeout("HEALTH_CHECK_TIMEOUT_SECONDS", 2)
}
// getMaxBodySize returns the maximum request body size in bytes, configured via
// the MAX_REQUEST_BODY_MB environment variable. Defaults to 10MB.
func getMaxBodySize() int64 {
mb := getEnvAsInt("MAX_REQUEST_BODY_MB", 10)
return int64(mb) * 1024 * 1024
}