mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-26 07:13:36 +01:00
feat(traefik): add refreshFrequency support for dynamic strategy
This commit is contained in:
@@ -16,7 +16,8 @@ testData:
|
||||
# Dynamic strategy, provides the waiting webui
|
||||
dynamic:
|
||||
displayName: My Title # (Optional) Defaults to the middleware name
|
||||
theme: hacker-terminal # (Optional)
|
||||
theme: hacker-terminal # (Optional) The theme to use
|
||||
refreshFrequency: 5s # (Optional) The loading page refresh frequency
|
||||
|
||||
# Blocking strategy, waits until services are up and running
|
||||
# but will not wait more than `timeout`
|
||||
|
||||
@@ -4,11 +4,13 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DynamicConfiguration struct {
|
||||
DisplayName string `yaml:"displayname"`
|
||||
Theme string `yaml:"theme"`
|
||||
DisplayName string `yaml:"displayname"`
|
||||
Theme string `yaml:"theme"`
|
||||
RefreshFrequency string `yaml:"refreshFrequency"`
|
||||
}
|
||||
|
||||
type BlockingConfiguration struct {
|
||||
@@ -76,6 +78,12 @@ func (c *Config) buildDynamicRequest(middlewareName string) (*http.Request, erro
|
||||
|
||||
q := request.URL.Query()
|
||||
|
||||
_, err = time.ParseDuration(c.SessionDuration)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing dynamic.sessionDuration: %v", err)
|
||||
}
|
||||
|
||||
q.Add("session_duration", c.SessionDuration)
|
||||
for _, name := range c.splittedNames {
|
||||
q.Add("names", name)
|
||||
@@ -92,6 +100,16 @@ func (c *Config) buildDynamicRequest(middlewareName string) (*http.Request, erro
|
||||
q.Add("theme", c.Dynamic.Theme)
|
||||
}
|
||||
|
||||
if c.Dynamic.RefreshFrequency != "" {
|
||||
_, err := time.ParseDuration(c.Dynamic.RefreshFrequency)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing dynamic.refreshFrequency: %v", err)
|
||||
}
|
||||
|
||||
q.Add("refresh_frequency", c.Dynamic.RefreshFrequency)
|
||||
}
|
||||
|
||||
request.URL.RawQuery = q.Encode()
|
||||
|
||||
return request, nil
|
||||
@@ -115,6 +133,12 @@ func (c *Config) buildBlockingRequest() (*http.Request, error) {
|
||||
}
|
||||
|
||||
if c.Blocking.Timeout != "" {
|
||||
_, err := time.ParseDuration(c.Blocking.Timeout)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error paring blocking.timeout: %v", err)
|
||||
}
|
||||
|
||||
q.Add("timeout", c.Blocking.Timeout)
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,43 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?display_name=Hello+World%21&names=nginx&names=apache&session_duration=1m&theme=hacker-terminal", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "dynamic session with invalid session duration",
|
||||
fields: fields{
|
||||
SablierURL: "http://sablier:10000",
|
||||
Names: "nginx , apache",
|
||||
SessionDuration: "invalid",
|
||||
Dynamic: &traefik.DynamicConfiguration{},
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "dynamic session with refresh frequency",
|
||||
fields: fields{
|
||||
SablierURL: "http://sablier:10000",
|
||||
Names: "nginx , apache",
|
||||
SessionDuration: "1m",
|
||||
Dynamic: &traefik.DynamicConfiguration{
|
||||
RefreshFrequency: "1m",
|
||||
},
|
||||
},
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?display_name=sablier-middleware&names=nginx&names=apache&refresh_frequency=1m&session_duration=1m", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "dynamic session with invalid refresh frequency",
|
||||
fields: fields{
|
||||
SablierURL: "http://sablier:10000",
|
||||
Names: "nginx , apache",
|
||||
SessionDuration: "1m",
|
||||
Dynamic: &traefik.DynamicConfiguration{
|
||||
RefreshFrequency: "invalid",
|
||||
},
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "blocking session with default values",
|
||||
fields: fields{
|
||||
@@ -85,6 +122,19 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/blocking?names=nginx&names=apache&session_duration=1m&timeout=5m", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "blocking session with invalid timeout value",
|
||||
fields: fields{
|
||||
SablierURL: "http://sablier:10000",
|
||||
Names: "nginx , apache",
|
||||
SessionDuration: "1m",
|
||||
Blocking: &traefik.BlockingConfiguration{
|
||||
Timeout: "invalid",
|
||||
},
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "both strategies defined",
|
||||
fields: fields{
|
||||
|
||||
@@ -37,7 +37,8 @@ func TestSablierMiddleware_ServeHTTP(t *testing.T) {
|
||||
fmt.Fprint(w, "response from service")
|
||||
}),
|
||||
Config: &Config{
|
||||
Dynamic: &DynamicConfiguration{},
|
||||
SessionDuration: "1m",
|
||||
Dynamic: &DynamicConfiguration{},
|
||||
},
|
||||
},
|
||||
expected: "response from service",
|
||||
@@ -55,7 +56,8 @@ func TestSablierMiddleware_ServeHTTP(t *testing.T) {
|
||||
fmt.Fprint(w, "response from service")
|
||||
}),
|
||||
Config: &Config{
|
||||
Dynamic: &DynamicConfiguration{},
|
||||
SessionDuration: "1m",
|
||||
Dynamic: &DynamicConfiguration{},
|
||||
},
|
||||
},
|
||||
expected: "response from sablier",
|
||||
|
||||
Reference in New Issue
Block a user