Fabian Vowie
3 years ago
No known key found for this signature in database
GPG Key ID: C27317C33B27C410
3 changed files with
29 additions and
14 deletions
-
main.go
-
settings/settings.go
-
settings/settings_test.go
|
|
@ -63,16 +63,20 @@ func main() { |
|
|
|
|
|
|
|
pipes := pipelines.LoadPipelines() |
|
|
|
|
|
|
|
authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Token) |
|
|
|
r := mux.NewRouter() |
|
|
|
|
|
|
|
if appSettings.Authentication.Enabled { |
|
|
|
authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token) |
|
|
|
|
|
|
|
r.Use(authMiddleware.Middleware) |
|
|
|
} |
|
|
|
|
|
|
|
if appSettings.RateLimiter.Enabled { |
|
|
|
rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
|
|
|
|
r := mux.NewRouter() |
|
|
|
r.Use(authMiddleware.Middleware) |
|
|
|
|
|
|
|
if appSettings.RateLimiter.Enabled { |
|
|
|
r.Use(rateLimiterMiddleware.Middleware) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -16,7 +16,7 @@ type FileSystemType int |
|
|
|
|
|
|
|
type Settings struct { |
|
|
|
Endpoint string `json:"endpoint"` |
|
|
|
Token string `json:"token"` |
|
|
|
Authentication AuthenticationSettings `json:"authentication"` |
|
|
|
RateLimiter RateLimiterSettings `json:"rate_limiter"` |
|
|
|
StorageProvider StorageSettings `json:"storage_provider"` |
|
|
|
} |
|
|
@ -26,6 +26,11 @@ type StorageSettings struct { |
|
|
|
BasePath string `json:"base_path"` |
|
|
|
} |
|
|
|
|
|
|
|
type AuthenticationSettings struct { |
|
|
|
Enabled bool `json:"enabled"` |
|
|
|
Token string `json:"token"` |
|
|
|
} |
|
|
|
|
|
|
|
type RateLimiterSettings struct { |
|
|
|
Enabled bool `json:"enabled"` |
|
|
|
RequestsPerMinute int `json:"requests_per_minute"` |
|
|
@ -57,7 +62,10 @@ func LoadSettings(fileSystem afero.Fs) (Settings, error) { |
|
|
|
// If file does not exist, create default settings
|
|
|
|
defaultSettings := Settings{ |
|
|
|
Endpoint: "127.0.0.1:8000", |
|
|
|
Authentication: AuthenticationSettings{ |
|
|
|
Enabled: true, |
|
|
|
Token: "changeme", |
|
|
|
}, |
|
|
|
RateLimiter: RateLimiterSettings{ |
|
|
|
Enabled: true, |
|
|
|
RequestsPerMinute: 20, |
|
|
|
|
|
@ -12,7 +12,10 @@ import ( |
|
|
|
func TestSettingsParsing(t *testing.T) { |
|
|
|
const file string = `{ |
|
|
|
"endpoint": "0.0.0.0:8000", |
|
|
|
"token": "foobar", |
|
|
|
"authentication": { |
|
|
|
"enabled": true, |
|
|
|
"token": "foobar" |
|
|
|
}, |
|
|
|
"rate_limiter": { |
|
|
|
"enabled": true, |
|
|
|
"requests_per_minute": 20, |
|
|
@ -28,7 +31,7 @@ func TestSettingsParsing(t *testing.T) { |
|
|
|
settings := parseSettings([]byte(file)) |
|
|
|
|
|
|
|
assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) |
|
|
|
assert.Equal(t, "foobar", settings.Token) |
|
|
|
assert.Equal(t, "foobar", settings.Authentication.Token) |
|
|
|
assert.Equal(t, "assets", settings.StorageProvider.BasePath) |
|
|
|
}) |
|
|
|
} |
|
|
|