Initial commit

This commit is contained in:
微凉
2020-12-24 01:39:45 +08:00
commit 430b4cf723
30 changed files with 1223 additions and 0 deletions

34
bootstrap/alidrive.go Normal file
View File

@@ -0,0 +1,34 @@
package bootstrap
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus"
)
func InitAliDrive() bool {
//首先token_login
if conf.Conf.AliDrive.RefreshToken == "" {
tokenLogin,err:=alidrive.TokenLogin()
if err!=nil {
log.Errorf("登录失败:%s",err.Error())
return false
}
//然后get_token
token,err:=alidrive.GetToken(tokenLogin)
if err!=nil {
return false
}
conf.Authorization=token.TokenType+" "+token.AccessToken
}
conf.Authorization=conf.Bearer+conf.Conf.AliDrive.AccessToken
log.Infof("token:%s",conf.Authorization)
user,err:=alidrive.GetUserInfo()
if err != nil {
log.Errorf("初始化用户失败:%s",err.Error())
return false
}
log.Infof("当前用户信息:%v",user)
alidrive.User=user
return true
}

10
bootstrap/client.go Normal file
View File

@@ -0,0 +1,10 @@
package bootstrap
import (
"github.com/Xhofe/alist/conf"
"net/http"
)
func InitClient() {
conf.Client=&http.Client{}
}

64
bootstrap/cmd.go Normal file
View File

@@ -0,0 +1,64 @@
package bootstrap
import (
"flag"
"github.com/Xhofe/alist/conf"
serv "github.com/Xhofe/alist/server"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func init() {
flag.BoolVar(&conf.Debug,"debug",false,"use debug mode")
flag.BoolVar(&conf.Help,"help",false,"show usage help")
flag.StringVar(&conf.Con,"conf","conf.yml","config file")
}
func Run() {
flag.Parse()
if conf.Help {
flag.Usage()
return
}
start()
}
func printASC() {
log.Info(`
________ ___ ___ ________ _________
|\ __ \|\ \ |\ \|\ ____\|\___ ___\
\ \ \|\ \ \ \ \ \ \ \ \___|\|___ \ \_|
\ \ __ \ \ \ \ \ \ \_____ \ \ \ \
\ \ \ \ \ \ \____\ \ \|____|\ \ \ \ \
\ \__\ \__\ \_______\ \__\____\_\ \ \ \__\
\|__|\|__|\|_______|\|__|\_________\ \|__|
\|_________|
`)
}
func start() {
InitLog()
printASC()
InitClient()
if !ReadConf(conf.Con) {
log.Errorf("读取配置文件时出现错误,启动失败.")
return
}
if !InitAliDrive() {
log.Errorf("初始化阿里云盘出现错误,启动失败.")
return
}
InitCron()
server()
}
func server() {
baseServer:="0.0.0.0:"+conf.Conf.Server.Port
log.Infof("Starting server @ %s",baseServer)
r:=gin.Default()
serv.InitRouter(r)
err:=r.Run(baseServer)
if err!=nil {
log.Errorf("Server failed start:%s",err.Error())
}
}

28
bootstrap/config.go Normal file
View File

@@ -0,0 +1,28 @@
package bootstrap
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
)
func ReadConf(config string) bool {
if !utils.Exists(config) {
log.Infof("找不到配置文件:%s",config)
return false
}
confFile,err:=ioutil.ReadFile(config)
if err !=nil {
log.Errorf("读取配置文件时发生错误:%s",err.Error())
return false
}
err = yaml.Unmarshal(confFile, conf.Conf)
if err !=nil {
log.Errorf("加载配置文件时发生错误:%s",err.Error())
return false
}
log.Debugf("config:%v",conf.Conf)
return true
}

26
bootstrap/cron.go Normal file
View File

@@ -0,0 +1,26 @@
package bootstrap
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
var Cron *cron.Cron
func refreshToken() {
alidrive.RefreshToken()
utils.WriteToYml(conf.Con,conf.Conf)
}
func InitCron() {
log.Infof("初始化定时任务:刷新token")
Cron=cron.New()
_,err:=Cron.AddFunc("@every 2h",refreshToken)
if err!=nil {
log.Errorf("添加启动任务失败:%s",err.Error())
}
Cron.Start()
}

21
bootstrap/log.go Normal file
View File

@@ -0,0 +1,21 @@
package bootstrap
import (
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func InitLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
}else {
gin.SetMode(gin.ReleaseMode)
}
log.SetFormatter(&log.TextFormatter{
ForceColors:true,
EnvironmentOverrideColors:true,
TimestampFormat:"2006-01-02 15:04:05",
FullTimestamp:true,
})
}