blog_backend_api/config/config.go

152 lines
3.1 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package config
2021-03-04 13:21:12 +00:00
import (
2019-03-07 02:36:09 +00:00
"log"
2019-01-25 09:11:15 +00:00
"os"
"runtime"
2021-03-04 13:21:12 +00:00
2021-08-15 16:19:25 +00:00
wsConf "tcptemplate/config"
2021-03-04 13:21:12 +00:00
"gopkg.in/yaml.v2"
2019-01-25 09:11:15 +00:00
)
var ostype = runtime.GOOS
var conf ConfAPI
type ConfAPI struct {
2019-06-23 11:33:45 +00:00
ListenSvr int `yaml:"listen_svr"` // 服务监听端口
ListenApi int `yaml:"listen_api"` // 服务监听端口
RunMode string `yaml:"runmode"` // 服务运行模式
MaxConn int `yaml:"max_conn"`
Logs LogConfig `yaml:"logs"` // 日志
Redis1 EntityRedis `yaml:"redis1"`
Mysql MysqlConfig `yaml:"mysql"` // 认证配置
2020-03-13 09:40:00 +00:00
ElasticSerach ElasticSearch `yaml:"elasticsearch"`
2019-05-16 10:05:20 +00:00
Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置
2019-06-23 11:33:45 +00:00
MongoConf MongoConfig `yaml:"mongo"`
2021-08-15 16:19:25 +00:00
WsServerConf wsConf.Config `yaml:"ws_server_conf"`
2019-06-30 14:59:00 +00:00
CaCert string `yaml:"ca_cert"`
ClientCert string `yaml:"client_cert"`
ClientKey string `yaml:"client_key"'`
init bool
2019-01-25 09:11:15 +00:00
}
2020-03-13 09:40:00 +00:00
type ElasticSearch struct{
2020-03-25 02:57:25 +00:00
Address string `yaml:"address"`
2021-02-08 02:16:24 +00:00
User string `yaml:"user"`
Password string `yaml:"password"`
2020-03-13 09:40:00 +00:00
}
2019-01-25 09:11:15 +00:00
2019-01-31 08:57:22 +00:00
type EntityRedis struct {
Addr string `yaml:"addr"`
Pwd string `yaml:"password"`
DB int `yaml:"db"`
PoolSize int `yaml:"poolsize"`
}
2019-01-25 09:11:15 +00:00
type RedisConfig struct {
Addr string `yaml:"addr"`
Pwd string `yaml:"password"`
DB int64 `yaml:"db"`
}
type LogConfig struct {
Dir string `yaml:"dir"`
File string `yaml:"file"`
Level int `yaml:"level"`
SaveFile bool `yaml:"savefile"`
}
type MysqlConfig struct {
Addr string `yaml:"addr"`
UserName string `yaml:"user"`
Password string `yaml:"password"`
Db string `yaml:"db"`
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
}
2019-06-23 11:33:45 +00:00
// mongodb://myuser:mypass@localhost:40001
type MongoConfig struct {
Addr string `yaml:"addr"`
Port int64 `yaml:"port"`
UserName string `yaml:"user"`
Password string `yaml:"password"`
Db string `yaml:"db"`
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
}
2019-03-07 02:36:09 +00:00
2019-01-25 09:11:15 +00:00
var gConf ConfAPI
2019-03-07 02:36:09 +00:00
2020-01-25 14:26:41 +00:00
func ApiConfig() ConfAPI{
return gConf
}
2019-01-25 09:11:15 +00:00
func Init(path string) error {
2019-03-07 02:36:09 +00:00
file, e := os.Open(path)
if nil != e {
2019-01-25 09:11:15 +00:00
log.Println(e.Error())
return e
}
2019-03-07 02:36:09 +00:00
stat, _ := file.Stat()
filec := make([]byte, stat.Size())
2021-08-15 16:19:25 +00:00
log.Print(filec)
2019-01-25 09:11:15 +00:00
file.Read(filec)
2019-03-07 02:36:09 +00:00
e = yaml.Unmarshal(filec, &gConf)
if nil != e {
2019-01-25 09:11:15 +00:00
log.Println(e.Error())
}
gConf.init = true
log.Print(gConf)
2019-01-25 09:11:15 +00:00
return nil
}
func GetPort() int {
2019-03-07 02:36:09 +00:00
if gConf.init {
2019-01-25 09:11:15 +00:00
return gConf.ListenApi
2019-03-07 02:36:09 +00:00
} else {
return 8001
2019-01-25 09:11:15 +00:00
}
}
2019-03-07 02:36:09 +00:00
func GetMysqlConfig() *MysqlConfig {
if gConf.init {
2019-01-25 09:11:15 +00:00
return &gConf.Mysql
2019-03-07 02:36:09 +00:00
} else {
return nil
2019-01-25 09:11:15 +00:00
}
2019-01-29 13:57:53 +00:00
}
2019-05-16 10:05:20 +00:00
func GetMysqlBlogConfig() *MysqlConfig {
if gConf.init {
return &gConf.Mysql1
} else {
return nil
}
}
2019-06-23 11:33:45 +00:00
func GETCaCert() string{
return gConf.CaCert
}
func GetMongoDBConfig() *MongoConfig{
if gConf.init{
return &gConf.MongoConf
} else {
return nil
}
}
2019-01-31 08:57:22 +00:00
func GetRedis1() *EntityRedis {
2019-03-07 02:36:09 +00:00
if gConf.init {
2019-01-31 08:57:22 +00:00
return &gConf.Redis1
2019-03-07 02:36:09 +00:00
} else {
return nil
2019-01-31 08:57:22 +00:00
}
}
2019-03-07 02:36:09 +00:00
func GetLogConfig() *LogConfig {
if gConf.init {
2019-01-29 13:57:53 +00:00
return &gConf.Logs
2019-03-07 02:36:09 +00:00
} else {
2019-01-29 13:57:53 +00:00
return nil
}
2019-01-31 08:57:22 +00:00
}
2019-06-23 11:33:45 +00:00
2020-03-13 09:40:00 +00:00
func GetElkConfig() *ElasticSearch{
if gConf.init{
return &gConf.ElasticSerach
}else {
return nil
}
}