Browse Source

Merge commit '4b2195d1743fd78083862bcbe18c5f10a746a54b' into HEAD

feature/update-route-registration
Jenkins 3 years ago
committed by Fabian Vowie
parent
commit
8df80d159f
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 13
      main.go
  2. 12
      settings/settings.go
  3. 8
      settings/settings_test.go

13
main.go

@ -63,15 +63,22 @@ func main() {
pipes := pipelines.LoadPipelines() 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) rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst)
if err != nil { if err != nil {
panic(err) panic(err)
} }
r := mux.NewRouter()
r.Use(authMiddleware.Middleware)
r.Use(rateLimiterMiddleware.Middleware) r.Use(rateLimiterMiddleware.Middleware)
}
r.HandleFunc("/", IndexHandler) r.HandleFunc("/", IndexHandler)

12
settings/settings.go

@ -16,7 +16,7 @@ type FileSystemType int
type Settings struct { type Settings struct {
Endpoint string `json:"endpoint"` Endpoint string `json:"endpoint"`
Token string `json:"token"`
Authentication AuthenticationSettings `json:"authentication"`
RateLimiter RateLimiterSettings `json:"rate_limiter"` RateLimiter RateLimiterSettings `json:"rate_limiter"`
StorageProvider StorageSettings `json:"storage_provider"` StorageProvider StorageSettings `json:"storage_provider"`
} }
@ -26,7 +26,13 @@ type StorageSettings struct {
BasePath string `json:"base_path"` BasePath string `json:"base_path"`
} }
type AuthenticationSettings struct {
Enabled bool `json:"enabled"`
Token string `json:"token"`
}
type RateLimiterSettings struct { type RateLimiterSettings struct {
Enabled bool `json:"enabled"`
RequestsPerMinute int `json:"requests_per_minute"` RequestsPerMinute int `json:"requests_per_minute"`
AllowedBurst int `json:"allowed_burst"` AllowedBurst int `json:"allowed_burst"`
} }
@ -56,8 +62,12 @@ func LoadSettings(fileSystem afero.Fs) (Settings, error) {
// If file does not exist, create default settings // If file does not exist, create default settings
defaultSettings := Settings{ defaultSettings := Settings{
Endpoint: "127.0.0.1:8000", Endpoint: "127.0.0.1:8000",
Authentication: AuthenticationSettings{
Enabled: true,
Token: "changeme", Token: "changeme",
},
RateLimiter: RateLimiterSettings{ RateLimiter: RateLimiterSettings{
Enabled: true,
RequestsPerMinute: 20, RequestsPerMinute: 20,
AllowedBurst: 5, AllowedBurst: 5,
}, },

8
settings/settings_test.go

@ -12,8 +12,12 @@ import (
func TestSettingsParsing(t *testing.T) { func TestSettingsParsing(t *testing.T) {
const file string = `{ const file string = `{
"endpoint": "0.0.0.0:8000", "endpoint": "0.0.0.0:8000",
"token": "foobar",
"authentication": {
"enabled": true,
"token": "foobar"
},
"rate_limiter": { "rate_limiter": {
"enabled": true,
"requests_per_minute": 20, "requests_per_minute": 20,
"allowed_burst": 5 "allowed_burst": 5
}, },
@ -27,7 +31,7 @@ func TestSettingsParsing(t *testing.T) {
settings := parseSettings([]byte(file)) settings := parseSettings([]byte(file))
assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) 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) assert.Equal(t, "assets", settings.StorageProvider.BasePath)
}) })
} }

Loading…
Cancel
Save