chore(config): move admins && trustedGroups to a new config bot_user_config.json.
feat(config): add load bot_user_config endpoint style(config): rename `LoadConfig` to `LoadGlobalConfig`.
This commit is contained in:
+24
-20
@@ -6,9 +6,9 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// LoadConfig reads and parses the configuration file, applies defaults,
|
||||
// LoadGlobalConfig reads and parses the configuration file, applies defaults,
|
||||
// and stores it as a global singleton.
|
||||
func LoadConfig(configPath string) (*Config, error) {
|
||||
func LoadGlobalConfig(configPath string) (*Config, error) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
@@ -37,23 +37,11 @@ func LoadConfig(configPath string) (*Config, error) {
|
||||
if cfg.ControllerMethod.QQ.NapcatPort == "" {
|
||||
cfg.ControllerMethod.QQ.NapcatPort = "3000"
|
||||
}
|
||||
if cfg.ControllerMethod.QQ.Admins == nil {
|
||||
cfg.ControllerMethod.QQ.Admins = []string{}
|
||||
}
|
||||
if cfg.ControllerMethod.QQ.TrustedGroups == nil {
|
||||
cfg.ControllerMethod.QQ.TrustedGroups = []string{}
|
||||
}
|
||||
|
||||
// Apply defaults for Telegram controller.
|
||||
if cfg.ControllerMethod.Telegram.ListenMethod == "" {
|
||||
cfg.ControllerMethod.Telegram.ListenMethod = "global"
|
||||
}
|
||||
if cfg.ControllerMethod.Telegram.Admins == nil {
|
||||
cfg.ControllerMethod.Telegram.Admins = []string{}
|
||||
}
|
||||
if cfg.ControllerMethod.Telegram.TrustedGroups == nil {
|
||||
cfg.ControllerMethod.Telegram.TrustedGroups = []string{}
|
||||
}
|
||||
|
||||
// Apply defaults for Email controller.
|
||||
if cfg.ControllerMethod.Email.SMTPPort == 0 {
|
||||
@@ -107,19 +95,35 @@ func LoadConfig(configPath string) (*Config, error) {
|
||||
cfg.ControllerMessage.ServerExecuteResult = "Command execute result:\nServer Name: {{ serverName }}\nCommand: {{ command }}\n***Result***\n\n{{ result }}\n\n************\nTime: {{ time }}"
|
||||
}
|
||||
|
||||
globalConfig = &cfg
|
||||
C_globalConfig = &cfg
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// GetConfig returns the global configuration singleton.
|
||||
func GetConfig() *Config {
|
||||
return globalConfig
|
||||
// GetGlobalConfig returns the global configuration singleton.
|
||||
func GetGlobalConfig() *Config {
|
||||
return C_globalConfig
|
||||
}
|
||||
|
||||
// LoadBotUserConfig reads and parses bot_user_config.json and stores it as the
|
||||
// global C_botUserConfig singleton, mirroring LoadGlobalConfig.
|
||||
func LoadBotUserConfig(configPath string) (*BotUserConfig, error) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read bot user config file: %w", err)
|
||||
}
|
||||
|
||||
var cfg BotUserConfig
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse bot user config file: %w", err)
|
||||
}
|
||||
C_botUserConfig = &cfg
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// IsDebugMode returns whether debug mode is enabled.
|
||||
func IsDebugMode() bool {
|
||||
if globalConfig == nil {
|
||||
if C_globalConfig == nil {
|
||||
return false
|
||||
}
|
||||
return globalConfig.System.DebugMode
|
||||
return C_globalConfig.System.DebugMode
|
||||
}
|
||||
|
||||
+61
-18
@@ -1,5 +1,7 @@
|
||||
package config
|
||||
|
||||
import "sort"
|
||||
|
||||
// SystemConfig holds system-level configuration.
|
||||
type SystemConfig struct {
|
||||
DebugMode bool `json:"debugMode"`
|
||||
@@ -32,25 +34,21 @@ type KomariConfig struct {
|
||||
|
||||
// QQConfig holds QQ (Napcat) Bot controller configuration.
|
||||
type QQConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
NetworkUseProxy bool `json:"networkUseProxy"`
|
||||
NapcatAddr string `json:"napcatAddr"`
|
||||
NapcatPort string `json:"napcatPort"`
|
||||
NapcatToken string `json:"napcatToken"`
|
||||
BotQQID int64 `json:"botQQID"`
|
||||
ListenMethod string `json:"listenMethod"`
|
||||
Admins []string `json:"admins"`
|
||||
TrustedGroups []string `json:"trustedGroups"`
|
||||
Enabled bool `json:"enabled"`
|
||||
NetworkUseProxy bool `json:"networkUseProxy"`
|
||||
NapcatAddr string `json:"napcatAddr"`
|
||||
NapcatPort string `json:"napcatPort"`
|
||||
NapcatToken string `json:"napcatToken"`
|
||||
BotQQID int64 `json:"botQQID"`
|
||||
ListenMethod string `json:"listenMethod"`
|
||||
}
|
||||
|
||||
// TelegramConfig holds Telegram Bot controller configuration.
|
||||
type TelegramConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
NetworkUseProxy bool `json:"networkUseProxy"`
|
||||
BotToken string `json:"botToken"`
|
||||
ListenMethod string `json:"listenMethod"`
|
||||
Admins []string `json:"admins"`
|
||||
TrustedGroups []string `json:"trustedGroups"`
|
||||
Enabled bool `json:"enabled"`
|
||||
NetworkUseProxy bool `json:"networkUseProxy"`
|
||||
BotToken string `json:"botToken"`
|
||||
ListenMethod string `json:"listenMethod"`
|
||||
}
|
||||
|
||||
// EmailConfig holds Email notification controller configuration.
|
||||
@@ -98,7 +96,7 @@ type ControllerMethodConfig struct {
|
||||
// ControllerMessageConfig holds message templates for controller responses.
|
||||
type ControllerMessageConfig struct {
|
||||
BotStarted string `json:"BOT_STARTED"`
|
||||
BotHelp string `json:"BOT_HELP"`
|
||||
BotHelp string `json:"BOT_HELP"`
|
||||
Tg_BotStart string `json:"TG_BOT_START"`
|
||||
ServerStatusChanged string `json:"SERVER_STATUS_CHANGED"`
|
||||
ServerList string `json:"SERVER_LIST"`
|
||||
@@ -108,7 +106,7 @@ type ControllerMessageConfig struct {
|
||||
// Config is the top-level application configuration.
|
||||
type Config struct {
|
||||
System SystemConfig `json:"system"`
|
||||
Debug DebugConfig `json:"debug"`
|
||||
Debug DebugConfig `json:"debug"`
|
||||
Komari KomariConfig `json:"komari"`
|
||||
ControllerMethod ControllerMethodConfig `json:"controllerMethod"`
|
||||
ControllerMessage ControllerMessageConfig `json:"controllerMessage"`
|
||||
@@ -116,4 +114,49 @@ type Config struct {
|
||||
DBPath string `json:"dbPath"`
|
||||
}
|
||||
|
||||
var globalConfig *Config
|
||||
var C_globalConfig *Config
|
||||
|
||||
// BotUserOptions holds per-member options stored in bot_user_config.json.
|
||||
type BotUserOptions struct {
|
||||
// EventStatusNotify indicates whether this member is subscribed to node
|
||||
// status change notifications.
|
||||
EventStatusNotify bool `json:"event_status_notify"`
|
||||
}
|
||||
|
||||
// BotUserMembers maps a member ID (QQ number, Telegram @username or numeric
|
||||
// user ID, or chat/group ID) to its per-member options.
|
||||
type BotUserMembers map[string]BotUserOptions
|
||||
|
||||
// IDs returns the member IDs as a sorted slice. JSON object keys have no
|
||||
// stable iteration order once decoded into a map, so the result is sorted to
|
||||
// keep notifications and authorization checks deterministic.
|
||||
func (m BotUserMembers) IDs() []string {
|
||||
ids := make([]string, 0, len(m))
|
||||
for id := range m {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return ids
|
||||
}
|
||||
|
||||
// BotUser_QQConfig holds the admins and trusted groups for the QQ (Napcat) bot.
|
||||
type BotUser_QQConfig struct {
|
||||
Admins BotUserMembers `json:"admins"`
|
||||
TrustedGroups BotUserMembers `json:"trustedGroups"`
|
||||
}
|
||||
|
||||
// BotUser_TelegramConfig holds the admins and trusted groups for the Telegram bot.
|
||||
type BotUser_TelegramConfig struct {
|
||||
Admins BotUserMembers `json:"admins"`
|
||||
TrustedGroups BotUserMembers `json:"trustedGroups"`
|
||||
}
|
||||
|
||||
// BotUserConfig is the schema of bot_user_config.json, which lists the admins
|
||||
// and trusted groups (chat targets) for each bot channel separately from the
|
||||
// main config.json.
|
||||
type BotUserConfig struct {
|
||||
QQ BotUser_QQConfig `json:"qq(napcat)"`
|
||||
Telegram BotUser_TelegramConfig `json:"telegram"`
|
||||
}
|
||||
|
||||
var C_botUserConfig *BotUserConfig
|
||||
|
||||
Reference in New Issue
Block a user