From 30e0f2def5408bb378c810ebdac2603848dc9066 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Sat, 16 Nov 2024 17:04:11 +0800 Subject: [PATCH] fix: host setting --- internal/bootstrap/provider.go | 6 ++++++ internal/settings/bool.go | 26 +++++++++++++++++++------- internal/settings/floate64.go | 18 +++++++++++++++++- internal/settings/int64.go | 18 +++++++++++++++++- internal/settings/string.go | 18 +++++++++++++++++- internal/settings/var.go | 18 ++++++++++++++++++ server/handlers/init.go | 22 ---------------------- server/handlers/movie.go | 2 +- server/handlers/user.go | 2 +- 9 files changed, 96 insertions(+), 34 deletions(-) diff --git a/internal/bootstrap/provider.go b/internal/bootstrap/provider.go index 05d5e32d..2c2ddbf5 100644 --- a/internal/bootstrap/provider.go +++ b/internal/bootstrap/provider.go @@ -166,6 +166,12 @@ func InitProviderSetting(pi provider.Provider) { pi.Init(opt) return s, nil }), + settings.WithAfterGetString(func(ss settings.StringSetting, s string) string { + if s == "" && settings.HOST.Get() != "" { + return fmt.Sprintf("%s/web/oauth2/callback/%s", settings.HOST.Get(), pi.Provider()) + } + return s + }), settings.WithInitPriorityString(1), settings.WithBeforeSetString(func(ss settings.StringSetting, s string) (string, error) { opt.RedirectURL = s diff --git a/internal/settings/bool.go b/internal/settings/bool.go index 82919484..50092b67 100644 --- a/internal/settings/bool.go +++ b/internal/settings/bool.go @@ -18,6 +18,7 @@ type BoolSetting interface { Stringify(bool) string SetBeforeInit(func(BoolSetting, bool) (bool, error)) SetBeforeSet(func(BoolSetting, bool) (bool, error)) + SetAfterGet(func(BoolSetting, bool) bool) } var _ BoolSetting = (*Bool)(nil) @@ -27,8 +28,9 @@ type Bool struct { beforeSet func(BoolSetting, bool) (bool, error) afterInit func(BoolSetting, bool) afterSet func(BoolSetting, bool) + afterGet func(BoolSetting, bool) bool setting - value uint32 + value atomic.Bool defaultValue bool } @@ -64,6 +66,12 @@ func WithAfterSetBool(afterSet func(BoolSetting, bool)) BoolSettingOption { } } +func WithAfterGetBool(afterGet func(BoolSetting, bool) bool) BoolSettingOption { + return func(s *Bool) { + s.SetAfterGet(afterGet) + } +} + func newBool(name string, value bool, group model.SettingGroup, options ...BoolSettingOption) *Bool { b := &Bool{ setting: setting{ @@ -96,16 +104,20 @@ func (b *Bool) SetAfterSet(afterSet func(BoolSetting, bool)) { b.afterSet = afterSet } +func (b *Bool) SetAfterGet(afterGet func(BoolSetting, bool) bool) { + b.afterGet = afterGet +} + func (b *Bool) set(value bool) { - if value { - atomic.StoreUint32(&b.value, 1) - } else { - atomic.StoreUint32(&b.value, 0) - } + b.value.Store(value) } func (b *Bool) Get() bool { - return atomic.LoadUint32(&b.value) == 1 + v := b.value.Load() + if b.afterGet != nil { + v = b.afterGet(b, v) + } + return v } func (b *Bool) Init(value string) error { diff --git a/internal/settings/floate64.go b/internal/settings/floate64.go index 33bea923..6e0b68b3 100644 --- a/internal/settings/floate64.go +++ b/internal/settings/floate64.go @@ -19,6 +19,7 @@ type Float64Setting interface { Stringify(float64) string SetBeforeInit(func(Float64Setting, float64) (float64, error)) SetBeforeSet(func(Float64Setting, float64) (float64, error)) + SetAfterGet(func(Float64Setting, float64) float64) } var _ Float64Setting = (*Float64)(nil) @@ -29,6 +30,7 @@ type Float64 struct { beforeSet func(Float64Setting, float64) (float64, error) afterInit func(Float64Setting, float64) afterSet func(Float64Setting, float64) + afterGet func(Float64Setting, float64) float64 setting value uint64 defaultValue float64 @@ -72,6 +74,12 @@ func WithAfterSetFloat64(afterSet func(Float64Setting, float64)) Float64SettingO } } +func WithAfterGetFloat64(afterGet func(Float64Setting, float64) float64) Float64SettingOption { + return func(s *Float64) { + s.SetAfterGet(afterGet) + } +} + func newFloat64(name string, value float64, group model.SettingGroup, options ...Float64SettingOption) *Float64 { f := &Float64{ setting: setting{ @@ -104,6 +112,10 @@ func (f *Float64) SetAfterSet(afterSet func(Float64Setting, float64)) { f.afterSet = afterSet } +func (f *Float64) SetAfterGet(afterGet func(Float64Setting, float64) float64) { + f.afterGet = afterGet +} + func (f *Float64) Parse(value string) (float64, error) { v, err := strconv.ParseFloat(value, 64) if err != nil { @@ -224,7 +236,11 @@ func (f *Float64) Set(v float64) (err error) { } func (f *Float64) Get() float64 { - return math.Float64frombits(atomic.LoadUint64(&f.value)) + v := math.Float64frombits(atomic.LoadUint64(&f.value)) + if f.afterGet != nil { + v = f.afterGet(f, v) + } + return v } func (f *Float64) Interface() any { diff --git a/internal/settings/int64.go b/internal/settings/int64.go index 91e5f0cd..efc0d8f2 100644 --- a/internal/settings/int64.go +++ b/internal/settings/int64.go @@ -18,6 +18,7 @@ type Int64Setting interface { Stringify(int64) string SetBeforeInit(func(Int64Setting, int64) (int64, error)) SetBeforeSet(func(Int64Setting, int64) (int64, error)) + SetAfterGet(func(Int64Setting, int64) int64) } var _ Int64Setting = (*Int64)(nil) @@ -28,6 +29,7 @@ type Int64 struct { beforeSet func(Int64Setting, int64) (int64, error) afterInit func(Int64Setting, int64) afterSet func(Int64Setting, int64) + afterGet func(Int64Setting, int64) int64 setting value int64 defaultValue int64 @@ -71,6 +73,12 @@ func WithAfterSetInt64(afterSet func(Int64Setting, int64)) Int64SettingOption { } } +func WithAfterGetInt64(afterGet func(Int64Setting, int64) int64) Int64SettingOption { + return func(s *Int64) { + s.SetAfterGet(afterGet) + } +} + func newInt64(name string, value int64, group model.SettingGroup, options ...Int64SettingOption) *Int64 { i := &Int64{ setting: setting{ @@ -103,6 +111,10 @@ func (i *Int64) SetAfterSet(afterSet func(Int64Setting, int64)) { i.afterSet = afterSet } +func (i *Int64) SetAfterGet(afterGet func(Int64Setting, int64) int64) { + i.afterGet = afterGet +} + func (i *Int64) Parse(value string) (int64, error) { v, err := strconv.ParseInt(value, 10, 64) if err != nil { @@ -223,7 +235,11 @@ func (i *Int64) Set(v int64) (err error) { } func (i *Int64) Get() int64 { - return atomic.LoadInt64(&i.value) + v := atomic.LoadInt64(&i.value) + if i.afterGet != nil { + v = i.afterGet(i, v) + } + return v } func (i *Int64) Interface() any { diff --git a/internal/settings/string.go b/internal/settings/string.go index 84adb52a..9f81c70a 100644 --- a/internal/settings/string.go +++ b/internal/settings/string.go @@ -17,6 +17,7 @@ type StringSetting interface { Stringify(string) string SetBeforeInit(func(StringSetting, string) (string, error)) SetBeforeSet(func(StringSetting, string) (string, error)) + SetAfterGet(func(StringSetting, string) string) } var _ StringSetting = (*String)(nil) @@ -27,6 +28,7 @@ type String struct { beforeSet func(StringSetting, string) (string, error) afterInit func(StringSetting, string) afterSet func(StringSetting, string) + afterGet func(StringSetting, string) string defaultValue string value string setting @@ -71,6 +73,12 @@ func WithAfterSetString(afterSet func(StringSetting, string)) StringSettingOptio } } +func WithAfterGetString(afterGet func(StringSetting, string) string) StringSettingOption { + return func(s *String) { + s.SetAfterGet(afterGet) + } +} + func newString(name string, value string, group model.SettingGroup, options ...StringSettingOption) *String { s := &String{ setting: setting{ @@ -103,6 +111,10 @@ func (s *String) SetAfterSet(afterSet func(StringSetting, string)) { s.afterSet = afterSet } +func (s *String) SetAfterGet(afterGet func(StringSetting, string) string) { + s.afterGet = afterGet +} + func (s *String) Parse(value string) (string, error) { if s.validator != nil { return value, s.validator(value) @@ -223,7 +235,11 @@ func (s *String) Set(v string) (err error) { func (s *String) Get() string { s.lock.RLock() defer s.lock.RUnlock() - return s.value + v := s.value + if s.afterGet != nil { + v = s.afterGet(s, v) + } + return v } func (s *String) Interface() any { diff --git a/internal/settings/var.go b/internal/settings/var.go index 53395acd..2da33558 100644 --- a/internal/settings/var.go +++ b/internal/settings/var.go @@ -2,6 +2,8 @@ package settings import ( "errors" + "net/url" + "strings" "github.com/synctv-org/synctv/internal/db" "github.com/synctv-org/synctv/internal/model" @@ -74,3 +76,19 @@ var ( var DatabaseVersion = NewStringSetting("database_version", db.CurrentVersion, model.SettingGroupDatabase, WithBeforeSetString(func(ss StringSetting, s string) (string, error) { return "", errors.New("not support change database version") })) + +var HOST = NewStringSetting( + "host", + "", + model.SettingGroupServer, + WithValidatorString(func(s string) error { + if s == "" { + return nil + } + if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") { + return errors.New("host must start with http:// or https://") + } + _, err := url.Parse(s) + return err + }), +) diff --git a/server/handlers/init.go b/server/handlers/init.go index d33c19ee..f7c547cf 100644 --- a/server/handlers/init.go +++ b/server/handlers/init.go @@ -1,13 +1,7 @@ package handlers import ( - "errors" - "net/url" - "strings" - "github.com/gin-gonic/gin" - "github.com/synctv-org/synctv/internal/model" - "github.com/synctv-org/synctv/internal/settings" "github.com/synctv-org/synctv/server/handlers/vendors" "github.com/synctv-org/synctv/server/handlers/vendors/vendoralist" "github.com/synctv-org/synctv/server/handlers/vendors/vendorbilibili" @@ -16,22 +10,6 @@ import ( "github.com/synctv-org/synctv/utils" ) -var HOST = settings.NewStringSetting( - "host", - "", - model.SettingGroupServer, - settings.WithValidatorString(func(s string) error { - if s == "" { - return nil - } - if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") { - return errors.New("host must start with http:// or https://") - } - _, err := url.Parse(s) - return err - }), -) - func Init(e *gin.Engine) { api := e.Group("/api") diff --git a/server/handlers/movie.go b/server/handlers/movie.go index 89e626f2..03c35c7f 100644 --- a/server/handlers/movie.go +++ b/server/handlers/movie.go @@ -405,7 +405,7 @@ func NewPublishKey(ctx *gin.Context) { host := settings.CustomPublishHost.Get() if host == "" { - host = HOST.Get() + host = settings.HOST.Get() } if host == "" { host = ctx.Request.Host diff --git a/server/handlers/user.go b/server/handlers/user.go index e0ee9151..c576f9cd 100644 --- a/server/handlers/user.go +++ b/server/handlers/user.go @@ -672,7 +672,7 @@ func SendUserRetrievePasswordEmailCaptcha(ctx *gin.Context) { return } - host := HOST.Get() + host := settings.HOST.Get() if host == "" { host = (&url.URL{ Scheme: "http",