1 Star 0 Fork 0

Darwin / geoRPC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
baoyx 提交于 2024-02-01 14:41 . publish release v10.0.
package conf
import (
"context"
"gitee.com/byx_darwin/micro-extend/pkg/config/kitex_conf"
"gitee.com/byx_darwin/uptrace-opentelemetry/log"
"github.com/bytedance/go-tagexpr/v2/validator"
"github.com/cloudwego/kitex/pkg/klog"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"sync"
)
var (
conf *Config
once sync.Once
)
type Config struct {
Service kitex_conf.Service `json:"service" yaml:"service"`
Server kitex_conf.Server `json:"server" yaml:"server"`
Logger log.Config `json:"logger" yaml:"logger"`
GeoOption GeoOption `json:"geo_option" yaml:"geo_option"`
}
type GeoOption struct {
Language string `json:"language" yaml:"language"`
Ip2RegionPath string `json:"ip2_region_path" yaml:"ip2_region_path"`
GeoIPCityPath string `json:"geoip_city_path" yaml:"geoip_city_path"`
GeoIPIspPath string `json:"geoip_isp_path" yaml:"geoip_isp_path"`
CacheSize int `json:"cache_size" yaml:"cache_size"`
}
// GetConf gets configuration instance
func GetConf() *Config {
once.Do(initConf)
return conf
}
func initConf() {
prefix := "conf"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := os.ReadFile(confFileRelPath)
ctx := context.Background()
if err != nil {
klog.CtxFatalf(ctx, "read conf.yaml error - %v", err)
panic(err)
}
conf = new(Config)
err = yaml.Unmarshal(content, conf)
if err != nil {
klog.CtxFatalf(ctx, "parse yaml error - %v", err)
}
if err := validator.Validate(conf); err != nil {
klog.Error("validate config error - %v", err)
panic(err)
}
}
func GetEnv() string {
e := os.Getenv("GO_ENV")
if len(e) == 0 {
return "dev"
}
return e
}
Go
1
https://gitee.com/byx_darwin/geo-rpc.git
git@gitee.com:byx_darwin/geo-rpc.git
byx_darwin
geo-rpc
geoRPC
022324c0c4aa

搜索帮助

53164aa7 5694891 3bd8fe86 5694891