2019-11-04 20:30:12 +08:00
|
|
|
|
package main
|
|
|
|
|
|
2019-11-05 12:31:22 +08:00
|
|
|
|
import (
|
2022-04-20 19:56:00 +08:00
|
|
|
|
_ "embed"
|
2020-03-09 16:53:01 +08:00
|
|
|
|
"flag"
|
2022-04-20 19:56:00 +08:00
|
|
|
|
"io"
|
|
|
|
|
"io/fs"
|
2022-05-09 19:24:40 +08:00
|
|
|
|
"os"
|
2022-04-20 19:56:00 +08:00
|
|
|
|
"strings"
|
2020-11-21 17:34:55 +08:00
|
|
|
|
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/bootstrap"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/routers"
|
2022-04-20 19:56:00 +08:00
|
|
|
|
|
|
|
|
|
"github.com/mholt/archiver/v4"
|
2019-11-05 12:31:22 +08:00
|
|
|
|
)
|
2019-11-04 20:30:12 +08:00
|
|
|
|
|
2020-05-23 13:57:02 +08:00
|
|
|
|
var (
|
2020-12-06 16:49:49 +08:00
|
|
|
|
isEject bool
|
|
|
|
|
confPath string
|
|
|
|
|
scriptName string
|
2020-05-23 13:57:02 +08:00
|
|
|
|
)
|
2020-03-09 16:53:01 +08:00
|
|
|
|
|
2022-04-20 19:56:00 +08:00
|
|
|
|
//go:embed assets.zip
|
|
|
|
|
var staticZip string
|
|
|
|
|
|
|
|
|
|
var staticFS fs.FS
|
2022-04-09 20:58:07 +08:00
|
|
|
|
|
2019-11-07 15:56:05 +08:00
|
|
|
|
func init() {
|
2020-03-11 10:32:35 +08:00
|
|
|
|
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径")
|
2020-05-23 13:57:02 +08:00
|
|
|
|
flag.BoolVar(&isEject, "eject", false, "导出内置静态资源")
|
2020-12-06 16:49:49 +08:00
|
|
|
|
flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本")
|
2020-03-09 16:53:01 +08:00
|
|
|
|
flag.Parse()
|
2022-04-20 19:56:00 +08:00
|
|
|
|
|
|
|
|
|
staticFS = archiver.ArchiveFS{
|
|
|
|
|
Stream: io.NewSectionReader(strings.NewReader(staticZip), 0, int64(len(staticZip))),
|
|
|
|
|
Format: archiver.Zip{},
|
|
|
|
|
}
|
|
|
|
|
bootstrap.Init(confPath, staticFS)
|
2019-11-07 15:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:30:12 +08:00
|
|
|
|
func main() {
|
2020-05-23 13:57:02 +08:00
|
|
|
|
if isEject {
|
|
|
|
|
// 开始导出内置静态资源文件
|
2022-04-20 19:56:00 +08:00
|
|
|
|
bootstrap.Eject(staticFS)
|
2020-05-23 13:57:02 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 16:49:49 +08:00
|
|
|
|
if scriptName != "" {
|
|
|
|
|
// 开始运行助手数据库脚本
|
|
|
|
|
bootstrap.RunScript(scriptName)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 12:31:22 +08:00
|
|
|
|
api := routers.InitRouter()
|
2020-05-23 13:17:48 +08:00
|
|
|
|
|
|
|
|
|
// 如果启用了SSL
|
|
|
|
|
if conf.SSLConfig.CertPath != "" {
|
|
|
|
|
go func() {
|
|
|
|
|
util.Log().Info("开始监听 %s", conf.SSLConfig.Listen)
|
|
|
|
|
if err := api.RunTLS(conf.SSLConfig.Listen,
|
|
|
|
|
conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil {
|
|
|
|
|
util.Log().Error("无法监听[%s],%s", conf.SSLConfig.Listen, err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 20:31:28 +08:00
|
|
|
|
// 如果启用了Unix
|
|
|
|
|
if conf.UnixConfig.Listen != "" {
|
2022-05-09 19:24:40 +08:00
|
|
|
|
// delete socket file before listening
|
|
|
|
|
if _, err := os.Stat(conf.UnixConfig.Listen); err == nil {
|
|
|
|
|
if err = os.Remove(conf.UnixConfig.Listen); err != nil {
|
|
|
|
|
util.Log().Error("删除 socket 文件错误, %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 11:01:00 +08:00
|
|
|
|
api.TrustedPlatform = conf.UnixConfig.ProxyHeader
|
2021-03-17 14:19:05 +08:00
|
|
|
|
util.Log().Info("开始监听 %s", conf.UnixConfig.Listen)
|
|
|
|
|
if err := api.RunUnix(conf.UnixConfig.Listen); err != nil {
|
|
|
|
|
util.Log().Error("无法监听[%s],%s", conf.UnixConfig.Listen, err)
|
|
|
|
|
}
|
|
|
|
|
return
|
2020-08-12 20:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 16:53:01 +08:00
|
|
|
|
util.Log().Info("开始监听 %s", conf.SystemConfig.Listen)
|
|
|
|
|
if err := api.Run(conf.SystemConfig.Listen); err != nil {
|
|
|
|
|
util.Log().Error("无法监听[%s],%s", conf.SystemConfig.Listen, err)
|
|
|
|
|
}
|
2019-11-04 20:30:12 +08:00
|
|
|
|
}
|