1
0

refactor: migrate backend main function

This commit is contained in:
2026-07-17 10:53:29 +08:00
parent 3c0a763572
commit 44e1415194
2 changed files with 63 additions and 21 deletions

View File

@@ -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()

View File

@@ -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}
var db database.Database
switch cfg.Database.Driver {
case config.DatabaseDriverSqlite:
db, err = database.NewSqlite3Database(deps)
case config.DatabaseDriverMysql:
db, err = database.NewMysqlDatabase(deps)
default:
log.Error("unknown database driver", "driver", cfg.Database.Driver)
os.Exit(1)
}
if err != nil {
log.Error("failed to open database", "error", err)
os.Exit(1)
}
// 创建默认的 Gin 引擎(包含 Logger 和 Recovery 中间件) // Initialize the schema and first admin user when requested.
r := gin.Default() 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.
r.GET("/", func(c *gin.Context) { defer db.Close()
c.JSON(http.StatusOK, gin.H{ log.Info("Starting server...")
"message": "Hello, Gin!", 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)
//启动服务器默认端口8088 }
r.Run(":8080")
} }