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"
|
2023-06-10 14:26:09 +00:00
|
|
|
"sync"
|
2022-08-28 07:34:12 +00:00
|
|
|
"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()
|
2023-06-05 08:00:31 +00:00
|
|
|
if conf.Conf.DelayedStart != 0 {
|
|
|
|
utils.Log.Infof("delayed start for %d seconds", conf.Conf.DelayedStart)
|
|
|
|
time.Sleep(time.Duration(conf.Conf.DelayedStart) * time.Second)
|
|
|
|
}
|
2022-08-11 13:32:33 +00:00
|
|
|
bootstrap.InitAria2()
|
2023-02-14 07:20:45 +00:00
|
|
|
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)
|
2023-06-10 14:26:09 +00:00
|
|
|
var httpSrv, httpsSrv *http.Server
|
|
|
|
if !conf.Conf.Scheme.DisableHttp {
|
|
|
|
httpBase := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
|
|
|
|
utils.Log.Infof("start HTTP server @ %s", httpBase)
|
|
|
|
httpSrv = &http.Server{Addr: httpBase, Handler: r}
|
|
|
|
go func() {
|
|
|
|
err := httpSrv.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
utils.Log.Fatalf("failed to start: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if conf.Conf.Scheme.Https {
|
|
|
|
httpsBase := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.HttpsPort)
|
|
|
|
utils.Log.Infof("start HTTPS server @ %s", httpsBase)
|
|
|
|
httpsSrv = &http.Server{Addr: httpsBase, Handler: r}
|
|
|
|
go func() {
|
|
|
|
err := httpsSrv.ListenAndServeTLS(conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
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
|
2023-06-10 14:26:09 +00:00
|
|
|
// a timeout of 1 second.
|
|
|
|
quit := make(chan os.Signal, 1)
|
2022-08-28 07:34:12 +00:00
|
|
|
// 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
|
2023-06-10 14:26:09 +00:00
|
|
|
utils.Log.Println("Shutdown server...")
|
2022-08-28 07:34:12 +00:00
|
|
|
|
2022-10-08 14:16:41 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
2022-08-28 07:34:12 +00:00
|
|
|
defer cancel()
|
2023-06-10 14:26:09 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
if !conf.Conf.Scheme.DisableHttp {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := httpSrv.Shutdown(ctx); err != nil {
|
|
|
|
utils.Log.Fatal("HTTP server shutdown:", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-08-07 05:09:59 +00:00
|
|
|
}
|
2023-06-10 14:26:09 +00:00
|
|
|
if conf.Conf.Scheme.Https {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := httpsSrv.Shutdown(ctx); err != nil {
|
|
|
|
utils.Log.Fatal("HTTPS server shutdown:", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-08-07 05:09:59 +00:00
|
|
|
}
|
2023-06-10 14:26:09 +00:00
|
|
|
wg.Wait()
|
|
|
|
utils.Log.Println("Server exit")
|
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")
|
|
|
|
}
|
2022-09-13 12:12:57 +00:00
|
|
|
|
|
|
|
// OutAlistInit 暴露用于外部启动server的函数
|
|
|
|
func OutAlistInit() {
|
|
|
|
var (
|
|
|
|
cmd *cobra.Command
|
|
|
|
args []string
|
|
|
|
)
|
2022-12-18 11:53:39 +00:00
|
|
|
ServerCmd.Run(cmd, args)
|
2022-09-13 12:12:57 +00:00
|
|
|
}
|