Skip to content

Commit

Permalink
✨ bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 27, 2021
1 parent 9644cc9 commit 55f683b
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 133 deletions.
20 changes: 20 additions & 0 deletions bootstrap/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bootstrap

import (
"github.com/Xhofe/alist/conf"
"github.com/allegro/bigcache/v3"
"github.com/eko/gocache/v2/cache"
"github.com/eko/gocache/v2/store"
log "github.com/sirupsen/logrus"
"time"
)

// InitCache init cache
func InitCache() {
log.Infof("init cache...")
bigCacheConfig := bigcache.DefaultConfig(60 * time.Minute)
bigCacheConfig.HardMaxCacheSize = 512
bigCacheClient, _ := bigcache.NewBigCache(bigCacheConfig)
bigCacheStore := store.NewBigcache(bigCacheClient, nil)
conf.Cache = cache.New(bigCacheStore)
}
32 changes: 32 additions & 0 deletions bootstrap/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bootstrap

import (
"encoding/json"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"io/ioutil"
)

// InitConf init config
func InitConf() {
log.Infof("reading config file: %s", conf.ConfigFile)
if !utils.Exists(conf.ConfigFile) {
log.Infof("config file not exists, creating default config file")
conf.Conf = conf.DefaultConfig()
if !utils.WriteToJson(conf.ConfigFile, conf.Conf) {
log.Fatalf("failed to create default config file")
}
return
}
config, err := ioutil.ReadFile(conf.ConfigFile)
if err != nil {
log.Fatalf("reading config file error:%s", err.Error())
}
conf.Conf = new(conf.Config)
err = json.Unmarshal(config, conf.Conf)
if err != nil {
log.Fatalf("load config error: %s", err.Error())
}
log.Debugf("config:%+v", conf.Conf)
}
14 changes: 14 additions & 0 deletions bootstrap/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bootstrap

import (
"github.com/Xhofe/alist/conf"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)

// InitCron init cron
func InitCron() {
log.Infof("init cron...")
conf.Cron = cron.New()
conf.Cron.Start()
}
21 changes: 21 additions & 0 deletions bootstrap/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package bootstrap

import (
"github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus"
)

// initLog init log
func InitLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}
log.SetFormatter(&log.TextFormatter{
//DisableColors: true,
ForceColors: true,
EnvironmentOverrideColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
78 changes: 76 additions & 2 deletions model/init.go → bootstrap/model.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package model
package bootstrap

import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -64,7 +67,7 @@ func InitModel() {
log.Fatalf("not supported database type: %s", config.Type)
}
log.Infof("auto migrate model")
err := conf.DB.AutoMigrate(&SettingItem{},&Account{})
err := conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{})
if err != nil {
log.Fatalf("failed to auto migrate")
}
Expand All @@ -73,3 +76,74 @@ func InitModel() {
initAccounts()
initSettings()
}

func initAccounts() {
log.Infof("init accounts...")
var accounts []model.Account
if err := conf.DB.Find(&accounts).Error; err != nil {
log.Fatalf("failed sync init accounts")
}
for _, account := range accounts {
model.RegisterAccount(account)
driver, ok := drivers.GetDriver(account.Type)
if !ok {
log.Error("no [%s] driver", driver)
} else {
err := driver.Save(&account, nil)
if err != nil {
log.Errorf("init account [%s] error:[%s]", account.Name, err.Error())
}
}
}
}

func initSettings() {
log.Infof("init settings...")
version, err := model.GetSettingByKey("version")
if err != nil {
log.Debugf("first run")
version = &model.SettingItem{
Key: "version",
Value: "0.0.0",
Description: "version",
Type: model.CONST,
}
}
settingsMap := map[string][]model.SettingItem{
"2.0.0": {
{
Key: "title",
Value: "Alist",
Description: "title",
Type: model.PUBLIC,
},
{
Key: "password",
Value: "alist",
Description: "password",
Type: model.PRIVATE,
},
{
Key: "version",
Value: "2.0.0",
Description: "version",
Type: model.CONST,
},
{
Key: "logo",
Value: "",
Description: "logo",
Type: model.PUBLIC,
},
},
}
for k, v := range settingsMap {
if utils.VersionCompare(k, version.Value) > 0 {
log.Infof("writing [v%s] settings", k)
err = model.SaveSettings(v)
if err != nil {
log.Fatalf("save settings error")
}
}
}
}
2 changes: 2 additions & 0 deletions conf/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conf
import (
"context"
"github.com/eko/gocache/v2/cache"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
)

Expand All @@ -14,6 +15,7 @@ var (
DB *gorm.DB
Cache *cache.Cache
Ctx = context.TODO()
Cron *cron.Cron
)

var (
Expand Down
72 changes: 68 additions & 4 deletions drivers/alidrive.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package drivers

import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/go-resty/resty/v2"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
"time"
)

var aliClient = resty.New()

func init() {
aliClient.
SetRetryCount(3).
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36").
SetHeader("content-type", "application/json").
SetHeader("origin", "https://aliyundrive.com")
}

type AliDrive struct {
}

Expand Down Expand Up @@ -43,23 +57,73 @@ type AliFile struct {
}

func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
_,err := conf.Cache.Get(conf.Ctx,path)
_, err := conf.Cache.Get(conf.Ctx, path)
if err == nil {
// return
}

panic("implement me")
}

