2021-09-29 23:57:09 +00:00
|
|
|
package properties
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-11-30 02:23:49 +00:00
|
|
|
"fmt"
|
2021-09-29 23:57:09 +00:00
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"path"
|
2021-11-11 03:00:38 +00:00
|
|
|
"pikapika/main/utils"
|
2021-09-29 23:57:09 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mutex = sync.Mutex{}
|
|
|
|
var db *gorm.DB
|
|
|
|
|
|
|
|
func InitDBConnect(databaseDir string) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
var err error
|
2021-10-22 02:24:39 +00:00
|
|
|
db, err = gorm.Open(sqlite.Open(path.Join(databaseDir, "properties.db")), utils.GormConfig)
|
2021-09-29 23:57:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("failed to connect database")
|
|
|
|
}
|
|
|
|
db.AutoMigrate(&Property{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Property struct {
|
|
|
|
gorm.Model
|
|
|
|
K string `gorm:"index:uk_k,unique"`
|
|
|
|
V string
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadProperty(name string, defaultValue string) (string, error) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
var property Property
|
|
|
|
err := db.First(&property, "k", name).Error
|
|
|
|
if err == nil {
|
|
|
|
return property.V, nil
|
|
|
|
}
|
|
|
|
if gorm.ErrRecordNotFound == err {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
panic(errors.New("?"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveProperty(name string, value string) error {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
return db.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "k"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"created_at", "updated_at", "v"}),
|
|
|
|
}).Create(&Property{
|
|
|
|
K: name,
|
|
|
|
V: value,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadBoolProperty(name string, defaultValue bool) (bool, error) {
|
|
|
|
stringValue, err := LoadProperty(name, strconv.FormatBool(defaultValue))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strconv.ParseBool(stringValue)
|
|
|
|
}
|
|
|
|
|
2021-11-30 02:23:49 +00:00
|
|
|
func LoadIntProperty(name string, defaultValue int) (int, error) {
|
|
|
|
str, err := LoadProperty(name, fmt.Sprintf("%d", defaultValue))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.Atoi(str)
|
2021-09-29 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 02:23:49 +00:00
|
|
|
func SaveIntProperty(name string, value int) error {
|
|
|
|
return SaveProperty(name, strconv.Itoa(value))
|
2021-09-29 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 02:23:49 +00:00
|
|
|
func SaveBoolProperty(name string, value bool) error {
|
|
|
|
return SaveProperty(name, strconv.FormatBool(value))
|
2021-09-29 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SaveProxy(value string) error {
|
|
|
|
return SaveProperty("proxy", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadProxy() (string, error) {
|
|
|
|
return LoadProperty("proxy", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveUsername(value string) error {
|
|
|
|
return SaveProperty("username", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadUsername() (string, error) {
|
|
|
|
return LoadProperty("username", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SavePassword(value string) error {
|
|
|
|
return SaveProperty("password", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadPassword() (string, error) {
|
|
|
|
return LoadProperty("password", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveToken(value string) {
|
|
|
|
SaveProperty("token", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadToken() (string, error) {
|
|
|
|
return LoadProperty("token", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveTokenTime(value int64) {
|
|
|
|
SaveProperty("token_time", strconv.FormatInt(value, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadTokenTime() (int64, error) {
|
|
|
|
str, err := LoadProperty("token_time", "0")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseInt(str, 10, 64)
|
|
|
|
}
|