1
0
Files
coconut-leaf/backend/config/config.go

152 lines
4.4 KiB
Go
Raw Normal View History

2026-07-15 11:08:45 +08:00
package config
import (
"fmt"
"github.com/BurntSushi/toml"
)
2026-07-15 10:53:24 +08:00
// 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 (
2026-07-15 10:53:24 +08:00
// DatabaseDriverSqlite means the SQLite database is used.
DatabaseDriverSqlite DatabaseDriver = iota
2026-07-15 10:53:24 +08:00
// DatabaseDriverMysql means the MySQL database is used.
DatabaseDriverMysql
)
2026-07-15 10:53:24 +08:00
// 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":
return DatabaseDriverSqlite, nil
case "mysql":
return DatabaseDriverMysql, nil
default:
return 0, fmt.Errorf("invalid database driver: %q", s)
}
}
2026-07-15 10:53:24 +08:00
// SqliteDatabaseConfig is the connection config for SQLite, mirroring the
// legacy SqliteDatabaseConfig.
type SqliteDatabaseConfig struct {
2026-07-15 10:53:24 +08:00
// Path is the database file path.
Path string `toml:"path"`
}
2026-07-15 10:53:24 +08:00
// MysqlDatabaseConfig is the connection config for MySQL, mirroring the legacy
// MysqlDatabaseConfig.
type MysqlDatabaseConfig struct {
2026-07-15 10:53:24 +08:00
// Host is the database host.
Host string `toml:"host"`
2026-07-15 10:53:24 +08:00
// Port is the database port.
Port int `toml:"port"`
2026-07-15 10:53:24 +08:00
// User is the database user.
User string `toml:"user"`
2026-07-15 10:53:24 +08:00
// Password is the database password.
Password string `toml:"password"`
2026-07-15 10:53:24 +08:00
// Database is the database name.
Database string `toml:"database"`
}
2026-07-15 10:53:24 +08:00
// 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 {
2026-07-15 10:53:24 +08:00
// Driver is the selected database driver.
Driver DatabaseDriver
2026-07-15 10:53:24 +08:00
// Sqlite is non-nil when Driver is DatabaseDriverSqlite.
Sqlite *SqliteDatabaseConfig
2026-07-15 10:53:24 +08:00
// Mysql is non-nil when Driver is DatabaseDriverMysql.
Mysql *MysqlDatabaseConfig
}
2026-07-15 10:53:24 +08:00
// WebConfig is the web server config, mirroring the legacy WebConfig.
type WebConfig struct {
2026-07-15 10:53:24 +08:00
// Port is the web server listening port.
Port int `toml:"port"`
}
2026-07-15 10:53:24 +08:00
// OthersConfig holds miscellaneous options, mirroring the legacy OthersConfig.
type OthersConfig struct {
2026-07-15 10:53:24 +08:00
// Debug indicates whether debug mode is enabled.
Debug bool `toml:"debug"`
2026-07-15 10:53:24 +08:00
// AutoTokenCleanDuration is the interval (in seconds) for auto-cleaning
// outdated tokens.
AutoTokenCleanDuration int `toml:"auto-token-clean-duration"`
}
2026-07-15 10:53:24 +08:00
// Config is the full configuration parsed from the config file, mirroring the
// legacy Config.
type Config struct {
2026-07-15 10:53:24 +08:00
// Database is the database-related config.
Database DatabaseConfig
2026-07-15 10:53:24 +08:00
// Web is the web-server-related config.
Web WebConfig
2026-07-15 10:53:24 +08:00
// Others is the miscellaneous config.
Others OthersConfig
}
2026-07-15 10:53:24 +08:00
// 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"`
}
2026-07-15 10:53:24 +08:00
// 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"`
}
2026-07-15 11:08:45 +08:00
// Load 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
2026-07-15 10:53:24 +08:00
// MysqlDatabaseConfig based on the driver.
2026-07-15 11:08:45 +08:00
func Load(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
}