func (a AliDrive) Link(path string, account *model.Account) (string, error) {
panic("implement me")
}

type AliTokenResp struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}

func AliRefreshToken(refresh string) (string, string, error) {
url := "https://auth.aliyundrive.com/v2/account/token"
var resp AliTokenResp
var e AliRespError
_, err := aliClient.R().
//ForceContentType("application/json").
SetBody(JsonStr(Json{"refresh_token": refresh, "grant_type": "refresh_token"})).
SetResult(&resp).
SetError(&e).
Post(url)
if err != nil {
return "", "", err
}
log.Debugf("%+v,%+v", resp, e)
if e.Code != "" {
return "", "", fmt.Errorf("failed to refresh token: %s", e.Message)
}
return resp.RefreshToken, resp.AccessToken, nil
}

func (a AliDrive) Save(account *model.Account, old *model.Account) error {
if old != nil {
// TODO clear something
conf.Cron.Remove(cron.EntryID(old.CronId))
}
panic("implement me")
refresh, access, err := AliRefreshToken(account.RefreshToken)
if err != nil {
return err
}
account.RefreshToken, account.AccessToken = refresh, access
cronId, err := conf.Cron.AddFunc("@every 2h", func() {
name := account.Name
newAccount, ok := model.GetAccount(name)
if !ok {
return
}
newAccount.RefreshToken, newAccount.AccessToken, err = AliRefreshToken(newAccount.RefreshToken)
if err != nil {
newAccount.Status = err.Error()
}
_ = model.SaveAccount(newAccount)
})
if err != nil {
return err
}
account.CronId = int(cronId)
err = model.SaveAccount(*account)
if err != nil {
return err
}
return nil
}

var _ Driver = (*AliDrive)(nil)
Expand Down
12 changes: 11 additions & 1 deletion drivers/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package drivers

import "github.com/Xhofe/alist/model"
import (
"encoding/json"
"github.com/Xhofe/alist/model"
)

type Driver interface {
Path(path string, account *model.Account) (*model.File, []*model.File, error)
Expand All @@ -26,3 +29,10 @@ func GetDriverNames() []string {
}
return names
}

type Json map[string]interface{}

func JsonStr(j Json) string {
data, _ := json.Marshal(j)
return string(data)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-redis/redis/v8 v8.9.0 // indirect
github.com/go-resty/resty/v2 v2.6.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand All @@ -46,6 +47,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.18.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/robfig/cron/v3 v3.0.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.31.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ github.com/go-playground/validator/v10 v10.9.0 h1:NgTtmN58D0m8+UuxtYmGztBJB7VnPg
github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-redis/redis/v8 v8.9.0 h1:FTTbB7WqlXfVNdVv0SsxA+oVi0bAwit6bMe3IUucq2o=
github.com/go-redis/redis/v8 v8.9.0/go.mod h1:ik7vb7+gm8Izylxu6kf6wG26/t2VljgCfSQ1DM4O1uU=
github.com/go-resty/resty/v2 v2.6.0 h1:joIR5PNLM2EFqqESUjCMGXrWmXNHEU9CEiK813oKYS4=
github.com/go-resty/resty/v2 v2.6.0/go.mod h1:PwvJS6hvaPkjtjNg9ph+VrSD92bi5Zq73w/BIH7cC3Q=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down Expand Up @@ -411,6 +413,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down Expand Up @@ -546,6 +550,7 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -593,6 +598,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading

0 comments on commit 55f683b

Please sign in to comment.