107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package config
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type contextKey struct{}
|
|
|
|
type Config struct {
|
|
BasePath string
|
|
AppTitle string
|
|
Theme string
|
|
HTMX HTMXConfig
|
|
SSE SSEConfig
|
|
Preload bool
|
|
Morphing bool
|
|
}
|
|
|
|
type HTMXConfig struct {
|
|
Transitions bool
|
|
ImplicitInheritance bool
|
|
SelfRequestsOnly bool
|
|
Timeout int
|
|
NoSwap []string
|
|
}
|
|
|
|
type SSEConfig struct {
|
|
Enabled bool
|
|
Reconnect bool
|
|
ReconnectDelay int
|
|
ReconnectMaxDelay int
|
|
ReconnectMaxAttempts int
|
|
ReconnectJitter float64
|
|
PauseInBackground bool
|
|
}
|
|
|
|
func Default() Config {
|
|
return Config{
|
|
BasePath: "/",
|
|
AppTitle: "Sonr Wallet",
|
|
Theme: "dark",
|
|
HTMX: HTMXConfig{
|
|
Transitions: true,
|
|
ImplicitInheritance: false,
|
|
SelfRequestsOnly: true,
|
|
Timeout: 0,
|
|
NoSwap: []string{"204", "304"},
|
|
},
|
|
SSE: SSEConfig{
|
|
Enabled: true,
|
|
Reconnect: true,
|
|
ReconnectDelay: 500,
|
|
ReconnectMaxDelay: 60000,
|
|
ReconnectMaxAttempts: 10,
|
|
ReconnectJitter: 0.3,
|
|
PauseInBackground: false,
|
|
},
|
|
Preload: true,
|
|
Morphing: true,
|
|
}
|
|
}
|
|
|
|
func WithContext(ctx context.Context, cfg *Config) context.Context {
|
|
return context.WithValue(ctx, contextKey{}, cfg)
|
|
}
|
|
|
|
func FromContext(ctx context.Context) *Config {
|
|
if cfg, ok := ctx.Value(contextKey{}).(*Config); ok {
|
|
return cfg
|
|
}
|
|
defaultCfg := Default()
|
|
return &defaultCfg
|
|
}
|
|
|
|
func (c *Config) HTMXConfigJSON() string {
|
|
cfg := map[string]any{
|
|
"transitions": c.HTMX.Transitions,
|
|
"implicitInheritance": c.HTMX.ImplicitInheritance,
|
|
"selfRequestsOnly": c.HTMX.SelfRequestsOnly,
|
|
"timeout": c.HTMX.Timeout,
|
|
"noSwap": c.HTMX.NoSwap,
|
|
}
|
|
|
|
if c.SSE.Enabled {
|
|
cfg["streams"] = map[string]any{
|
|
"reconnect": c.SSE.Reconnect,
|
|
"reconnectDelay": c.SSE.ReconnectDelay,
|
|
"reconnectMaxDelay": c.SSE.ReconnectMaxDelay,
|
|
"reconnectMaxAttempts": c.SSE.ReconnectMaxAttempts,
|
|
"reconnectJitter": c.SSE.ReconnectJitter,
|
|
"pauseInBackground": c.SSE.PauseInBackground,
|
|
}
|
|
}
|
|
|
|
data, err := json.Marshal(cfg)
|
|
if err != nil {
|
|
return "{}"
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func (c *Config) ThemeClass() string {
|
|
return fmt.Sprintf("wa-theme-%s", c.Theme)
|
|
}
|