-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
152 lines (137 loc) · 3.91 KB
/
Copy pathconfig.go
File metadata and controls
152 lines (137 loc) · 3.91 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package config
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
defaultAppName = "wherobots"
defaultOpenAPISpec = "https://api.cloud.wherobots.com/openapi.json"
defaultCacheTTL = 15 * time.Minute
defaultHTTPTimeout = 30 * time.Second
envAppName = "APP_NAME"
envWherobotsAPIURL = "WHEROBOTS_API_URL"
envWherobotsAPIKey = "WHEROBOTS_API_KEY"
envWherobotsUploadPath = "WHEROBOTS_UPLOAD_PATH"
envOpenAPICacheTTL = "OPENAPI_CACHE_TTL"
envHTTPTimeout = "OPENAPI_HTTP_TIMEOUT"
)
type Config struct {
AppName string
OpenAPIURL string
APIKey string
CachePath string
CacheMeta string
CacheTTL time.Duration
HTTPTimeout time.Duration
UploadPath string
}
func Load() (Config, error) {
appName := getenvDefault(envAppName, defaultAppName)
openAPIURL, err := resolveOpenAPISpecURL(os.Getenv(envWherobotsAPIURL))
if err != nil {
return Config{}, err
}
apiKey := strings.TrimSpace(os.Getenv(envWherobotsAPIKey))
cacheRoot, err := os.UserCacheDir()
if err != nil {
return Config{}, fmt.Errorf("resolve user cache dir: %w", err)
}
cacheDir := filepath.Join(cacheRoot, appName)
ttl, err := parseTTL(os.Getenv(envOpenAPICacheTTL))
if err != nil {
return Config{}, err
}
timeout, err := parseDuration(os.Getenv(envHTTPTimeout), defaultHTTPTimeout)
if err != nil {
return Config{}, fmt.Errorf("parse %s: %w", envHTTPTimeout, err)
}
uploadPath := strings.TrimSpace(os.Getenv(envWherobotsUploadPath))
return Config{
AppName: appName,
OpenAPIURL: openAPIURL,
APIKey: apiKey,
CachePath: filepath.Join(cacheDir, "spec.json"),
CacheMeta: filepath.Join(cacheDir, "spec.meta.json"),
CacheTTL: ttl,
HTTPTimeout: timeout,
UploadPath: uploadPath,
}, nil
}
// RequireAPIKey returns an error with setup instructions when the API key is
// empty, or nil when a key is present.
func (c Config) RequireAPIKey() error {
if c.APIKey != "" {
return nil
}
return fmt.Errorf(
"%s is required\n\nTo create an API key, visit: %s\nThen export it:\n\n export %s='<your-api-key>'",
envWherobotsAPIKey, apiKeyURL(c.OpenAPIURL), envWherobotsAPIKey,
)
}
func resolveOpenAPISpecURL(baseURL string) (string, error) {
raw := strings.TrimSpace(baseURL)
if raw == "" {
return defaultOpenAPISpec, nil
}
raw = strings.TrimRight(raw, "/")
if !strings.HasSuffix(raw, "/openapi.json") {
raw += "/openapi.json"
}
parsed, err := url.Parse(raw)
if err != nil || !parsed.IsAbs() {
return "", fmt.Errorf("%s must be an absolute URL", envWherobotsAPIURL)
}
return parsed.String(), nil
}
func parseTTL(raw string) (time.Duration, error) {
if raw == "" {
return defaultCacheTTL, nil
}
d, err := parseDuration(raw, defaultCacheTTL)
if err != nil {
return 0, fmt.Errorf("parse %s: %w", envOpenAPICacheTTL, err)
}
return d, nil
}
func parseDuration(raw string, fallback time.Duration) (time.Duration, error) {
if raw == "" {
return fallback, nil
}
if asInt, err := strconv.Atoi(raw); err == nil {
if asInt <= 0 {
return 0, fmt.Errorf("must be > 0, got %d", asInt)
}
return time.Duration(asInt) * time.Minute, nil
}
d, err := time.ParseDuration(raw)
if err != nil {
return 0, err
}
if d <= 0 {
return 0, fmt.Errorf("must be > 0, got %s", d)
}
return d, nil
}
// apiKeyURL derives the console settings URL from the resolved OpenAPI spec URL.
// It strips the "api." prefix from the host (e.g. api.cloud.wherobots.com → cloud.wherobots.com)
// and appends /settings#api-keys.
func apiKeyURL(openAPISpecURL string) string {
parsed, err := url.Parse(openAPISpecURL)
if err != nil {
return "https://cloud.wherobots.com/settings#api-keys"
}
host := parsed.Hostname()
host = strings.TrimPrefix(host, "api.")
return fmt.Sprintf("%s://%s/settings#api-keys", parsed.Scheme, host)
}
func getenvDefault(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}