✨ cache
This commit is contained in:
@ -6,15 +6,18 @@ import (
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
Name string `json:"name" gorm:"primaryKey" validate:"required"`
|
||||
Type string `json:"type"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
AccessToken string `json:"access_token"`
|
||||
RootFolder string `json:"root_folder"`
|
||||
Status int `json:"status"`
|
||||
CronId int `json:"cron_id"`
|
||||
Name string `json:"name" gorm:"primaryKey" validate:"required"`
|
||||
Type string `json:"type"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
AccessToken string `json:"access_token"`
|
||||
RootFolder string `json:"root_folder"`
|
||||
Status string
|
||||
CronId int
|
||||
DriveId string
|
||||
OrderBy string `json:"order_by"`
|
||||
OrderDirection string `json:"order_direction"`
|
||||
}
|
||||
|
||||
var accountsMap = map[string]Account{}
|
||||
@ -78,6 +81,7 @@ func GetAccounts() []*Account {
|
||||
}
|
||||
|
||||
func initAccounts() {
|
||||
log.Infof("init accounts...")
|
||||
var accounts []Account
|
||||
if err := conf.DB.Find(&accounts).Error; err != nil {
|
||||
log.Fatalf("failed sync init accounts")
|
||||
|
@ -69,7 +69,7 @@ func InitModel() {
|
||||
log.Fatalf("failed to auto migrate")
|
||||
}
|
||||
|
||||
// TODO init accounts and filetype
|
||||
// TODO init filetype
|
||||
initAccounts()
|
||||
initSettings()
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,16 @@
|
||||
package model
|
||||
|
||||
import "github.com/Xhofe/alist/conf"
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
PUBLIC = iota
|
||||
PRIVATE
|
||||
CONST
|
||||
)
|
||||
|
||||
type SettingItem struct {
|
||||
Key string `json:"key" gorm:"primaryKey" validate:"required"`
|
||||
@ -10,11 +20,6 @@ type SettingItem struct {
|
||||
}
|
||||
|
||||
func SaveSettings(items []SettingItem) error {
|
||||
//tx := conf.DB.Begin()
|
||||
//for _,item := range items{
|
||||
// tx.Save(item)
|
||||
//}
|
||||
//tx.Commit()
|
||||
return conf.DB.Save(items).Error
|
||||
}
|
||||
|
||||
@ -25,3 +30,56 @@ func GetSettingByType(t int) (*[]SettingItem, error) {
|
||||
}
|
||||
return &items, nil
|
||||
}
|
||||
|
||||
func GetSettingByKey(key string) (*SettingItem, error) {
|
||||
var items SettingItem
|
||||
if err := conf.DB.Where("key = ?", key).First(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &items, nil
|
||||
}
|
||||
|
||||
func initSettings() {
|
||||
log.Infof("init settings...")
|
||||
version, err := GetSettingByKey("version")
|
||||
if err != nil {
|
||||
log.Debugf("first run")
|
||||
version = &SettingItem{
|
||||
Key: "version",
|
||||
Value: "0.0.0",
|
||||
Description: "version",
|
||||
Type: CONST,
|
||||
}
|
||||
}
|
||||
settingsMap := map[string][]SettingItem{
|
||||
"2.0.0": {
|
||||
{
|
||||
Key: "title",
|
||||
Value: "Alist",
|
||||
Description: "title",
|
||||
Type: PUBLIC,
|
||||
},
|
||||
{
|
||||
Key: "password",
|
||||
Value: "alist",
|
||||
Description: "password",
|
||||
Type: PRIVATE,
|
||||
},
|
||||
{
|
||||
Key: "version",
|
||||
Value: "2.0.0",
|
||||
Description: "version",
|
||||
Type: CONST,
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, v := range settingsMap {
|
||||
if utils.VersionCompare(k, version.Value) > 0 {
|
||||
log.Infof("writing [v%s] settings",k)
|
||||
err = SaveSettings(v)
|
||||
if err != nil {
|
||||
log.Fatalf("save settings error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user