This commit is contained in:
zhaoxiaorong
2025-02-07 13:01:38 +08:00
parent ebcdfe1ee8
commit 57a0d8ae81
52 changed files with 3313 additions and 0 deletions

92
conf/new.go Normal file
View File

@@ -0,0 +1,92 @@
package conf
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"git.apinb.com/bsm-sdk/core/env"
"git.apinb.com/bsm-sdk/core/print"
"git.apinb.com/bsm-sdk/core/vars"
"golang.org/x/exp/rand"
yaml "gopkg.in/yaml.v3"
)
func New(srvKey string, cfg any) *Runtime {
var run Runtime
// 设置服务键
vars.ServiceKey = srvKey
// 获取主机名
vars.HostName, _ = os.Hostname()
// 获取根目录路径,优先使用环境变量设置的路径,如果未设置则使用当前工作目录
rootDir := strings.ToLower(env.GetEnvDefault("CORE_Path", ""))
if rootDir == "" {
rootDir, _ = os.Getwd()
}
// 获取运行模式,如果环境变量未设置,则默认为"dev"
run.Mode = strings.ToLower(env.GetEnvDefault("CORE_Mode", "dev"))
// 获取JWT密钥用于身份验证
run.JwtKey = strings.ToLower(env.GetEnvDefault("CORE_JwtKey", ""))
// 获取许可证路径,如果环境变量未设置,则默认在根目录下的"etc"文件夹
run.LicencePath = strings.ToLower(env.GetEnvDefault("CORE_LicencePath", ""))
if run.LicencePath == "" {
run.LicencePath = filepath.Join(rootDir, "etc")
}
// 如果JWT密钥未设置则记录错误并终止程序
if run.JwtKey == "" {
log.Fatalf("ENV: CORE_JwtKey Not Nil !")
}
// 构造配置文件路径,输出配置文件信息
cfp := fmt.Sprintf("%s_%s.yaml", srvKey, run.Mode)
cfp = filepath.Join(rootDir, "etc", cfp)
print.Info("[CORE - %s] Config File: %s", srvKey, cfp)
// 读取配置文件内容
yamlFile, err := os.ReadFile(cfp)
if err != nil {
log.Fatalf("ERROR: %v", err)
}
// 检查配置文件中是否存在Service和Addr字段
if !strings.Contains(string(yamlFile), "Service:") {
log.Fatalln("ERROR: Service Not Nil", cfp)
}
if !strings.Contains(string(yamlFile), "Port:") {
log.Fatalln("ERROR: Port Not Nil", cfp)
}
// 解析YAML
err = yaml.Unmarshal(yamlFile, cfg)
if err != nil {
log.Fatalf("ERROR: %v", err)
}
return &run
}
func NotNil(values ...string) {
for _, value := range values {
if strings.TrimSpace(value) == "" {
log.Fatalln("ERROR:Must config not nil")
}
}
}
func CheckPort(port int) int {
if port <= 0 || port >= 65535 {
rand.Seed(uint64(time.Now().UnixNano()))
return rand.Intn(65535-1024) + 1024 // 生成1024到65535之间的随机端口
}
return port
}

62
conf/types.go Normal file
View File

@@ -0,0 +1,62 @@
package conf
type Base struct {
Service string `yaml:"Service"` // 服务名称
Port int `yaml:"Port"` // 服务监听端口,0为自动随机端口
Cache string `yaml:"Cache"` // REDIS缓存
OnMicroService bool `yaml:"OnMicroService"` // 是否启用微服务
SecretKey string `yaml:"SecretKey"` // 服务秘钥
}
type DBConf struct {
Driver string `yaml:"Driver"` // 数据库驱动mysql,sqlite,postgres,sqlserver,dm,kingbase
Source []string `yaml:"Source"` // 数据库连接
}
type ApmConf struct {
Name string // APM服务名称
Platform string `yaml:"Platform"` // APM平台apm,skywalking
Endpoint string `yaml:"Endpoint"` // APM服务接入地址
}
type EtcdConf struct {
Endpoints []string `yaml:"Endpoints"` // etcd服务接入地址
Passwd *PasswdConf `yaml:"Passwd"` // etcd账号密码
TLS *TlsConf `yaml:"TLS"` // tls 安全连接
}
type PasswdConf struct {
Account string `yaml:"Account"` // etcd账号
Password string `yaml:"Password"` // etcd密码
}
type RpcConf struct {
Key string `yaml:"Key"` // 微服务名称
Endpoint string `yaml:"Endpoint"` // 服务接入地址
SecretKey string `yaml:"SecretKey"` // 秘钥
}
type OssConf struct {
Site string `yaml:"Site"` // oss站点HOST
Endpoint string `yaml:"Endpoint"` // oss服务接入地址
Region string `yaml:"Region"` // oss服务区域
AccessKeyID string `yaml:"AccessKeyId"` // oss AccessKeyId
AccessKeySecret string `yaml:"AccessKeySecret"` // oss AccessKeySecret
}
type MqConf struct {
Endpoints []string `yaml:"Endpoints"` // MQ服务接入地址
Space string `yaml:"Space"` // MQ服务空间
}
type Runtime struct {
Mode string // 运行模式dev,test,prod
JwtKey string // JWT密钥
LicencePath string // Licence文件路径
}
type TlsConf struct {
CaFile string // CA文件路径
CertFile string // 证书文件路径
KeyFile string // 密钥文件路径
}