2 Star 1 Fork 0

法马智慧/fmgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
零海 提交于 2022-11-23 09:06 . translate方法调整
package configx
import (
"encoding/json"
"fmt"
"gitee.com/fmpt/fmgo/filex"
"gitee.com/fmpt/fmgo/jwtx"
"gitee.com/fmpt/fmgo/logx"
"go.uber.org/zap/zapcore"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
)
// ConfigAll 包含全部配置信息
type ConfigAll struct {
Config
BaseConfig
}
type Config struct {
Name string `json:"name"`
Version string `json:"version"`
Host string `json:"host"`
DataRoot string `json:"data_root"`
RelativeShutdownInfoFilePath string `json:"relative_shutdown_info_file_path"`
RelativeLogPath string `json:"relative_log_path"`
Jwt *jwtx.JwtMould
}
type BaseConfig struct {
sync.RWMutex
Database string `json:"database"`
CenterAddr string `json:"center_addr"`
DfsPort string `json:"dfs_port"`
StreamPort string `json:"stream_port"`
MqUserName string `json:"mqUserName"`
MqPassword string `json:"mqPassword"`
Redis string `json:"redis"`
JaegerAddr string `json:"jaeger_addr"`
RedisPassword string `json:"redis_password"`
UdpPort int32 `json:"udp_port"`
}
func Default() *Config {
return &Config{
DataRoot: ".",
Jwt: jwtx.DefaultJwt(),
}
}
func (c *Config) InitLog(level zapcore.Level) {
localPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
logPath := filepath.Join(localPath, c.RelativeLogPath)
logx.InitLogger(logPath, level)
}
func (c *Config) WriteShutdownPort(port int) error {
portMap := map[string]interface{}{"port": port, "version": c.Version}
buf, err := json.Marshal(portMap)
if err != nil {
return err
}
sdpPath := filepath.Join(c.DataRoot, c.RelativeShutdownInfoFilePath)
err = filex.WriteFile(sdpPath, buf, 0666)
if err != nil {
return err
}
return nil
}
func (c *Config) Listener() {
sign := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sign)
go func() {
for {
select {
case s := <-sign:
switch s {
case syscall.SIGINT, syscall.SIGTERM:
done <- true
}
}
}
}()
strInfo := fmt.Sprintf("service is running ...")
logx.Info(strInfo)
<-done
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fmpt/fmgo.git
git@gitee.com:fmpt/fmgo.git
fmpt
fmgo
fmgo
v1.2.23

搜索帮助