mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-24 06:28:21 +01:00
fix(config): set session_duration optional with default value from sessions.default-duration
This commit is contained in:
@@ -4,6 +4,6 @@ import "time"
|
||||
|
||||
type BlockingRequest struct {
|
||||
Names []string `form:"names" binding:"required"`
|
||||
SessionDuration time.Duration `form:"session_duration" binding:"required"`
|
||||
SessionDuration time.Duration `form:"session_duration"`
|
||||
Timeout time.Duration `form:"timeout"`
|
||||
}
|
||||
|
||||
@@ -9,6 +9,6 @@ type DynamicRequest struct {
|
||||
ShowDetails bool `form:"show_details"`
|
||||
DisplayName string `form:"display_name"`
|
||||
Theme string `form:"theme"`
|
||||
SessionDuration time.Duration `form:"session_duration" binding:"required"`
|
||||
SessionDuration time.Duration `form:"session_duration"`
|
||||
RefreshFrequency time.Duration `form:"refresh_frequency"`
|
||||
}
|
||||
|
||||
@@ -28,17 +28,19 @@ type ServeStrategy struct {
|
||||
|
||||
SessionsManager sessions.Manager
|
||||
StrategyConfig config.Strategy
|
||||
SessionsConfig config.Sessions
|
||||
}
|
||||
|
||||
func NewServeStrategy(sessionsManager sessions.Manager, conf config.Strategy) *ServeStrategy {
|
||||
func NewServeStrategy(sessionsManager sessions.Manager, strategyConf config.Strategy, sessionsConf config.Sessions) *ServeStrategy {
|
||||
|
||||
serveStrategy := &ServeStrategy{
|
||||
SessionsManager: sessionsManager,
|
||||
StrategyConfig: conf,
|
||||
StrategyConfig: strategyConf,
|
||||
SessionsConfig: sessionsConf,
|
||||
}
|
||||
|
||||
if conf.Dynamic.CustomThemesPath != "" {
|
||||
customThemesFs := osDirFS(conf.Dynamic.CustomThemesPath)
|
||||
if strategyConf.Dynamic.CustomThemesPath != "" {
|
||||
customThemesFs := osDirFS(strategyConf.Dynamic.CustomThemesPath)
|
||||
serveStrategy.customThemesFS = customThemesFs
|
||||
serveStrategy.customThemes = listThemes(customThemesFs)
|
||||
}
|
||||
@@ -51,6 +53,7 @@ func (s *ServeStrategy) ServeDynamic(c *gin.Context) {
|
||||
Theme: s.StrategyConfig.Dynamic.DefaultTheme,
|
||||
ShowDetails: s.StrategyConfig.Dynamic.ShowDetailsByDefault,
|
||||
RefreshFrequency: s.StrategyConfig.Dynamic.DefaultRefreshFrequency,
|
||||
SessionDuration: s.SessionsConfig.DefaultDuration,
|
||||
}
|
||||
|
||||
if err := c.ShouldBind(&request); err != nil {
|
||||
|
||||
@@ -264,7 +264,8 @@ func createMap(instances []*instance.State) (store *sync.Map) {
|
||||
func TestNewServeStrategy(t *testing.T) {
|
||||
type args struct {
|
||||
sessionsManager sessions.Manager
|
||||
conf config.Strategy
|
||||
strategyConf config.Strategy
|
||||
sessionsConf config.Sessions
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -276,7 +277,7 @@ func TestNewServeStrategy(t *testing.T) {
|
||||
name: "load custom themes",
|
||||
args: args{
|
||||
sessionsManager: &SessionsManagerMock{},
|
||||
conf: config.Strategy{
|
||||
strategyConf: config.Strategy{
|
||||
Dynamic: config.DynamicStrategy{
|
||||
CustomThemesPath: "my/path/to/themes",
|
||||
},
|
||||
@@ -295,7 +296,7 @@ func TestNewServeStrategy(t *testing.T) {
|
||||
name: "load custom themes recursively",
|
||||
args: args{
|
||||
sessionsManager: &SessionsManagerMock{},
|
||||
conf: config.Strategy{
|
||||
strategyConf: config.Strategy{
|
||||
Dynamic: config.DynamicStrategy{
|
||||
CustomThemesPath: "my/path/to/themes",
|
||||
},
|
||||
@@ -316,7 +317,7 @@ func TestNewServeStrategy(t *testing.T) {
|
||||
name: "do not load custom themes outside of path",
|
||||
args: args{
|
||||
sessionsManager: &SessionsManagerMock{},
|
||||
conf: config.Strategy{
|
||||
strategyConf: config.Strategy{
|
||||
Dynamic: config.DynamicStrategy{
|
||||
CustomThemesPath: "my/path/to/themes",
|
||||
},
|
||||
@@ -353,7 +354,7 @@ func TestNewServeStrategy(t *testing.T) {
|
||||
|
||||
osDirFS = myOsDirFS
|
||||
|
||||
if got := NewServeStrategy(tt.args.sessionsManager, tt.args.conf); !reflect.DeepEqual(got.customThemes, tt.want) {
|
||||
if got := NewServeStrategy(tt.args.sessionsManager, tt.args.strategyConf, tt.args.sessionsConf); !reflect.DeepEqual(got.customThemes, tt.want) {
|
||||
t.Errorf("NewServeStrategy() = %v, want %v", got.customThemes, tt.want)
|
||||
}
|
||||
})
|
||||
@@ -363,6 +364,7 @@ func TestNewServeStrategy(t *testing.T) {
|
||||
func TestServeStrategy_ServeDynamicThemes(t *testing.T) {
|
||||
type fields struct {
|
||||
StrategyConfig config.Strategy
|
||||
SessionsConfig config.Sessions
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -428,7 +430,7 @@ func TestServeStrategy_ServeDynamicThemes(t *testing.T) {
|
||||
|
||||
osDirFS = myOsDirFS
|
||||
|
||||
s := NewServeStrategy(nil, tt.fields.StrategyConfig)
|
||||
s := NewServeStrategy(nil, tt.fields.StrategyConfig, tt.fields.SessionsConfig)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
c := GetTestGinContext(recorder)
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Start(serverConf config.Server, strategyConf config.Strategy, sessionManager sessions.Manager) {
|
||||
func Start(serverConf config.Server, strategyConf config.Strategy, sessionsConf config.Sessions, sessionManager sessions.Manager) {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
@@ -29,7 +29,7 @@ func Start(serverConf config.Server, strategyConf config.Strategy, sessionManage
|
||||
{
|
||||
api := base.Group("/api")
|
||||
{
|
||||
strategy := routes.NewServeStrategy(sessionManager, strategyConf)
|
||||
strategy := routes.NewServeStrategy(sessionManager, strategyConf, sessionsConf)
|
||||
api.GET("/strategies/dynamic", strategy.ServeDynamic)
|
||||
api.GET("/strategies/dynamic/themes", strategy.ServeDynamicThemes)
|
||||
api.GET("/strategies/blocking", strategy.ServeBlocking)
|
||||
|
||||
@@ -47,7 +47,7 @@ func Start(conf config.Config) error {
|
||||
loadSessions(storage, sessionsManager)
|
||||
}
|
||||
|
||||
http.Start(conf.Server, conf.Strategy, sessionsManager)
|
||||
http.Start(conf.Server, conf.Strategy, conf.Sessions, sessionsManager)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ func (c *Config) buildDynamicRequest(middlewareName string) (*http.Request, erro
|
||||
|
||||
q := request.URL.Query()
|
||||
|
||||
if c.SessionDuration != "" {
|
||||
_, err = time.ParseDuration(c.SessionDuration)
|
||||
|
||||
if err != nil {
|
||||
@@ -87,6 +88,8 @@ func (c *Config) buildDynamicRequest(middlewareName string) (*http.Request, erro
|
||||
}
|
||||
|
||||
q.Add("session_duration", c.SessionDuration)
|
||||
}
|
||||
|
||||
for _, name := range c.splittedNames {
|
||||
q.Add("names", name)
|
||||
}
|
||||
@@ -133,7 +136,16 @@ func (c *Config) buildBlockingRequest() (*http.Request, error) {
|
||||
|
||||
q := request.URL.Query()
|
||||
|
||||
if c.SessionDuration != "" {
|
||||
_, 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)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,16 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
want *http.Request
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "dynamic session with required values",
|
||||
fields: fields{
|
||||
SablierURL: "http://sablier:10000",
|
||||
Names: "nginx , apache",
|
||||
Dynamic: &traefik.DynamicConfiguration{},
|
||||
},
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/dynamic?display_name=sablier-middleware&names=nginx&names=apache", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "dynamic session with default values",
|
||||
fields: fields{
|
||||
@@ -143,6 +153,16 @@ func TestConfig_BuildRequest(t *testing.T) {
|
||||
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: "blocking session with required values",
|
||||
fields: fields{
|
||||
SablierURL: "http://sablier:10000",
|
||||
Names: "nginx , apache",
|
||||
Blocking: &traefik.BlockingConfiguration{},
|
||||
},
|
||||
want: createRequest("GET", "http://sablier:10000/api/strategies/blocking?names=nginx&names=apache", nil),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "blocking session with default values",
|
||||
fields: fields{
|
||||
|
||||
Reference in New Issue
Block a user