Skip to content

Commit 22f3480

Browse files
committed
feat(config): add poke strategy directive
1 parent c3f95ad commit 22f3480

3 files changed

Lines changed: 96 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ You can have the following configuration:
5959
blocking {
6060
[timeout 1m]
6161
}
62+
poke
6263
}
6364
reverse_proxy myservice:port
6465
}

config.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ type BlockingConfiguration struct {
2828
Timeout *time.Duration
2929
}
3030

31+
type PokeConfiguration struct {
32+
}
33+
3134
type Config struct {
3235
SablierURL string
3336
Names []string
3437
Group string
3538
SessionDuration *time.Duration
3639
Dynamic *DynamicConfiguration
3740
Blocking *BlockingConfiguration
41+
Poke *PokeConfiguration
3842
}
3943

4044
func CreateConfig() *Config {
@@ -44,6 +48,7 @@ func CreateConfig() *Config {
4448
SessionDuration: nil,
4549
Dynamic: nil,
4650
Blocking: nil,
51+
Poke: nil,
4752
}
4853
}
4954

@@ -62,6 +67,7 @@ func CreateConfig() *Config {
6267
// blocking {
6368
// [timeout 1m]
6469
// }
70+
// poke
6571
// }
6672
//
6773
func (c *Config) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
@@ -100,15 +106,26 @@ func (c *Config) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
100106
return err
101107
}
102108
c.Blocking = blocking
109+
case "poke":
110+
c.Poke = &PokeConfiguration{}
103111
}
104112
}
105113
}
106114

107-
if c.Blocking == nil && c.Dynamic == nil {
108-
return fmt.Errorf("you must specify one strategy (dynamic or blocking)")
115+
strategyCount := 0
116+
if c.Blocking != nil {
117+
strategyCount++
109118
}
110-
111-
if c.Blocking != nil && c.Dynamic != nil {
119+
if c.Dynamic != nil {
120+
strategyCount++
121+
}
122+
if c.Poke != nil {
123+
strategyCount++
124+
}
125+
if strategyCount == 0 {
126+
return fmt.Errorf("you must specify one strategy (dynamic, blocking or poke)")
127+
}
128+
if strategyCount > 1 {
112129
return fmt.Errorf("you must specify only one strategy")
113130
}
114131

@@ -195,6 +212,8 @@ func (c *Config) BuildRequest() (*http.Request, error) {
195212
return c.buildDynamicRequest()
196213
} else if c.Blocking != nil {
197214
return c.buildBlockingRequest()
215+
} else if c.Poke != nil {
216+
return c.buildPokeRequest()
198217
}
199218
return nil, fmt.Errorf("no strategy configured")
200219
}
@@ -276,3 +295,32 @@ func (c *Config) buildBlockingRequest() (*http.Request, error) {
276295

277296
return request, nil
278297
}
298+
299+
func (c *Config) buildPokeRequest() (*http.Request, error) {
300+
if c.Poke == nil {
301+
return nil, fmt.Errorf("poke config is nil")
302+
}
303+
304+
request, err := http.NewRequest("GET", fmt.Sprintf("%s/api/strategies/poke", c.SablierURL), nil)
305+
if err != nil {
306+
return nil, err
307+
}
308+
309+
q := request.URL.Query()
310+
311+
if c.SessionDuration != nil {
312+
q.Add("session_duration", c.SessionDuration.String())
313+
}
314+
315+
for _, name := range c.Names {
316+
q.Add("names", name)
317+
}
318+
319+
if c.Group != "" {
320+
q.Add("group", c.Group)
321+
}
322+
323+
request.URL.RawQuery = q.Encode()
324+
325+
return request, nil
326+
}

config_test.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ func TestConfig_BuildRequest(t *testing.T) {
2323
want *http.Request
2424
wantErr bool
2525
}{
26+
{
27+
name: "poke session with names",
28+
fields: caddy.Config{
29+
SablierURL: "http://sablier:10000",
30+
Names: []string{"nginx", "apache"},
31+
SessionDuration: &oneMinute,
32+
Poke: &caddy.PokeConfiguration{},
33+
},
34+
want: createRequest("GET", "http://sablier:10000/api/strategies/poke?names=nginx&names=apache&session_duration=1m", nil),
35+
wantErr: false,
36+
},
2637
{
2738
name: "dynamic session with required values",
2839
fields: caddy.Config{
@@ -193,6 +204,7 @@ func TestConfig_BuildRequest(t *testing.T) {
193204
SessionDuration: tt.fields.SessionDuration,
194205
Dynamic: tt.fields.Dynamic,
195206
Blocking: tt.fields.Blocking,
207+
Poke: tt.fields.Poke,
196208
}
197209

198210
got, err := c.BuildRequest()
@@ -308,17 +320,32 @@ func TestConfig_UnmarshalCaddyfile(t *testing.T) {
308320
},
309321
wantErr: false,
310322
},
323+
{
324+
name: "parse valid poke strategy",
325+
input: `sablier {
326+
group mygroup
327+
session_duration 1m
328+
poke
329+
}`,
330+
want: caddy.Config{
331+
SablierURL: "http://sablier:10000",
332+
Group: "mygroup",
333+
SessionDuration: &oneMinute,
334+
Poke: &caddy.PokeConfiguration{},
335+
},
336+
wantErr: false,
337+
},
311338
{
312339
name: "parse invalid no strategies",
313340
input: `sablier`,
314341
want: caddy.Config{
315342
SablierURL: "http://sablier:10000",
316343
},
317344
wantErr: true,
318-
wantErrValue: "you must specify one strategy (dynamic or blocking)",
345+
wantErrValue: "you must specify one strategy (dynamic, blocking or poke)",
319346
},
320347
{
321-
name: "parse invalid two strategies",
348+
name: "parse invalid two strategies dynamic-blocking",
322349
input: `sablier {
323350
blocking
324351
dynamic
@@ -329,6 +356,20 @@ func TestConfig_UnmarshalCaddyfile(t *testing.T) {
329356
wantErr: true,
330357
wantErrValue: "you must specify only one strategy",
331358
},
359+
{
360+
name: "parse invalid two strategies poke-blocking",
361+
input: `sablier {
362+
poke
363+
blocking {
364+
timeout 1m
365+
}
366+
}`,
367+
want: caddy.Config{
368+
SablierURL: "http://sablier:10000",
369+
},
370+
wantErr: true,
371+
wantErrValue: "you must specify only one strategy",
372+
},
332373
{
333374
name: "parse invalid no names or group",
334375
input: `sablier {

0 commit comments

Comments
 (0)