1 Star 0 Fork 0

liuxuezhan / mylib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
impl.go 4.89 KB
一键复制 编辑 原始数据 按行查看 历史
liuxuezhan 提交于 2020-09-04 12:57 . Initial commit
package JsonConfig
import (
"errors"
"gopkg.in/square/go-jose.v1/json"
"io/ioutil"
"os"
"strings"
)
const (
configPath = "conf/server.config.json"
)
var (
GlobalConfig *Config
)
//基本配置
type BasicConfig struct {
DebugFlag bool `json:"debug_flag"`
Manual bool `json:"manual"`
UUID string `json:"uuid"`
ServerID uint32 `json:"server_id"`
ServerName string `json:"server_name"`
ServerHost string `json:"server_host"`
Port uint32 `json:"port"`
EnableGM bool `json:"enable_gm"`
EnablePanicOnLockTimeout bool `json:"enable_panic_on_lock_timeout"`
ServerUrl string `json:"server_url"`
ServerInitTime uint64 `json:"server_inittime"`
CanMoveInto bool `json:"canmoveinto"`
RpcPort uint32 `json:"rpc_port"`
UseGzip bool `json:"useGzip"`
GzipMinSize int32 `json:"GzipMinSize"`
NotCheckUid bool `json:"not_check_uid"`
PprofPort string `json:"pprof_port"`
Compensation bool `json:"compensation"`
Version string `json:"version"`
Chat_AppKey string `json:"Chat_AppKey"`
Chat_Secret string `json:"Chat_Secret"`
}
//日志配置
type LogConfig struct {
LogLevel uint32 `json:"log_level"`
ToSTD bool `json:"to_std"`
}
//OSS配置
type OSSConfig struct {
Enable bool `json:"enable"`
Host string `json:"host"`
Port uint32 `json:"port"`
Region string `json:"region"`
Channel string `json:"channel"`
}
//翻译配置
type TranslateServiceConfig struct {
URL string `json:"url"`
Host string `json:"host"`
Port uint32 `json:"port"`
}
//推送配置
type PushServiceConfig struct {
Enable bool `json:"enable"`
URL string `json:"url"`
PopActivity string `json:"pop_activity"`
}
//中心服务器配置
type CenterServerConfig struct {
Host string `json:"host"`
Port uint32 `json:"port"`
}
//数据库配置
type DBConfig struct {
ConnectionString string `json:"connection_string"`
DBName string `json:"db_name"`
}
type UniqueServerConfig struct {
Host string `json:"host"`
Port uint32 `json:"port"`
DB string `json:"db"`
}
type AWSConfig struct {
BucketName string `json:"bucket"`
Folder string `json:"folder"`
Region string `json:"region"`
AWSKey string `json:"aws_access_key_id"`
AWSSkey string `json:"aws_secret_access_key"`
AWSToken string `json:"aws_session_token"`
}
type ActivityServerConfig struct {
Host string `json:"host"`
Port uint32 `json:"port"`
}
type SSDataConf struct {
Enable bool `json:"enable"`
Url string `json:"url"`
Debug bool `json:"debug"`
AppId string `json:"appid"`
}
type LoginServerConf struct {
Url string `json:"url"`
Host string `json:"host"`
Port uint32 `json:"port"`
}
type MailServerConf struct {
Host string `json:"host"`
Port uint32 `json:"port"`
}
type GlobalServerConf struct {
Host string `json:"host"`
Port uint32 `json:"port"`
}
//所有配置
type Config struct {
Basic BasicConfig `json:"basic"`
Log LogConfig `json:"log"`
OSS OSSConfig `json:"oss"`
TranslateService TranslateServiceConfig `json:"translate_service"`
PushService PushServiceConfig `json:"push_service"`
CenterServer CenterServerConfig `json:"center_server"`
UniqueServer UniqueServerConfig `json:"unique_server"`
ActivityServer ActivityServerConfig `json:"activity_server"`
DB DBConfig `json:"db"`
AWS AWSConfig `json:"aws"`
SSData SSDataConf `json:"ssdata"`
LoginServer LoginServerConf `json:"login_server"`
MailServer MailServerConf `json:"mail_server"`
GlobalServer GlobalServerConf `json:"global_server"`
}
//配置文件初始化
func Initialize() {
//加载或新建配置
_, err := os.Stat(configPath)
if err == nil || os.IsExist(err) {
f, err := os.Open(configPath)
if nil != err {
panic(err)
}
bytes, err := ioutil.ReadAll(f)
if nil != err {
panic(err)
}
GlobalConfig = &Config{}
if err := json.Unmarshal(bytes, GlobalConfig); nil != err {
panic(err)
}
} else {
index := strings.LastIndex(configPath, "/")
if -1 != index {
dir := configPath[:index+1]
if err := os.MkdirAll(dir, os.ModeDir); nil != err {
panic(err)
}
}
GlobalConfig = &Config{}
bytes, err := json.Marshal(GlobalConfig)
if nil != err {
panic(err)
}
if err := ioutil.WriteFile(configPath, bytes, os.ModeType); nil != err {
panic(err)
}
}
//检查配置
if GlobalConfig.Basic.Manual { //手动配置模式
if 0 == len(GlobalConfig.Basic.ServerHost) {
panic(errors.New("you should set server host manual"))
}
if 0 == GlobalConfig.Basic.ServerID {
panic(errors.New("you should set server id manual"))
}
return
}
}
1
https://gitee.com/liuxuezhan/mylib.git
git@gitee.com:liuxuezhan/mylib.git
liuxuezhan
mylib
mylib
v1.1.3

搜索帮助