130 lines
3.7 KiB
Go
130 lines
3.7 KiB
Go
package config
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
"os"
|
||
|
||
"gopkg.in/yaml.v3"
|
||
)
|
||
|
||
type Config struct {
|
||
Server ServerConfig `yaml:"server"`
|
||
Database DatabaseConfig `yaml:"database"`
|
||
TaskDatabase DatabaseConfig `yaml:"task_database"`
|
||
JWT JWTConfig `yaml:"jwt"`
|
||
APISign APISignConfig `yaml:"api_sign"`
|
||
Log LogConfig `yaml:"log"`
|
||
ES ESConfig `yaml:"es"`
|
||
OCR OCRConfig `yaml:"ocr"`
|
||
ExternalAPI ExternalAPIConfig `yaml:"external_api"`
|
||
Wangdian WangdianConfig `yaml:"wangdian"`
|
||
}
|
||
|
||
type ServerConfig struct {
|
||
Port string `yaml:"port"`
|
||
Host string `yaml:"host"`
|
||
}
|
||
|
||
type DatabaseConfig struct {
|
||
Host string `yaml:"host"`
|
||
Port string `yaml:"port"`
|
||
User string `yaml:"user"`
|
||
Password string `yaml:"password"`
|
||
Name string `yaml:"name"`
|
||
EncryptKey string `yaml:"encrypt_key"`
|
||
}
|
||
|
||
type JWTConfig struct {
|
||
Secret string `yaml:"secret"`
|
||
ExpireHours int `yaml:"expire_hours"`
|
||
}
|
||
|
||
type APISignConfig struct {
|
||
AppKey string `yaml:"app_key"`
|
||
AppSecret string `yaml:"app_secret"`
|
||
SignMethod string `yaml:"sign_method"`
|
||
TimestampTolerance int `yaml:"timestamp_tolerance"`
|
||
ClientId string `yaml:"client_id"`
|
||
}
|
||
|
||
type LogConfig struct {
|
||
MaxAge int `yaml:"max_age"`
|
||
RotateTime int `yaml:"rotate_time"`
|
||
RootPath string `yaml:"root_path"`
|
||
Channel map[string]string `yaml:"channel"`
|
||
}
|
||
|
||
type ESConfig struct {
|
||
Host string `yaml:"host"`
|
||
Index string `yaml:"index"`
|
||
Username string `yaml:"username"`
|
||
Password string `yaml:"password"`
|
||
}
|
||
|
||
type OCRConfig struct {
|
||
ServiceUrl string `yaml:"service_url"`
|
||
ExeUrl string `yaml:"exe_url"`
|
||
}
|
||
|
||
type ExternalAPIConfig struct {
|
||
SyncProductURL string `yaml:"sync_product_url"`
|
||
SyncTaskURL string `yaml:"sync_task_url"`
|
||
SyncTaskBodyURL string `yaml:"sync_task_body_url"`
|
||
ESUpdateBookURL string `yaml:"es_update_book_url"`
|
||
Timeout int `yaml:"timeout"`
|
||
}
|
||
|
||
type WangdianConfig struct {
|
||
Host string `yaml:"host"`
|
||
SandboxHost string `yaml:"sandbox_host"`
|
||
Sandbox bool `yaml:"sandbox"`
|
||
Sid string `yaml:"sid"`
|
||
AppKey string `yaml:"appkey"`
|
||
AppSecret string `yaml:"appsecret"`
|
||
Timeout int `yaml:"timeout"`
|
||
}
|
||
|
||
// GetURL 根据 sandbox 标志和 API 名称构建完整请求地址
|
||
func (c *WangdianConfig) GetURL(apiName string) string {
|
||
host := c.Host
|
||
if c.Sandbox && c.SandboxHost != "" {
|
||
host = c.SandboxHost
|
||
}
|
||
if host == "" {
|
||
host = "api.wangdian.cn"
|
||
}
|
||
return fmt.Sprintf("https://%s/openapi2/%s", host, apiName)
|
||
}
|
||
|
||
var AppConfig *Config
|
||
|
||
func Init() {
|
||
configPath := "config.yaml"
|
||
|
||
data, err := os.ReadFile(configPath)
|
||
if err != nil {
|
||
log.Fatalf("读取配置文件失败: %v", err)
|
||
}
|
||
|
||
AppConfig = &Config{}
|
||
err = yaml.Unmarshal(data, AppConfig)
|
||
if err != nil {
|
||
log.Fatalf("解析配置文件失败: %v", err)
|
||
}
|
||
|
||
log.Println("配置文件加载成功")
|
||
}
|
||
|
||
// GetDSN 返回MySQL连接字符串
|
||
func (c *Config) GetDSN() string {
|
||
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=10s&readTimeout=30s&writeTimeout=30s&interpolateParams=true&multiStatements=true&allowNativePasswords=true&checkConnLiveness=true",
|
||
c.Database.User, c.Database.Password, c.Database.Host, c.Database.Port, c.Database.Name)
|
||
}
|
||
|
||
// GetTaskDSN 返回任务库MySQL连接字符串(腾讯云MySQL)
|
||
func (c *Config) GetTaskDSN() string {
|
||
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=10s&readTimeout=30s&writeTimeout=30s&allowNativePasswords=true",
|
||
c.TaskDatabase.User, c.TaskDatabase.Password, c.TaskDatabase.Host, c.TaskDatabase.Port, c.TaskDatabase.Name)
|
||
}
|