Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ You can have the following configuration:
blocking {
[timeout 1m]
}
poke
}
reverse_proxy myservice:port
}
Expand Down
56 changes: 52 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ type BlockingConfiguration struct {
Timeout *time.Duration
}

type PokeConfiguration struct {
}

type Config struct {
SablierURL string
Names []string
Group string
SessionDuration *time.Duration
Dynamic *DynamicConfiguration
Blocking *BlockingConfiguration
Poke *PokeConfiguration
}

func CreateConfig() *Config {
Expand All @@ -44,6 +48,7 @@ func CreateConfig() *Config {
SessionDuration: nil,
Dynamic: nil,
Blocking: nil,
Poke: nil,
}
}

Expand All @@ -62,6 +67,7 @@ func CreateConfig() *Config {
// blocking {
// [timeout 1m]
// }
// poke
// }
//
func (c *Config) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
Expand Down Expand Up @@ -100,15 +106,26 @@ func (c *Config) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return err
}
c.Blocking = blocking
case "poke":
c.Poke = &PokeConfiguration{}
}
}
}

if c.Blocking == nil && c.Dynamic == nil {
return fmt.Errorf("you must specify one strategy (dynamic or blocking)")
strategyCount := 0
if c.Blocking != nil {
strategyCount++
}

if c.Blocking != nil && c.Dynamic != nil {
if c.Dynamic != nil {
strategyCount++
}
if c.Poke != nil {
strategyCount++
}
if strategyCount == 0 {
return fmt.Errorf("you must specify one strategy (dynamic, blocking or poke)")
}
if strategyCount > 1 {
return fmt.Errorf("you must specify only one strategy")
}

Expand Down Expand Up @@ -195,6 +212,8 @@ func (c *Config) BuildRequest() (*http.Request, error) {
return c.buildDynamicRequest()
} else if c.Blocking != nil {
return c.buildBlockingRequest()
} else if c.Poke != nil {
return c.buildPokeRequest()
}
return nil, fmt.Errorf("no strategy configured")
}
Expand Down Expand Up @@ -276,3 +295,32 @@ func (c *Config) buildBlockingRequest() (*http.Request, error) {

return request, nil
}

func (c *Config) buildPokeRequest() (*http.Request, error) {
if c.Poke == nil {
return nil, fmt.Errorf("poke config is nil")
}

request, err := http.NewRequest("GET", fmt.Sprintf("%s/api/strategies/poke", c.SablierURL), nil)
if err != nil {
return nil, err
}

q := request.URL.Query()

if c.SessionDuration != nil {
q.Add("session_duration", c.SessionDuration.String())
}

for _, name := range c.Names {
q.Add("names", name)
}

if c.Group != "" {
q.Add("group", c.Group)
}

request.URL.RawQuery = q.Encode()

return request, nil
}
45 changes: 43 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func TestConfig_BuildRequest(t *testing.T) {
want *http.Request
wantErr bool
}{
{
name: "poke session with names",
fields: caddy.Config{
SablierURL: "http://sablier:10000",
Names: []string{"nginx", "apache"},
SessionDuration: &oneMinute,
Poke: &caddy.PokeConfiguration{},
},
want: createRequest("GET", "http://sablier:10000/api/strategies/poke?names=nginx&names=apache&session_duration=1m", nil),
wantErr: false,
},
{
name: "dynamic session with required values",
fields: caddy.Config{
Expand Down Expand Up @@ -193,6 +204,7 @@ func TestConfig_BuildRequest(t *testing.T) {
SessionDuration: tt.fields.SessionDuration,
Dynamic: tt.fields.Dynamic,
Blocking: tt.fields.Blocking,
Poke: tt.fields.Poke,
}

got, err := c.BuildRequest()
Expand Down Expand Up @@ -308,17 +320,32 @@ func TestConfig_UnmarshalCaddyfile(t *testing.T) {
},
wantErr: false,
},
{
name: "parse valid poke strategy",
input: `sablier {
group mygroup
session_duration 1m
poke
}`,
want: caddy.Config{
SablierURL: "http://sablier:10000",
Group: "mygroup",
SessionDuration: &oneMinute,
Poke: &caddy.PokeConfiguration{},
},
wantErr: false,
},
{
name: "parse invalid no strategies",
input: `sablier`,
want: caddy.Config{
SablierURL: "http://sablier:10000",
},
wantErr: true,
wantErrValue: "you must specify one strategy (dynamic or blocking)",
wantErrValue: "you must specify one strategy (dynamic, blocking or poke)",
},
{
name: "parse invalid two strategies",
name: "parse invalid two strategies dynamic-blocking",
input: `sablier {
blocking
dynamic
Expand All @@ -329,6 +356,20 @@ func TestConfig_UnmarshalCaddyfile(t *testing.T) {
wantErr: true,
wantErrValue: "you must specify only one strategy",
},
{
name: "parse invalid two strategies poke-blocking",
input: `sablier {
poke
blocking {
timeout 1m
}
}`,
want: caddy.Config{
SablierURL: "http://sablier:10000",
},
wantErr: true,
wantErrValue: "you must specify only one strategy",
},
{
name: "parse invalid no names or group",
input: `sablier {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (sm SablierMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request,
//nolint:errcheck
defer resp.Body.Close()

if resp.Header.Get("X-Sablier-Session-Status") == "ready" {
if resp.Header.Get("X-Sablier-Session-Status") == "ready" || sm.Config.Poke != nil {
return next.ServeHTTP(rw, req)
}

Expand Down