diff --git a/bootstrap/init.go b/bootstrap/init.go index 2aea14d..0f93399 100644 --- a/bootstrap/init.go +++ b/bootstrap/init.go @@ -98,16 +98,7 @@ func Init(path string, statics fs.FS) { } for _, dependency := range dependencies { - switch dependency.mode { - case "master": - if conf.SystemConfig.Mode == "master" { - dependency.factory() - } - case "slave": - if conf.SystemConfig.Mode == "slave" { - dependency.factory() - } - default: + if dependency.mode == conf.SystemConfig.Mode || dependency.mode == "both" { dependency.factory() } } diff --git a/go.mod b/go.mod index 6cb1dc3..394b77a 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20200120023323-87ff3bc489ac github.com/upyun/go-sdk v2.1.0+incompatible golang.org/x/image v0.0.0-20211028202545-6944b10bf410 - gopkg.in/go-playground/validator.v9 v9.29.1 + golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba ) require ( @@ -99,7 +99,6 @@ require ( github.com/mattn/go-colorable v0.1.4 // indirect github.com/mattn/go-isatty v0.0.12 // indirect github.com/mattn/go-runewidth v0.0.12 // indirect - github.com/mattn/go-sqlite3 v1.14.7 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mitchellh/mapstructure v1.1.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -149,7 +148,6 @@ require ( golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect golang.org/x/text v0.3.7 // indirect - golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect golang.org/x/tools v0.1.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/go.sum b/go.sum index a29f4ce..23fd3af 100644 --- a/go.sum +++ b/go.sum @@ -1393,10 +1393,8 @@ gopkg.in/cheggaaa/pb.v1 v1.0.28/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qS gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= -gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= -gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc= gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/main.go b/main.go index 69e628b..b691f6e 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,21 @@ package main import ( + "context" _ "embed" "flag" "io" "io/fs" + "net" + "net/http" "os" + "os/signal" "strings" + "syscall" + "time" "github.com/cloudreve/Cloudreve/v3/bootstrap" + 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" @@ -41,6 +48,9 @@ func init() { } func main() { + // 关闭数据库连接 + defer model.DB.Close() + if isEject { // 开始导出内置静态资源文件 bootstrap.Eject(staticFS) @@ -54,16 +64,35 @@ func main() { } api := routers.InitRouter() + 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 + util.Log().Info("收到信号 %s,开始关闭 server", sig) + 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 { + util.Log().Error("关闭 server 错误, %s", err) + } + }() // 如果启用了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) - } - }() + util.Log().Info("开始监听 %s", conf.SSLConfig.Listen) + server.Addr = conf.SSLConfig.Listen + if err := server.ListenAndServeTLS(conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil { + util.Log().Error("无法监听[%s],%s", conf.SSLConfig.Listen, err) + return + } } // 如果启用了Unix @@ -78,14 +107,26 @@ func main() { api.TrustedPlatform = conf.UnixConfig.ProxyHeader util.Log().Info("开始监听 %s", conf.UnixConfig.Listen) - if err := api.RunUnix(conf.UnixConfig.Listen); err != nil { + if err := RunUnix(server); err != nil { util.Log().Error("无法监听[%s],%s", conf.UnixConfig.Listen, err) } return } util.Log().Info("开始监听 %s", conf.SystemConfig.Listen) - if err := api.Run(conf.SystemConfig.Listen); err != nil { + server.Addr = conf.SystemConfig.Listen + if err := server.ListenAndServe(); err != nil { util.Log().Error("无法监听[%s],%s", conf.SystemConfig.Listen, err) } } + +func RunUnix(server *http.Server) error { + listener, err := net.Listen("unix", conf.UnixConfig.Listen) + if err != nil { + return err + } + defer listener.Close() + defer os.Remove(conf.UnixConfig.Listen) + + return server.Serve(listener) +} diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index a9f7d06..5770e71 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -3,7 +3,7 @@ package conf import ( "github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/go-ini/ini" - "gopkg.in/go-playground/validator.v9" + "github.com/go-playground/validator/v10" ) // database 数据库 @@ -26,6 +26,7 @@ type system struct { Debug bool SessionSecret string HashIDSalt string + GracePeriod int `validate:"gte=0"` } type ssl struct {