mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 21:33:06 +01:00
feat(traefik): DisplayName defaults as the middleware name
This commit is contained in:
@@ -35,7 +35,7 @@ func CreateConfig() *Config {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) BuildRequest() (*http.Request, error) {
|
||||
func (c *Config) BuildRequest(middlewareName string) (*http.Request, error) {
|
||||
|
||||
if len(c.SablierURL) == 0 {
|
||||
return nil, fmt.Errorf("sablierURL cannot be empty")
|
||||
@@ -57,14 +57,14 @@ func (c *Config) BuildRequest() (*http.Request, error) {
|
||||
}
|
||||
|
||||
if c.Dynamic != nil {
|
||||
return c.buildDynamicRequest()
|
||||
return c.buildDynamicRequest(middlewareName)
|
||||
} else if c.Blocking != nil {
|
||||
return c.buildBlockingRequest()
|
||||
}
|
||||
return nil, fmt.Errorf("no strategy configured")
|
||||
}
|
||||
|
||||
func (c *Config) buildDynamicRequest() (*http.Request, error) {
|
||||
func (c *Config) buildDynamicRequest(middlewareName string) (*http.Request, error) {
|
||||
if c.Dynamic == nil {
|
||||
return nil, fmt.Errorf("dynamic config is nil")
|
||||
}
|
||||
@@ -83,6 +83,9 @@ func (c *Config) buildDynamicRequest() (*http.Request, error) {
|
||||
|
||||
if c.Dynamic.DisplayName != "" {
|
||||
q.Add("display_name", c.Dynamic.DisplayName)
|
||||
} else {
|
||||
// display name defaults as middleware name
|
||||
q.Add("display_name", middlewareName)
|
||||
}
|
||||
|
||||
if c.Dynamic.Theme != "" {
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
SessionDuration: "1m",
|
||||
Dynamic: &traefik.DynamicConfiguration{},
|
||||
},
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?names=nginx&names=apache&session_duration=1m", nil),
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?display_name=sablier-middleware&names=nginx&names=apache&session_duration=1m", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
@@ -44,7 +44,7 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
Theme: "hacker-terminal",
|
||||
},
|
||||
},
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?names=nginx&names=apache&session_duration=1m&theme=hacker-terminal", nil),
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?display_name=sablier-middleware&names=nginx&names=apache&session_duration=1m&theme=hacker-terminal", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
@@ -108,7 +108,7 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
Blocking: tt.fields.Blocking,
|
||||
}
|
||||
|
||||
got, err := c.BuildRequest()
|
||||
got, err := c.BuildRequest("sablier-middleware")
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Config.BuildRequest() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
||||
@@ -14,7 +14,7 @@ type SablierMiddleware struct {
|
||||
|
||||
// New function creates the configuration
|
||||
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
|
||||
req, err := config.BuildRequest()
|
||||
req, err := config.BuildRequest(name)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user