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