1 Star 0 Fork 0

寻根 / goweb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 4.55 KB
一键复制 编辑 原始数据 按行查看 历史
lemonzheng(郑刚) 提交于 2024-04-12 15:14 . 代码优化
package webx
import (
"encoding/json"
"fmt"
"gitee.com/xungen/goweb/dbx"
"gitee.com/xungen/goweb/logx"
"gitee.com/xungen/goweb/utils"
"gitee.com/xungen/goweb/yaml"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
)
type AppConfig struct {
RouteConfig
Id int
Name string
Path string
Route RouteConfig
}
type LogConfig struct {
RouteConfig
Path string
Level int
Maxsize int
}
type RedisConfig struct {
RouteConfig
Password string
}
type RouteConfig struct {
Port int
Host string
}
func (obj RouteConfig) String() string {
return fmt.Sprintf("%s:%d", obj.Host, obj.Port)
}
type Config struct {
App AppConfig
Log LogConfig
Redis RedisConfig
Datebase dbx.Config
}
var seqno uint64
var config *Config
var configpath string
var sequencehead string
var remoteconfigmap sync.Map
var confignode yaml.YAMLNode
func GetConfig() *Config {
return config
}
func GetConfigPath() string {
return configpath
}
func CreateSequence() string {
idx := atomic.AddUint64(&seqno, 1) % 100000000
return sequencehead + fmt.Sprintf("%09d", idx)
}
func ClearRemoteConfig(name string) {
if name == "" {
remoteconfigmap.Range(func(key, value interface{}) bool {
remoteconfigmap.Delete(key)
return true
})
} else {
remoteconfigmap.Delete(name)
}
}
func LoadConfig(path string) *Config {
if config == nil {
cfg := &Config{}
if confignode.Load(path) {
confignode.Object(cfg, true)
if cfg.App.Host == "" {
cfg.App.Host = "0.0.0.0"
}
if cfg.App.Name == "" {
cfg.App.Name = "cppweb.go"
}
if cfg.App.Path == "" || cfg.App.Path == "." || cfg.App.Path == "./" {
cfg.App.Path = utils.GetCurrentPath()
}
if cfg.Log.Path == "" || cfg.Log.Path == "." || cfg.Log.Path == "./" {
cfg.Log.Path = utils.GetCurrentPath() + "/log"
}
os.Setenv("WEBAPP_ROOT_PATH", cfg.App.Path)
checkStartSequence(cfg.App.Id, cfg.App.Path)
logx.Instance().Init(cfg.Log.Path, cfg.Log.Level, cfg.Log.Maxsize)
configpath = path
config = cfg
publish(confignode.Get("app.dir"), false)
publish(confignode.Get("app.url"), false)
publish(confignode.Get("app.zip"), true)
}
if config == nil {
panic("load config failed")
}
}
return config
}
func SyncRemoteConfig(typ string, name string) {
switch typ {
case "confile":
ClearRemoteConfig(name)
case "database":
dbx.Clear(name)
}
}
func GetRemoteConfig(ctx *Context, name string) (*yaml.YAMLNode, error) {
if res, ok := remoteconfigmap.Load(name); ok {
if cfg, ok := res.(*yaml.YAMLNode); ok {
return cfg, nil
} else {
return nil, res.(error)
}
}
type Result struct {
Code int `json:"code"`
Content string `json:"content"`
}
var host = GetRouteHost()
if host == nil {
return nil, utils.SYSERR.Copy("route center config missing")
}
var res Result
var link = fmt.Sprintf("http://%s:%d/confile/sharenote?flag=Q&title=%s", host.Host, host.Port, name)
if buff := GetHttpResult(ctx, link); len(buff) == 0 {
ctx.Error("grasp remote config[%s] failed", name)
return nil, utils.NETERR.Copy("grasp remote config failed")
} else if err := json.Unmarshal(buff, &res); err != nil {
ctx.Error("grasp remote config[%s] failed[%s]", name, err.Error())
return nil, utils.DATAERR.Copy(err.Error())
} else if res.Code == utils.NOTFOUND.Code() {
err = utils.NOTFOUND.Copy("remote config not found")
ctx.Error("remote config[%s] not found", name)
remoteconfigmap.Store(name, err)
return nil, err
} else if res.Code < 0 {
ctx.Error("grasp remote config[%s] failed[%s]", name, string(buff))
return nil, utils.ERROR
} else {
var cfg yaml.YAMLNode
cfg.Parse(res.Content)
remoteconfigmap.Store(name, &cfg)
return &cfg, nil
}
}
func getAbsPath(path string) string {
if filepath.IsAbs(path) {
return path
} else {
return config.App.Path + "/" + path
}
}
func checkStartSequence(id int, path string) {
sequencehead = fmt.Sprintf("%03d%03d", id%1000, time.Now().Unix()/60%1000)
}
func publish(node *yaml.YAMLNode, zipfile bool) int {
num := 0
if node == nil {
return num
}
for _, v := range node.Children() {
src := v.Value()
dest := v.Name()
if src = os.ExpandEnv(src); len(src) > 0 {
if zipfile {
PublishZip(getAbsPath(src), dest)
} else {
Publish(getAbsPath(src), dest)
}
num++
}
}
return num
}
func tryUpdateConfig(log *LogConfig, redis *RedisConfig) {
if log != nil && config.Log.Host == "" {
config.Log.Host = log.Host
config.Log.Port = log.Port
}
if redis != nil && config.Redis.Host == "" {
config.Redis.Host = redis.Host
config.Redis.Port = redis.Port
config.Redis.Password = redis.Password
}
}
Go
1
https://gitee.com/xungen/goweb.git
git@gitee.com:xungen/goweb.git
xungen
goweb
goweb
v0.0.8

搜索帮助