forked from koryxio/koryx-serv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
189 lines (168 loc) · 6.08 KB
/
config.go
File metadata and controls
189 lines (168 loc) · 6.08 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package koryxserv
import (
"encoding/json"
"os"
"time"
)
// Config represents the full server configuration
type Config struct {
Server ServerConfig `json:"server"`
Security SecurityConfig `json:"security"`
Performance PerformanceConfig `json:"performance"`
Logging LoggingConfig `json:"logging"`
Features FeaturesConfig `json:"features"`
RuntimeConfig *RuntimeConfigConfig `json:"runtime_config,omitempty"`
}
// ServerConfig contains basic server settings
type ServerConfig struct {
Port int `json:"port"`
Host string `json:"host"`
RootDir string `json:"root_dir"`
ReadTimeout int `json:"read_timeout"` // seconds
WriteTimeout int `json:"write_timeout"` // seconds
}
// SecurityConfig contains security settings
type SecurityConfig struct {
EnableHTTPS bool `json:"enable_https"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
BasicAuth *BasicAuthConfig `json:"basic_auth,omitempty"`
CORS *CORSConfig `json:"cors,omitempty"`
RateLimit *RateLimitConfig `json:"rate_limit,omitempty"`
IPWhitelist []string `json:"ip_whitelist,omitempty"`
IPBlacklist []string `json:"ip_blacklist,omitempty"`
BlockHiddenFiles bool `json:"block_hidden_files"`
AllowedPaths []string `json:"allowed_paths,omitempty"`
BlockedPaths []string `json:"blocked_paths,omitempty"`
}
// BasicAuthConfig configures HTTP basic authentication
type BasicAuthConfig struct {
Enabled bool `json:"enabled"`
Username string `json:"username"`
Password string `json:"password"`
Realm string `json:"realm"`
}
// CORSConfig contains CORS settings
type CORSConfig struct {
Enabled bool `json:"enabled"`
AllowedOrigins []string `json:"allowed_origins"`
AllowedMethods []string `json:"allowed_methods"`
AllowedHeaders []string `json:"allowed_headers"`
AllowCredentials bool `json:"allow_credentials"`
MaxAge int `json:"max_age"`
}
// RateLimitConfig defines rate limit settings
type RateLimitConfig struct {
Enabled bool `json:"enabled"`
RequestsPerIP int `json:"requests_per_ip"` // requests per minute
BurstSize int `json:"burst_size"`
}
// PerformanceConfig contains performance settings
type PerformanceConfig struct {
EnableCompression bool `json:"enable_compression"`
CompressionLevel int `json:"compression_level"` // 1-9
EnableCache bool `json:"enable_cache"`
CacheMaxAge int `json:"cache_max_age"` // seconds
EnableETags bool `json:"enable_etags"`
CustomHeaders map[string]string `json:"custom_headers,omitempty"`
}
// LoggingConfig contains logging settings
type LoggingConfig struct {
Enabled bool `json:"enabled"`
Level string `json:"level"` // debug, info, warn, error
AccessLog bool `json:"access_log"`
ErrorLog bool `json:"error_log"`
LogFile string `json:"log_file,omitempty"`
ColorOutput bool `json:"color_output"`
}
// FeaturesConfig contains additional features
type FeaturesConfig struct {
DirectoryListing bool `json:"directory_listing"`
IndexFiles []string `json:"index_files"`
SPAMode bool `json:"spa_mode"` // redirect all routes to index.html
SPAIndex string `json:"spa_index"`
CustomErrorPages map[string]string `json:"custom_error_pages,omitempty"`
}
// RuntimeConfigConfig configures runtime config output
type RuntimeConfigConfig struct {
Enabled bool `json:"enabled"`
Route string `json:"route"` // route where config is served (default: /runtime-config.js)
Format string `json:"format"` // "js" or "json" (default: js)
VarName string `json:"var_name"` // JavaScript variable name (default: APP_CONFIG)
EnvPrefix string `json:"env_prefix"` // env var prefix (e.g., "APP_" or "RUNTIME_")
EnvVariables []string `json:"env_variables"` // specific variable list (alternative to prefix)
NoCache bool `json:"no_cache"` // if true, add no-cache headers
}
// DefaultConfig returns the default configuration
func DefaultConfig() *Config {
return &Config{
Server: ServerConfig{
Port: 8080,
Host: "0.0.0.0",
RootDir: ".",
ReadTimeout: 30,
WriteTimeout: 30,
},
Security: SecurityConfig{
EnableHTTPS: false,
BlockHiddenFiles: true,
},
Performance: PerformanceConfig{
EnableCompression: true,
CompressionLevel: 6,
EnableCache: true,
CacheMaxAge: 3600,
EnableETags: true,
},
Logging: LoggingConfig{
Enabled: true,
Level: "info",
AccessLog: true,
ErrorLog: true,
ColorOutput: true,
},
Features: FeaturesConfig{
DirectoryListing: false,
IndexFiles: []string{"index.html", "index.htm"},
SPAMode: false,
SPAIndex: "index.html",
},
}
}
// LoadConfig loads configuration from a JSON file
func LoadConfig(filename string) (*Config, error) {
config := DefaultConfig()
// If file does not exist, return default configuration
if _, err := os.Stat(filename); os.IsNotExist(err) {
return config, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(config); err != nil {
return nil, err
}
return config, nil
}
// SaveConfig saves configuration to a JSON file
func SaveConfig(filename string, config *Config) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return encoder.Encode(config)
}
// GetReadTimeout returns the read timeout as Duration
func (c *ServerConfig) GetReadTimeout() time.Duration {
return time.Duration(c.ReadTimeout) * time.Second
}
// GetWriteTimeout returns the write timeout as Duration
func (c *ServerConfig) GetWriteTimeout() time.Duration {
return time.Duration(c.WriteTimeout) * time.Second
}