1
0

refactor: migrate utils in backend

This commit is contained in:
2026-07-15 10:53:24 +08:00
parent b3efb196ed
commit a8de650a5a
5 changed files with 150 additions and 42 deletions

View File

@@ -6,19 +6,20 @@ import (
"github.com/BurntSushi/toml"
)
// DatabaseDriver 表示配置文件中指定的数据库驱动类型,对应旧后端的 DatabaseDriver(StrEnum)。
// 用 const 配合 iota 模拟数值枚举,字符串形式由 databaseDriverFromString 转换。
// DatabaseDriver represents the database driver specified in the config file,
// mirroring the legacy DatabaseDriver(StrEnum). It is a numeric enum simulated
// with const + iota; its string form is produced by databaseDriverFromString.
type DatabaseDriver int
const (
// DatabaseDriverSqlite 表示使用 SQLite 数据库。
// DatabaseDriverSqlite means the SQLite database is used.
DatabaseDriverSqlite DatabaseDriver = iota
// DatabaseDriverMysql 表示使用 MySQL 数据库。
// DatabaseDriverMysql means the MySQL database is used.
DatabaseDriverMysql
)
// databaseDriverFromString 将配置文件中的 driver 字符串转换为 DatabaseDriver
// 无法识别的字符串将返回错误。
// databaseDriverFromString converts a driver string from the config file into a
// DatabaseDriver. An unrecognized string returns an error.
func databaseDriverFromString(s string) (DatabaseDriver, error) {
switch s {
case "sqlite":
@@ -30,78 +31,87 @@ func databaseDriverFromString(s string) (DatabaseDriver, error) {
}
}
// SqliteDatabaseConfig 是 SQLite 数据库的连接配置,对应旧后端 SqliteDatabaseConfig。
// SqliteDatabaseConfig is the connection config for SQLite, mirroring the
// legacy SqliteDatabaseConfig.
type SqliteDatabaseConfig struct {
// Path 是数据库文件的路径。
// Path is the database file path.
Path string `toml:"path"`
}
// MysqlDatabaseConfig 是 MySQL 数据库的连接配置,对应旧后端 MysqlDatabaseConfig。
// MysqlDatabaseConfig is the connection config for MySQL, mirroring the legacy
// MysqlDatabaseConfig.
type MysqlDatabaseConfig struct {
// Host 是数据库主机地址。
// Host is the database host.
Host string `toml:"host"`
// Port 是数据库端口。
// Port is the database port.
Port int `toml:"port"`
// User 是数据库用户名。
// User is the database user.
User string `toml:"user"`
// Password 是数据库密码。
// Password is the database password.
Password string `toml:"password"`
// Database 是数据库名称。
// Database is the database name.
Database string `toml:"database"`
}
// DatabaseConfig 描述数据库驱动及其对应的具体配置,对应旧后端 DatabaseConfig。
// 其中 Driver 起判别作用,决定 Sqlite 或 Mysql 中的哪一个被填充。
// DatabaseConfig describes the database driver and its concrete config, mirroring
// the legacy DatabaseConfig. Driver is the discriminator deciding whether Sqlite
// or Mysql is populated.
type DatabaseConfig struct {
// Driver 是当前选择的数据库驱动。
// Driver is the selected database driver.
Driver DatabaseDriver
// Sqlite Driver DatabaseDriverSqlite 时非空。
// Sqlite is non-nil when Driver is DatabaseDriverSqlite.
Sqlite *SqliteDatabaseConfig
// Mysql Driver DatabaseDriverMysql 时非空。
// Mysql is non-nil when Driver is DatabaseDriverMysql.
Mysql *MysqlDatabaseConfig
}
// WebConfig 是 Web 服务器的配置,对应旧后端 WebConfig
// WebConfig is the web server config, mirroring the legacy WebConfig.
type WebConfig struct {
// Port 是 Web 服务器监听端口。
// Port is the web server listening port.
Port int `toml:"port"`
}
// OthersConfig 是其余杂项配置,对应旧后端 OthersConfig
// OthersConfig holds miscellaneous options, mirroring the legacy OthersConfig.
type OthersConfig struct {
// Debug 指示是否启用调试模式。
// Debug indicates whether debug mode is enabled.
Debug bool `toml:"debug"`
// AutoTokenCleanDuration 是自动清理过期 token 的时间间隔(秒)。
// AutoTokenCleanDuration is the interval (in seconds) for auto-cleaning
// outdated tokens.
AutoTokenCleanDuration int `toml:"auto-token-clean-duration"`
}
// Config 是从配置文件解析得到的完整配置,对应旧后端 Config。
// Config is the full configuration parsed from the config file, mirroring the
// legacy Config.
type Config struct {
// Database 是数据库相关配置。
// Database is the database-related config.
Database DatabaseConfig
// Web 是 Web 服务器相关配置。
// Web is the web-server-related config.
Web WebConfig
// Others 是其余杂项配置。
// Others is the miscellaneous config.
Others OthersConfig
}
// rawDatabaseConfig 是仅用于 TOML 解析的内部结构。
// Config 字段以 toml.Primitive 暂存,以便依据 Driver 做两段式分发解码。
// rawDatabaseConfig is an internal struct used only for TOML decoding. Its
// Config field is stored as a toml.Primitive so it can be dispatched and decoded
// in two phases based on Driver.
type rawDatabaseConfig struct {
Driver string `toml:"driver"`
Config toml.Primitive `toml:"config"`
}
// rawConfig 是仅用于 TOML 解析的内部结构,配合两段式解码使用。
// rawConfig is an internal struct used only for TOML decoding, in tandem with
// the two-phase decode.
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。
// LoadConfig reads and parses the TOML config file at the given path, returning
// the parsed config or an error. It merges the legacy setup_config and
// get_config: the first phase uses toml.DecodeFile to defer database.config, and
// the second phase dispatches PrimitiveDecode into SqliteDatabaseConfig or
// MysqlDatabaseConfig based on the driver.
func LoadConfig(path string) (*Config, error) {
var raw rawConfig
meta, err := toml.DecodeFile(path, &raw)