Cloudreve/main.go

139 lines
3.4 KiB
Go
Raw Normal View History

2019-11-04 07:30:12 -05:00
package main
2019-11-04 23:31:22 -05:00
import (
2022-08-22 06:49:19 -05:00
"context"
_ "embed"
2020-03-09 03:53:01 -05:00
"flag"
"io/fs"
2022-08-22 06:49:19 -05:00
"net"
"net/http"
"os"
2022-08-22 06:49:19 -05:00
"os/signal"
"syscall"
"time"
"github.com/cloudreve/Cloudreve/v3/bootstrap"
2022-08-22 06:49:19 -05:00
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v3/routers"
2019-11-04 23:31:22 -05:00
)
2019-11-04 07:30:12 -05:00
2020-05-23 00:57:02 -05:00
var (
isEject bool
confPath string
scriptName string
2020-05-23 00:57:02 -05:00
)
2020-03-09 03:53:01 -05:00
//go:embed assets.zip
var staticZip string
var staticFS fs.FS
2019-11-07 02:56:05 -05:00
func init() {
2022-10-08 05:51:52 -05:00
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "Path to the config file.")
flag.BoolVar(&isEject, "eject", false, "Eject all embedded static files.")
flag.StringVar(&scriptName, "database-script", "", "Name of database util script.")
2020-03-09 03:53:01 -05:00
flag.Parse()
staticFS = bootstrap.NewFS(staticZip)
bootstrap.Init(confPath, staticFS)
2019-11-07 02:56:05 -05:00
}
2019-11-04 07:30:12 -05:00
func main() {
2022-08-22 06:49:19 -05:00
// 关闭数据库连接
defer model.DB.Close()
2020-05-23 00:57:02 -05:00
if isEject {
// 开始导出内置静态资源文件
bootstrap.Eject(staticFS)
2020-05-23 00:57:02 -05:00
return
}
if scriptName != "" {
// 开始运行助手数据库脚本
bootstrap.RunScript(scriptName)
return
}
2019-11-04 23:31:22 -05:00
api := routers.InitRouter()
2022-08-22 06:49:19 -05:00
server := &http.Server{Handler: api}
// 收到信号后关闭服务器
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
go func() {
sig := <-sigChan
2022-10-08 05:51:52 -05:00
util.Log().Info("Signal %s received, shutting down server...", sig)
2022-08-22 06:49:19 -05:00
ctx := context.Background()
if conf.SystemConfig.GracePeriod != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(conf.SystemConfig.GracePeriod)*time.Second)
defer cancel()
}
err := server.Shutdown(ctx)
if err != nil {
2022-10-08 05:51:52 -05:00
util.Log().Error("Failed to shutdown server: %s", err)
2022-08-22 06:49:19 -05:00
}
}()
// 如果启用了SSL
if conf.SSLConfig.CertPath != "" {
2022-10-08 05:51:52 -05:00
util.Log().Info("Listening to %q", conf.SSLConfig.Listen)
2022-08-22 06:49:19 -05:00
server.Addr = conf.SSLConfig.Listen
if err := server.ListenAndServeTLS(conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil {
2022-10-08 05:51:52 -05:00
util.Log().Error("Failed to listen to %q: %s", conf.SSLConfig.Listen, err)
2022-08-22 06:49:19 -05:00
return
}
}
// 如果启用了Unix
if conf.UnixConfig.Listen != "" {
// delete socket file before listening
if _, err := os.Stat(conf.UnixConfig.Listen); err == nil {
if err = os.Remove(conf.UnixConfig.Listen); err != nil {
2022-10-08 05:51:52 -05:00
util.Log().Error("Failed to delete socket file: %s", err)
return
}
}
api.TrustedPlatform = conf.UnixConfig.ProxyHeader
2022-10-08 05:51:52 -05:00
util.Log().Info("Listening to %q", conf.UnixConfig.Listen)
2022-08-22 06:49:19 -05:00
if err := RunUnix(server); err != nil {
2022-10-08 05:51:52 -05:00
util.Log().Error("Failed to listen to %q: %s", conf.UnixConfig.Listen, err)
}
return
}
2022-10-08 05:51:52 -05:00
util.Log().Info("Listening to %q", conf.SystemConfig.Listen)
2022-08-22 06:49:19 -05:00
server.Addr = conf.SystemConfig.Listen
if err := server.ListenAndServe(); err != nil {
2022-10-08 05:51:52 -05:00
util.Log().Error("Failed to listen to %q: %s", conf.SystemConfig.Listen, err)
2020-03-09 03:53:01 -05:00
}
2019-11-04 07:30:12 -05:00
}
2022-08-22 06:49:19 -05:00
func RunUnix(server *http.Server) error {
listener, err := net.Listen("unix", conf.UnixConfig.Listen)
if err != nil {
return err
}
2022-08-22 06:49:19 -05:00
defer listener.Close()
defer os.Remove(conf.UnixConfig.Listen)
if conf.UnixConfig.Perm > 0 {
err = os.Chmod(conf.UnixConfig.Listen, os.FileMode(conf.UnixConfig.Perm))
if err != nil {
util.Log().Warning(
"Failed to set permission to %q for socket file %q: %s",
conf.UnixConfig.Perm,
conf.UnixConfig.Listen,
err,
)
}
}
2022-08-22 06:49:19 -05:00
return server.Serve(listener)
}