feat: finish project basic structure and core functions
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// LoadConfig reads and parses the configuration file, applies defaults,
|
||||
// and stores it as a global singleton.
|
||||
func LoadConfig(configPath string) (*Config, error) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
|
||||
// Apply defaults for System.
|
||||
if cfg.System.ListenAddr == "" {
|
||||
cfg.System.ListenAddr = "0.0.0.0"
|
||||
}
|
||||
if cfg.System.ListenPort == "" {
|
||||
cfg.System.ListenPort = "8080"
|
||||
}
|
||||
|
||||
// Apply defaults for QQ controller.
|
||||
if cfg.ControllerMethod.QQ.ListenMethod == "" {
|
||||
cfg.ControllerMethod.QQ.ListenMethod = "global"
|
||||
}
|
||||
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 {
|
||||
cfg.ControllerMethod.Email.SMTPPort = 587
|
||||
}
|
||||
if cfg.ControllerMethod.Email.To == nil {
|
||||
cfg.ControllerMethod.Email.To = []string{}
|
||||
}
|
||||
|
||||
// Apply defaults for Ntfy controller.
|
||||
if cfg.ControllerMethod.Ntfy.Server == "" {
|
||||
cfg.ControllerMethod.Ntfy.Server = "https://ntfy.sh"
|
||||
}
|
||||
if cfg.ControllerMethod.Ntfy.Priority == "" {
|
||||
cfg.ControllerMethod.Ntfy.Priority = "default"
|
||||
}
|
||||
|
||||
// Apply defaults for Webhook controller.
|
||||
if cfg.ControllerMethod.Webhook.Method == "" {
|
||||
cfg.ControllerMethod.Webhook.Method = "POST"
|
||||
}
|
||||
if cfg.ControllerMethod.Webhook.Headers == nil {
|
||||
cfg.ControllerMethod.Webhook.Headers = map[string]string{}
|
||||
}
|
||||
|
||||
// Apply defaults for paths.
|
||||
if cfg.DataPath == "" {
|
||||
cfg.DataPath = "./data"
|
||||
}
|
||||
if cfg.DBPath == "" {
|
||||
cfg.DBPath = "./logs.db"
|
||||
}
|
||||
|
||||
// Apply default message templates if not specified.
|
||||
if cfg.ControllerMessage.ServerStatusChanged == "" {
|
||||
cfg.ControllerMessage.ServerStatusChanged = "Server Status Changed Alert\n{{ serverName }} - {{ upStatus }}\nEvent: {{ event }}\nServer Name: {{ serverName }}\nMessage: {{ message }}\nTime: {{ time }}"
|
||||
}
|
||||
if cfg.ControllerMessage.ServerList == "" {
|
||||
cfg.ControllerMessage.ServerList = "All server list:\nOnline:\n{{ list.onlineServers }}\nOffline:\n{{ list.offlineServers }}"
|
||||
}
|
||||
if cfg.ControllerMessage.ServerExecuteResult == "" {
|
||||
cfg.ControllerMessage.ServerExecuteResult = "Command execute result:\nServer Name: {{ serverName }}\nCommand: {{ command }}\n***Result***\n\n{{ result }}\n\n************\nTime: {{ time }}"
|
||||
}
|
||||
|
||||
globalConfig = &cfg
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// GetConfig returns the global configuration singleton.
|
||||
func GetConfig() *Config {
|
||||
return globalConfig
|
||||
}
|
||||
|
||||
// IsDebugMode returns whether debug mode is enabled.
|
||||
func IsDebugMode() bool {
|
||||
if globalConfig == nil {
|
||||
return false
|
||||
}
|
||||
return globalConfig.System.DebugMode
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package config
|
||||
|
||||
// SystemConfig holds system-level configuration.
|
||||
type SystemConfig struct {
|
||||
DebugMode bool `json:"debugMode"`
|
||||
ListenAddr string `json:"listenAddr"`
|
||||
ListenPort string `json:"listenPort"`
|
||||
}
|
||||
|
||||
// KomariAccount holds Komari login credentials.
|
||||
type KomariAccount struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// KomariConfig holds Komari Dashboard connection settings.
|
||||
type KomariConfig struct {
|
||||
DashboardURL string `json:"dashboardURL"`
|
||||
Account KomariAccount `json:"account"`
|
||||
}
|
||||
|
||||
// QQConfig holds QQ (Napcat) Bot controller configuration.
|
||||
type QQConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
URL string `json:"url"`
|
||||
Token string `json:"token"`
|
||||
BotQQID int64 `json:"botQQID"`
|
||||
ListenMethod string `json:"listenMethod"`
|
||||
Admins []string `json:"admins"`
|
||||
TrustedGroups []string `json:"trustedGroups"`
|
||||
}
|
||||
|
||||
// TelegramConfig holds Telegram Bot controller configuration.
|
||||
type TelegramConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
BotToken string `json:"botToken"`
|
||||
ListenMethod string `json:"listenMethod"`
|
||||
Admins []string `json:"admins"`
|
||||
TrustedGroups []string `json:"trustedGroups"`
|
||||
}
|
||||
|
||||
// EmailConfig holds Email notification controller configuration.
|
||||
type EmailConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
SMTPHost string `json:"smtpHost"`
|
||||
SMTPPort int `json:"smtpPort"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
From string `json:"from"`
|
||||
To []string `json:"to"`
|
||||
UseTLS bool `json:"useTLS"`
|
||||
}
|
||||
|
||||
// NtfyConfig holds Ntfy notification controller configuration.
|
||||
type NtfyConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Server string `json:"server"`
|
||||
Topic string `json:"topic"`
|
||||
Token string `json:"token"`
|
||||
Priority string `json:"priority"`
|
||||
}
|
||||
|
||||
// WebhookConfig holds Webhook notification controller configuration.
|
||||
type WebhookConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
URL string `json:"url"`
|
||||
Method string `json:"method"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Template string `json:"template"`
|
||||
}
|
||||
|
||||
// ControllerMethodConfig holds all controller method configurations.
|
||||
type ControllerMethodConfig struct {
|
||||
QQ QQConfig `json:"qq(napcat)"`
|
||||
Telegram TelegramConfig `json:"telegram"`
|
||||
Email EmailConfig `json:"email"`
|
||||
Ntfy NtfyConfig `json:"ntfy"`
|
||||
Webhook WebhookConfig `json:"webhook"`
|
||||
}
|
||||
|
||||
// ControllerMessageConfig holds message templates for controller responses.
|
||||
type ControllerMessageConfig struct {
|
||||
ServerStatusChanged string `json:"SERVER_STATUS_CHANGED"`
|
||||
ServerList string `json:"SERVER_LIST"`
|
||||
ServerExecuteResult string `json:"SERVER_EXECUTE_RESULT"`
|
||||
}
|
||||
|
||||
// Config is the top-level application configuration.
|
||||
type Config struct {
|
||||
System SystemConfig `json:"system"`
|
||||
Komari KomariConfig `json:"komari"`
|
||||
ControllerMethod ControllerMethodConfig `json:"controllerMethod"`
|
||||
ControllerMessage ControllerMessageConfig `json:"controllerMessage"`
|
||||
DataPath string `json:"dataPath"`
|
||||
DBPath string `json:"dbPath"`
|
||||
}
|
||||
|
||||
var globalConfig *Config
|
||||
Reference in New Issue
Block a user