fix: host setting

pull/268/head v0.7.11
zijiren233 2 years ago
parent b82a1544d3
commit 30e0f2def5

@ -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

@ -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 {

@ -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 {

@ -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 {

@ -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 {

@ -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
}),
)

@ -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")

@ -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

@ -672,7 +672,7 @@ func SendUserRetrievePasswordEmailCaptcha(ctx *gin.Context) {
return
}
host := HOST.Get()
host := settings.HOST.Get()
if host == "" {
host = (&url.URL{
Scheme: "http",

Loading…
Cancel
Save