daShangDao_psiServer/config/config.go
2026-06-23 14:26:46 +08:00

118 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
URL string `yaml:"url"`
SandboxURL string `yaml:"sandbox_url"`
Sandbox bool `yaml:"sandbox"`
Sid string `yaml:"sid"`
AppKey string `yaml:"appkey"`
AppSecret string `yaml:"appsecret"`
Timeout int `yaml:"timeout"`
}
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)
}