refactor: update go backend with cli and config
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user