refactor: migrate backend main function
This commit is contained in:
@@ -13,6 +13,10 @@ type CLIArgs struct {
|
|||||||
Config string
|
Config string
|
||||||
// Init indicates whether to initialize the calendar system before running.
|
// Init indicates whether to initialize the calendar system before running.
|
||||||
Init bool
|
Init bool
|
||||||
|
// Username is the first admin user's name; required together with --init.
|
||||||
|
Username string
|
||||||
|
// Password is the first admin user's password; required together with --init.
|
||||||
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses the command-line arguments via the standard flag package and
|
// Parse parses the command-line arguments via the standard flag package and
|
||||||
@@ -31,8 +35,12 @@ func Parse() *CLIArgs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --config maps to the legacy -c/--config (required), --init to -i/--init.
|
// --config maps to the legacy -c/--config (required), --init to -i/--init.
|
||||||
|
// --username/--password replace the legacy interactive GetUsernamePassword
|
||||||
|
// prompt and are required together with --init.
|
||||||
flag.StringVar(&args.Config, "config", "", "The configuration file `CONFIG_TOML` for coconut-leaf")
|
flag.StringVar(&args.Config, "config", "", "The configuration file `CONFIG_TOML` for coconut-leaf")
|
||||||
flag.BoolVar(&args.Init, "init", false, "Set for initialize the calendar system")
|
flag.BoolVar(&args.Init, "init", false, "Set for initialize the calendar system")
|
||||||
|
flag.StringVar(&args.Username, "username", "", "The first admin user's name (required with --init)")
|
||||||
|
flag.StringVar(&args.Password, "password", "", "The first admin user's password (required with --init)")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"fmt"
|
||||||
"net/http"
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
|
|
||||||
"github.com/yyc12345/coconut-leaf/backend/cli"
|
"github.com/yyc12345/coconut-leaf/backend/cli"
|
||||||
"github.com/yyc12345/coconut-leaf/backend/config"
|
"github.com/yyc12345/coconut-leaf/backend/config"
|
||||||
|
"github.com/yyc12345/coconut-leaf/backend/database"
|
||||||
"github.com/yyc12345/coconut-leaf/backend/logger"
|
"github.com/yyc12345/coconut-leaf/backend/logger"
|
||||||
|
"github.com/yyc12345/coconut-leaf/backend/server"
|
||||||
|
"github.com/yyc12345/coconut-leaf/backend/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -17,29 +18,62 @@ func main() {
|
|||||||
|
|
||||||
cfg, err := config.Load(args.Config)
|
cfg, err := config.Load(args.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
fmt.Fprintln(os.Stderr, "error loading config file:", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var loggerLevel logger.LoggerLevel
|
// Splash is printed directly; the logger is not built yet.
|
||||||
|
fmt.Println("Coconut-leaf")
|
||||||
|
fmt.Println("A light, self-host and multi-account calendar system")
|
||||||
|
fmt.Println("Project: https://github.com/yyc12345/coconut-leaf")
|
||||||
|
fmt.Println("===================")
|
||||||
|
|
||||||
|
// Build the logger from the loaded config.
|
||||||
|
loggerLevel := logger.Production
|
||||||
if cfg.Others.Debug {
|
if cfg.Others.Debug {
|
||||||
loggerLevel = logger.Development
|
loggerLevel = logger.Development
|
||||||
} else {
|
|
||||||
loggerLevel = logger.Production
|
|
||||||
}
|
}
|
||||||
logger := logger.New(loggerLevel)
|
log := logger.New(loggerLevel)
|
||||||
|
|
||||||
_ = logger
|
// Create the database backend selected by the config.
|
||||||
|
deps := database.Deps{Cfg: cfg, Logger: log}
|
||||||
// 创建默认的 Gin 引擎(包含 Logger 和 Recovery 中间件)
|
var db database.Database
|
||||||
r := gin.Default()
|
switch cfg.Database.Driver {
|
||||||
|
case config.DatabaseDriverSqlite:
|
||||||
// 定义路由
|
db, err = database.NewSqlite3Database(deps)
|
||||||
r.GET("/", func(c *gin.Context) {
|
case config.DatabaseDriverMysql:
|
||||||
c.JSON(http.StatusOK, gin.H{
|
db, err = database.NewMysqlDatabase(deps)
|
||||||
"message": "Hello, Gin!",
|
default:
|
||||||
})
|
log.Error("unknown database driver", "driver", cfg.Database.Driver)
|
||||||
})
|
os.Exit(1)
|
||||||
|
}
|
||||||
//启动服务器(默认端口8088)
|
if err != nil {
|
||||||
r.Run(":8080")
|
log.Error("failed to open database", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the schema and first admin user when requested.
|
||||||
|
if args.Init {
|
||||||
|
if !utils.IsValidUsername(args.Username) {
|
||||||
|
log.Error("invalid init username")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if !utils.IsValidPassword(args.Password) {
|
||||||
|
log.Error("invalid init password")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if err := db.Init(args.Username, args.Password); err != nil {
|
||||||
|
log.Error("failed to initialize database", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve.
|
||||||
|
defer db.Close()
|
||||||
|
log.Info("Starting server...")
|
||||||
|
handler := &server.Handler{DB: db, Logger: log, Cfg: cfg}
|
||||||
|
if err := server.Run(handler); err != nil {
|
||||||
|
log.Error("server stopped with error", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user