alist/cmd/server.go

101 lines
2.9 KiB
Go
Raw Normal View History

2022-08-07 05:09:59 +00:00
package cmd
import (
2022-08-28 07:34:12 +00:00
"context"
2022-08-07 05:09:59 +00:00
"fmt"
2022-08-28 07:34:12 +00:00
"net/http"
"os"
"os/signal"
"syscall"
"time"
2022-08-07 05:23:15 +00:00
2022-08-07 05:09:59 +00:00
"github.com/alist-org/alist/v3/cmd/flags"
2022-08-07 05:23:15 +00:00
_ "github.com/alist-org/alist/v3/drivers"
2022-08-11 13:32:33 +00:00
"github.com/alist-org/alist/v3/internal/bootstrap"
2022-08-07 05:09:59 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-08-30 07:22:54 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-08-07 05:09:59 +00:00
"github.com/alist-org/alist/v3/server"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
2022-12-18 11:53:39 +00:00
// ServerCmd represents the server command
var ServerCmd = &cobra.Command{
2022-08-07 05:09:59 +00:00
Use: "server",
Short: "Start the server at the specified address",
Long: `Start the server at the specified address
the address is defined in config file`,
Run: func(cmd *cobra.Command, args []string) {
Init()
2022-08-11 13:32:33 +00:00
bootstrap.InitAria2()
bootstrap.InitQbittorrent()
2022-08-11 13:32:33 +00:00
bootstrap.LoadStorages()
2022-08-07 05:09:59 +00:00
if !flags.Debug && !flags.Dev {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
server.Init(r)
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
2022-08-30 07:22:54 +00:00
utils.Log.Infof("start server @ %s", base)
2022-08-28 07:34:12 +00:00
srv := &http.Server{Addr: base, Handler: r}
go func() {
var err error
if conf.Conf.Scheme.Https {
//err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
err = srv.ListenAndServeTLS(conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
} else {
err = srv.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
2022-08-30 08:13:01 +00:00
utils.Log.Fatalf("failed to start: %s", err.Error())
2022-08-28 07:34:12 +00:00
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
2022-08-30 07:22:54 +00:00
utils.Log.Println("Shutdown Server ...")
2022-08-28 07:34:12 +00:00
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
2022-08-28 07:34:12 +00:00
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
2022-08-30 07:22:54 +00:00
utils.Log.Fatal("Server Shutdown:", err)
2022-08-07 05:09:59 +00:00
}
2022-08-28 07:34:12 +00:00
// catching ctx.Done(). timeout of 3 seconds.
select {
case <-ctx.Done():
utils.Log.Println("timeout of 1 seconds.")
2022-08-07 05:09:59 +00:00
}
2022-08-30 07:22:54 +00:00
utils.Log.Println("Server exiting")
2022-08-07 05:09:59 +00:00
},
}
func init() {
2022-12-18 11:53:39 +00:00
RootCmd.AddCommand(ServerCmd)
2022-08-07 05:09:59 +00:00
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// OutAlistInit 暴露用于外部启动server的函数
func OutAlistInit() {
var (
cmd *cobra.Command
args []string
)
2022-12-18 11:53:39 +00:00
ServerCmd.Run(cmd, args)
}