mirror of https://github.com/synctv-org/synctv
Feat: settings
parent
d3abf4bc45
commit
aa6fa5411f
@ -1,6 +1,16 @@
|
||||
package model
|
||||
|
||||
type SettingItem struct {
|
||||
type SettingType string
|
||||
|
||||
const (
|
||||
SettingTypeBool SettingType = "bool"
|
||||
SettingTypeInt64 SettingType = "int64"
|
||||
SettingTypeFloat64 SettingType = "float64"
|
||||
SettingTypeString SettingType = "string"
|
||||
)
|
||||
|
||||
type Setting struct {
|
||||
Name string `gorm:"primaryKey"`
|
||||
Value string
|
||||
Type SettingType `gorm:"not null;default:string"`
|
||||
}
|
||||
|
@ -0,0 +1,92 @@
|
||||
package op
|
||||
|
||||
import (
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
"github.com/synctv-org/synctv/internal/model"
|
||||
)
|
||||
|
||||
var boolSettings map[string]*Bool
|
||||
|
||||
type Setting interface {
|
||||
Name() string
|
||||
Raw() string
|
||||
Type() model.SettingType
|
||||
}
|
||||
|
||||
type BoolSetting interface {
|
||||
Setting
|
||||
Set(value bool) error
|
||||
Get() (bool, error)
|
||||
}
|
||||
|
||||
type Bool struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
|
||||
func NewBool(name, value string) *Bool {
|
||||
return &Bool{
|
||||
name: name,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bool) Name() string {
|
||||
return b.name
|
||||
}
|
||||
|
||||
func (b *Bool) Set(value bool) error {
|
||||
if value {
|
||||
b.value = "1"
|
||||
} else {
|
||||
b.value = "0"
|
||||
}
|
||||
return db.SetSettingItemValue(b.name, b.value)
|
||||
}
|
||||
|
||||
func (b *Bool) Get() (bool, error) {
|
||||
return b.value == "1", nil
|
||||
}
|
||||
|
||||
func (b *Bool) Raw() string {
|
||||
return b.value
|
||||
}
|
||||
|
||||
func (b *Bool) Type() model.SettingType {
|
||||
return model.SettingTypeBool
|
||||
}
|
||||
|
||||
type Int64Setting interface {
|
||||
Set(value int64) error
|
||||
Get() (int64, error)
|
||||
Raw() string
|
||||
}
|
||||
|
||||
type Float64Setting interface {
|
||||
Set(value float64) error
|
||||
Get() (float64, error)
|
||||
Raw() string
|
||||
}
|
||||
|
||||
type StringSetting interface {
|
||||
Set(value string) error
|
||||
Get() (string, error)
|
||||
Raw() string
|
||||
}
|
||||
|
||||
func cleanReg() {
|
||||
boolSettings = nil
|
||||
}
|
||||
|
||||
func newRegBoolSetting(k, v string) BoolSetting {
|
||||
b := NewBool(k, v)
|
||||
if boolSettings == nil {
|
||||
boolSettings = make(map[string]*Bool)
|
||||
}
|
||||
boolSettings[k] = b
|
||||
return b
|
||||
}
|
||||
|
||||
var (
|
||||
DisableCreateRoom = newRegBoolSetting("disable_create_room", "0")
|
||||
)
|
@ -1,20 +1,21 @@
|
||||
package provider
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/synctv-org/synctv/internal/provider"
|
||||
providerpb "github.com/synctv-org/synctv/proto/provider"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type GRPCServer struct {
|
||||
providerpb.UnimplementedOauth2PluginServer
|
||||
Impl ProviderInterface
|
||||
Impl provider.ProviderInterface
|
||||
}
|
||||
|
||||
func (s *GRPCServer) Init(ctx context.Context, req *providerpb.InitReq) (*providerpb.Enpty, error) {
|
||||
s.Impl.Init(Oauth2Option{
|
||||
s.Impl.Init(provider.Oauth2Option{
|
||||
ClientID: req.ClientId,
|
||||
ClientSecret: req.ClientSecret,
|
||||
RedirectURL: req.RedirectUrl,
|
@ -0,0 +1,53 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/synctv-org/synctv/internal/provider"
|
||||
)
|
||||
|
||||
var (
|
||||
enabledProviders map[provider.OAuth2Provider]provider.ProviderInterface
|
||||
allowedProviders = make(map[provider.OAuth2Provider]provider.ProviderInterface)
|
||||
)
|
||||
|
||||
func InitProvider(p provider.OAuth2Provider, c provider.Oauth2Option) error {
|
||||
pi, ok := allowedProviders[p]
|
||||
if !ok {
|
||||
return FormatErrNotImplemented(p)
|
||||
}
|
||||
pi.Init(c)
|
||||
if enabledProviders == nil {
|
||||
enabledProviders = make(map[provider.OAuth2Provider]provider.ProviderInterface)
|
||||
}
|
||||
enabledProviders[pi.Provider()] = pi
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterProvider(ps ...provider.ProviderInterface) {
|
||||
for _, p := range ps {
|
||||
allowedProviders[p.Provider()] = p
|
||||
}
|
||||
}
|
||||
|
||||
func GetProvider(p provider.OAuth2Provider) (provider.ProviderInterface, error) {
|
||||
pi, ok := enabledProviders[p]
|
||||
if !ok {
|
||||
return nil, FormatErrNotImplemented(p)
|
||||
}
|
||||
return pi, nil
|
||||
}
|
||||
|
||||
func AllowedProvider() map[provider.OAuth2Provider]provider.ProviderInterface {
|
||||
return allowedProviders
|
||||
}
|
||||
|
||||
func EnabledProvider() map[provider.OAuth2Provider]provider.ProviderInterface {
|
||||
return enabledProviders
|
||||
}
|
||||
|
||||
type FormatErrNotImplemented string
|
||||
|
||||
func (f FormatErrNotImplemented) Error() string {
|
||||
return fmt.Sprintf("%s not implemented", string(f))
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func AdminSettings(ctx *gin.Context) {
|
||||
|
||||
}
|
Loading…
Reference in New Issue