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"
|
|
|
|
"github.com/alist-org/alist/v3/server"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// serverCmd represents the server command
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
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.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)
|
|
|
|
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 {
|
|
|
|
log.Errorf("failed to start: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// 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
|
|
|
|
log.Println("Shutdown Server ...")
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
|
|
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():
|
|
|
|
log.Println("timeout of 3 seconds.")
|
2022-08-07 05:09:59 +00:00
|
|
|
}
|
2022-08-28 07:34:12 +00:00
|
|
|
log.Println("Server exiting")
|
2022-08-07 05:09:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(serverCmd)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|