refactor: update go backend with cli and config
This commit is contained in:
45
backend/cli.go
Normal file
45
backend/cli.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// CLIArgs 保存从命令行解析得到的参数,对应旧后端 coconut-leaf.py 中 argparse 解析的结果。
|
||||
type CLIArgs struct {
|
||||
// Config 是 coconut-leaf 的配置文件路径(即旧后端的 CONFIG_TOML)。
|
||||
Config string
|
||||
// Init 指示是否在启动前初始化日历系统。
|
||||
Init bool
|
||||
}
|
||||
|
||||
// ParseCLI 使用标准库 flag 包解析命令行参数并返回 *CLIArgs。
|
||||
// 当必需的 --config 参数缺失时,打印用法并以退出码 2 退出(与 argparse 的退出码一致)。
|
||||
func ParseCLI() *CLIArgs {
|
||||
args := &CLIArgs{}
|
||||
|
||||
// 自定义用法输出,复刻旧后端 ArgumentParser 的 description 横幅。
|
||||
flag.Usage = func() {
|
||||
out := flag.CommandLine.Output()
|
||||
fmt.Fprintln(out, "The server of light, self-host and multi-account calendar system.")
|
||||
fmt.Fprintln(out)
|
||||
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
// --config 对应旧后端的 -c/--config(必需),--init 对应旧后端的 -i/--init。
|
||||
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.Parse()
|
||||
|
||||
// 标准库 flag 不支持 required,故解析后手动校验。
|
||||
if args.Config == "" {
|
||||
fmt.Fprintln(os.Stderr, "error: the required flag --config is not provided")
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
141
backend/config.go
Normal file
141
backend/config.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// DatabaseDriver 表示配置文件中指定的数据库驱动类型,对应旧后端的 DatabaseDriver(StrEnum)。
|
||||
// 用 const 配合 iota 模拟数值枚举,字符串形式由 databaseDriverFromString 转换。
|
||||
type DatabaseDriver int
|
||||
|
||||
const (
|
||||
// DatabaseDriverSqlite 表示使用 SQLite 数据库。
|
||||
DatabaseDriverSqlite DatabaseDriver = iota
|
||||
// DatabaseDriverMysql 表示使用 MySQL 数据库。
|
||||
DatabaseDriverMysql
|
||||
)
|
||||
|
||||
// databaseDriverFromString 将配置文件中的 driver 字符串转换为 DatabaseDriver,
|
||||
// 无法识别的字符串将返回错误。
|
||||
func databaseDriverFromString(s string) (DatabaseDriver, error) {
|
||||
switch s {
|
||||
case "sqlite":
|
||||
return DatabaseDriverSqlite, nil
|
||||
case "mysql":
|
||||
return DatabaseDriverMysql, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("invalid database driver: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
// SqliteDatabaseConfig 是 SQLite 数据库的连接配置,对应旧后端 SqliteDatabaseConfig。
|
||||
type SqliteDatabaseConfig struct {
|
||||
// Path 是数据库文件的路径。
|
||||
Path string `toml:"path"`
|
||||
}
|
||||
|
||||
// MysqlDatabaseConfig 是 MySQL 数据库的连接配置,对应旧后端 MysqlDatabaseConfig。
|
||||
type MysqlDatabaseConfig struct {
|
||||
// Host 是数据库主机地址。
|
||||
Host string `toml:"host"`
|
||||
// Port 是数据库端口。
|
||||
Port int `toml:"port"`
|
||||
// User 是数据库用户名。
|
||||
User string `toml:"user"`
|
||||
// Password 是数据库密码。
|
||||
Password string `toml:"password"`
|
||||
// Database 是数据库名称。
|
||||
Database string `toml:"database"`
|
||||
}
|
||||
|
||||
// DatabaseConfig 描述数据库驱动及其对应的具体配置,对应旧后端 DatabaseConfig。
|
||||
// 其中 Driver 起判别作用,决定 Sqlite 或 Mysql 中的哪一个被填充。
|
||||
type DatabaseConfig struct {
|
||||
// Driver 是当前选择的数据库驱动。
|
||||
Driver DatabaseDriver
|
||||
// Sqlite 在 Driver 为 DatabaseDriverSqlite 时非空。
|
||||
Sqlite *SqliteDatabaseConfig
|
||||
// Mysql 在 Driver 为 DatabaseDriverMysql 时非空。
|
||||
Mysql *MysqlDatabaseConfig
|
||||
}
|
||||
|
||||
// WebConfig 是 Web 服务器的配置,对应旧后端 WebConfig。
|
||||
type WebConfig struct {
|
||||
// Port 是 Web 服务器监听端口。
|
||||
Port int `toml:"port"`
|
||||
}
|
||||
|
||||
// OthersConfig 是其余杂项配置,对应旧后端 OthersConfig。
|
||||
type OthersConfig struct {
|
||||
// Debug 指示是否启用调试模式。
|
||||
Debug bool `toml:"debug"`
|
||||
// AutoTokenCleanDuration 是自动清理过期 token 的时间间隔(秒)。
|
||||
AutoTokenCleanDuration int `toml:"auto-token-clean-duration"`
|
||||
}
|
||||
|
||||
// Config 是从配置文件解析得到的完整配置,对应旧后端 Config。
|
||||
type Config struct {
|
||||
// Database 是数据库相关配置。
|
||||
Database DatabaseConfig
|
||||
// Web 是 Web 服务器相关配置。
|
||||
Web WebConfig
|
||||
// Others 是其余杂项配置。
|
||||
Others OthersConfig
|
||||
}
|
||||
|
||||
// rawDatabaseConfig 是仅用于 TOML 解析的内部结构。
|
||||
// 其 Config 字段以 toml.Primitive 暂存,以便依据 Driver 做两段式分发解码。
|
||||
type rawDatabaseConfig struct {
|
||||
Driver string `toml:"driver"`
|
||||
Config toml.Primitive `toml:"config"`
|
||||
}
|
||||
|
||||
// rawConfig 是仅用于 TOML 解析的内部结构,配合两段式解码使用。
|
||||
type rawConfig struct {
|
||||
Database rawDatabaseConfig `toml:"database"`
|
||||
Web WebConfig `toml:"web"`
|
||||
Others OthersConfig `toml:"others"`
|
||||
}
|
||||
|
||||
// LoadConfig 从给定路径读取并解析 TOML 配置文件,返回解析后的配置或错误。
|
||||
// 它合并了旧后端的 setup_config 与 get_config:第一阶段用 toml.DecodeFile 暂存 database.config,
|
||||
// 第二阶段依据 driver 用 PrimitiveDecode 分发解码到 SqliteDatabaseConfig 或 MysqlDatabaseConfig。
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
var raw rawConfig
|
||||
meta, err := toml.DecodeFile(path, &raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driver, err := databaseDriverFromString(raw.Database.Driver)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
Web: raw.Web,
|
||||
Others: raw.Others,
|
||||
Database: DatabaseConfig{
|
||||
Driver: driver,
|
||||
},
|
||||
}
|
||||
|
||||
switch driver {
|
||||
case DatabaseDriverSqlite:
|
||||
var s SqliteDatabaseConfig
|
||||
if err := meta.PrimitiveDecode(raw.Database.Config, &s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.Database.Sqlite = &s
|
||||
case DatabaseDriverMysql:
|
||||
var m MysqlDatabaseConfig
|
||||
if err := meta.PrimitiveDecode(raw.Database.Config, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.Database.Mysql = &m
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
@@ -2,6 +2,11 @@ module github.com/yyc12345/coconut-leaf/backend
|
||||
|
||||
go 1.26.2
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.6.0
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
@@ -9,7 +14,6 @@ require (
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/gin-gonic/gin v1.12.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.1 // indirect
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
@@ -7,6 +9,7 @@ github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCc
|
||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
@@ -14,6 +17,8 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
|
||||
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
@@ -24,6 +29,8 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
|
||||
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
@@ -40,6 +47,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
@@ -54,12 +62,16 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
|
||||
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||
@@ -75,4 +87,5 @@ google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aO
|
||||
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -2,11 +2,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := ParseCLI()
|
||||
|
||||
cfg, err := LoadConfig(args.Config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_ = cfg
|
||||
|
||||
// 创建默认的 Gin 引擎(包含 Logger 和 Recovery 中间件)
|
||||
r := gin.Default()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user