mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 13:23:03 +01:00
feat(config): add strategy config
This commit is contained in:
@@ -5,8 +5,9 @@ import (
|
||||
)
|
||||
|
||||
type DynamicRequest struct {
|
||||
Names []string `form:"names" binding:"required"`
|
||||
DisplayName string `form:"display_name"`
|
||||
Theme string `form:"theme"`
|
||||
SessionDuration time.Duration `form:"session_duration" binding:"required"`
|
||||
Names []string `form:"names" binding:"required"`
|
||||
DisplayName string `form:"display_name"`
|
||||
Theme string `form:"theme"`
|
||||
SessionDuration time.Duration `form:"session_duration" binding:"required"`
|
||||
RefreshFrequency time.Duration `form:"refresh_frequency"`
|
||||
}
|
||||
|
||||
@@ -13,19 +13,22 @@ import (
|
||||
"github.com/acouvreur/sablier/app/http/routes/models"
|
||||
"github.com/acouvreur/sablier/app/instance"
|
||||
"github.com/acouvreur/sablier/app/sessions"
|
||||
"github.com/acouvreur/sablier/config"
|
||||
"github.com/acouvreur/sablier/version"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ServeStrategy struct {
|
||||
SessionsManager sessions.Manager
|
||||
StrategyConfig config.Strategy
|
||||
}
|
||||
|
||||
// ServeDynamic returns a waiting page displaying the session request if the session is not ready
|
||||
// If the session is ready, returns a redirect 307 with an arbitrary location
|
||||
func (s *ServeStrategy) ServeDynamic(c *gin.Context) {
|
||||
request := models.DynamicRequest{
|
||||
Theme: "hacker-terminal",
|
||||
Theme: s.StrategyConfig.Dynamic.DefaultTheme,
|
||||
RefreshFrequency: s.StrategyConfig.Dynamic.DefaultRefreshFrequency,
|
||||
}
|
||||
|
||||
if err := c.ShouldBind(&request); err != nil {
|
||||
@@ -59,7 +62,9 @@ func (s *ServeStrategy) ServeDynamic(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *ServeStrategy) ServeBlocking(c *gin.Context) {
|
||||
request := models.BlockingRequest{}
|
||||
request := models.BlockingRequest{
|
||||
Timeout: s.StrategyConfig.Blocking.DefaultTimeout,
|
||||
}
|
||||
|
||||
if err := c.ShouldBind(&request); err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/acouvreur/sablier/app/http/routes/models"
|
||||
"github.com/acouvreur/sablier/app/instance"
|
||||
"github.com/acouvreur/sablier/app/sessions"
|
||||
"github.com/acouvreur/sablier/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
@@ -92,6 +93,7 @@ func TestServeStrategy_ServeDynamic(t *testing.T) {
|
||||
SessionsManager: &SessionsManagerMock{
|
||||
SessionState: tt.arg.session,
|
||||
},
|
||||
StrategyConfig: config.NewStrategyConfig(),
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
c := GetTestGinContext(recorder)
|
||||
|
||||
@@ -12,16 +12,16 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Start(conf config.Server, sessionManager sessions.Manager) error {
|
||||
func Start(serverConf config.Server, strategyConf config.Strategy, sessionManager sessions.Manager) error {
|
||||
r := gin.New()
|
||||
|
||||
r.Use(middleware.Logger(log.New()), gin.Recovery())
|
||||
|
||||
base := r.Group(conf.BasePath)
|
||||
base := r.Group(serverConf.BasePath)
|
||||
{
|
||||
api := base.Group("/api")
|
||||
{
|
||||
strategy := routes.ServeStrategy{SessionsManager: sessionManager}
|
||||
strategy := routes.ServeStrategy{SessionsManager: sessionManager, StrategyConfig: strategyConf}
|
||||
api.GET("/strategies/dynamic", strategy.ServeDynamic)
|
||||
api.GET("/strategies/blocking", strategy.ServeBlocking)
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func Start(conf config.Server, sessionManager sessions.Manager) error {
|
||||
|
||||
logRoutes(r.Routes())
|
||||
|
||||
return r.Run(fmt.Sprintf(":%d", conf.Port))
|
||||
return r.Run(fmt.Sprintf(":%d", serverConf.Port))
|
||||
}
|
||||
|
||||
func logRoutes(routes gin.RoutesInfo) {
|
||||
|
||||
@@ -46,7 +46,7 @@ func Start(conf config.Config) error {
|
||||
loadSessions(storage, sessionsManager)
|
||||
}
|
||||
|
||||
err = http.Start(conf.Server, sessionsManager)
|
||||
err = http.Start(conf.Server, conf.Strategy, sessionsManager)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -61,6 +61,14 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringVar(&conf.Logging.Level, "logging.level", log.InfoLevel.String(), "The logging level. Can be one of [panic, fatal, error, warn, info, debug, trace]")
|
||||
viper.BindPFlag("logging.level", rootCmd.PersistentFlags().Lookup("logging.level"))
|
||||
|
||||
// strategy
|
||||
startCmd.Flags().StringVar(&conf.Strategy.Dynamic.DefaultTheme, "strategy.dynamic.default-theme", "hacker-terminal", "Default theme used for dynamic strategy")
|
||||
viper.BindPFlag("strategy.dynamic.default-theme", startCmd.Flags().Lookup("strategy.dynamic.default-theme"))
|
||||
startCmd.Flags().DurationVar(&conf.Strategy.Dynamic.DefaultRefreshFrequency, "strategy.dynamic.default-refresh-frequency", 5*time.Second, "Default refresh frequency in the HTML page for dynamic strategy")
|
||||
viper.BindPFlag("strategy.dynamic.default-refresh-frequency", startCmd.Flags().Lookup("strategy.dynamic.default-refresh-frequency"))
|
||||
startCmd.Flags().DurationVar(&conf.Strategy.Blocking.DefaultTimeout, "strategy.blocking.default-timeout", 1*time.Minute, "Default timeout used for blocking strategy")
|
||||
viper.BindPFlag("strategy.blocking.default-timeout", startCmd.Flags().Lookup("strategy.blocking.default-timeout"))
|
||||
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ type Config struct {
|
||||
Provider Provider
|
||||
Sessions Sessions
|
||||
Logging Logging
|
||||
Strategy Strategy
|
||||
}
|
||||
|
||||
func NewConfig() Config {
|
||||
@@ -14,6 +15,7 @@ func NewConfig() Config {
|
||||
Storage: NewStorageConfig(),
|
||||
Provider: NewProviderConfig(),
|
||||
Sessions: NewSessionsConfig(),
|
||||
Logging: NewLoggingLevel(),
|
||||
Logging: NewLoggingConfig(),
|
||||
Strategy: NewStrategyConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
37
config/strategy.go
Normal file
37
config/strategy.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type DynamicStrategy struct {
|
||||
DefaultTheme string `mapstructure:"DEFAULTTHEME" yaml:"defaultTheme" default:"hacker-terminal"`
|
||||
DefaultRefreshFrequency time.Duration `mapstructure:"DEFAULTREFRESHFREQUENCY" yaml:"defaultRefreshFrequency" default:"5s"`
|
||||
}
|
||||
|
||||
type BlockingStrategy struct {
|
||||
DefaultTimeout time.Duration `mapstructure:"DEFAULTTIMEOUT" yaml:"defaultTimeout" default:"1m"`
|
||||
}
|
||||
|
||||
type Strategy struct {
|
||||
Dynamic DynamicStrategy
|
||||
Blocking BlockingStrategy
|
||||
}
|
||||
|
||||
func NewStrategyConfig() Strategy {
|
||||
return Strategy{
|
||||
Dynamic: newDynamicStrategy(),
|
||||
Blocking: newBlockingStrategy(),
|
||||
}
|
||||
}
|
||||
|
||||
func newDynamicStrategy() DynamicStrategy {
|
||||
return DynamicStrategy{
|
||||
DefaultTheme: "hacker-terminal",
|
||||
DefaultRefreshFrequency: 5 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
func newBlockingStrategy() BlockingStrategy {
|
||||
return BlockingStrategy{
|
||||
DefaultTimeout: 1 * time.Minute,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user