conf

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 9 Imported by: 0

README

配置文件

  1. 读取配置文件 yaml

  2. 读取配置中心 etcd

  3. 读取配置中心 自研

  4. 读取配置文件 ini

TODO 文档

yaml 配置文件

默认读取 app.yaml

环境变量 RUNMODE 确定配置文件昵称

例如: RUNMODE=prod 就会读取prod.yaml文件

# app相关基础信息
app:
  name: "test_case_1"  # 项目名称
  runType: "dev"  # 运行模式

# http 服务
httpServer:
  open: true
  prod: 12345 # 端口

# grpc 服务
grpcServer:
  open: true
  prod: 12346
  log: true # 打印日志

# grpc 客户端
grpcClient:
  prod: true

# tcp 服务
tcpServer:
  open: true
  prod: 12347

# tcp 客户端
tcpClient:
  prod: 12348

# udp 服务
udpServer:
  open: true
  prod: 12349

# udp 客户端
udpClient:
  prod: 12348

# redis 配置
redis:
  -
    name: "redis1"
    host: "127.0.0.1"
    port: "3306"
    db: 1
    password: ""
    maxIdle: 10  # 最大 Idle 连接
    maxActive: 50 # 最大 活跃 连接

# mysql 配置
mysql:
  -
    dbname: "test"
    user: "root"
    password: "123"
    host: "127.0.0.1"
    port: "3306"


mqType: ""


nsq:
  producer: ""
  consumer: ""

rabbit:
  addr: ""
  user: ""
  password: ""

kafka:
  addr: ""

mongo:
  host: ""
  user: ""
  password: ""

ttf: ""


cluster:
  open: ""
  myAddr: ""
  initCluster: ""


logCentre:
  host: ""
  prod: ""


jwt:
  secret: ""
  expire: ""


minio:
  host: ""
  access: ""
  secret: ""

TODO

  • yaml 配置文件 [ok]
  • ini 配置文件
  • etcd 配置中心
  • 自研配置中心服务, ManGe-ConfigCenter
全局配置对象

var Config


读取yaml配置文件,将配置保存到全局配置对象 Config

func NewConf(appConfigPath string) error


Config 初始化配置对象

func (c *conf) Init() error


Config 获取配置输出整形

func (c *conf) GetInt(key string) int


Config 获取配置

func (c *conf) Get(key string) interface{}


Config 获取配置输出字符串类型

func (c *conf) GetStr(key string) string


Configs 默认配置对象, 通用结构, 参考 上面的 ”yaml 配置文件“
type Configs struct {
	App        *App        `yaml:"app"`
	HttpServer *HttpServer `yaml:"httpServer"`
	GrpcServer *GrpcServer `yaml:"grpcServer"`
	GrpcClient *GrpcClient `yaml:"grpcClient"`
	TcpServer  *TcpServer  `yaml:"tcpServer"`
	TcpClient  *TcpClient  `yaml:"tcpClient"`
	UdpServer  *UdpServer  `yaml:"udpServer"`
	UdpClient  *UdpClient  `yaml:"udpClient"`
	Redis      []*Redis    `yaml:"redis"`
	Mysql      []*Mysql    `yaml:"mysql"`
	MqType     string      `yaml:"mqType"`
	Nsq        *Nsq        `yaml:"nsq"`
	Rabbit     *Rabbit     `yaml:"rabbit"`
	Kafka      *Kafka      `yaml:"kafka"`
	Mongo      []*Mongo    `yaml:"mongo"`
	TTF        string      `yaml:"ttf"`
	Cluster    *Cluster    `yaml:"cluster"`
	LogCentre  *LogCentre  `yaml:"logCentre"`
	Jwt        *Jwt        `yaml:"jwt"`
	Minio      *Minio      `yaml:"minio"`
}

全局默认配置对象 Conf

var Conf *Configs = &Configs{}


读取yaml文件 获取配置, 常用于 func init() 中

func InitConf(path string)

使用

例如配置文件目录结构
./conf/app.yaml
./conf/dev.yaml
./conf/prod.yaml

InitConf("./conf/")


Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = &conf{}

Functions

func InitConf

func InitConf(path string)

InitConf 读取yaml文件 获取配置, 常用于 func init() 中

func InitConfIni

func InitConfIni(path string)

InitConfIni 读取ini文件 获取配置, 常用于 func init() 中

func NewConf

func NewConf(appConfigPath string) error

NewConf 默认yaml, 如果文件是ini 则读取ini文件

func YamlGet

func YamlGet(key string) (interface{}, bool)

YamlGet :: 区分 每一级

func YamlGetInt64

func YamlGetInt64(key string) (int64, error)

func YamlGetString

func YamlGetString(key string) (string, error)

YamlGetString 区分

Types

type App

type App struct {
	Name    string `yaml:"name"`
	RunType string `yaml:"runType"` // 项目昵称
}

App app相关基础信息

type Cluster

type Cluster struct {
	Open        bool   `yaml:"open"`
	MyAddr      string `yaml:"myAddr"`
	InitCluster string `yaml:"initCluster"`
}

Cluster 集群使用 主要用于 ServiceTable

type ConfigType

type ConfigType string
var (
	Yaml ConfigType = "yaml"
	Ini  ConfigType = "ini"
)

type Configs

type Configs struct {
	YamlPath string
	YamlData map[string]interface{}
	Default  *DefaultConf
}
var Conf *Configs = &Configs{}

type DefaultConf

type DefaultConf struct {
	App        *App        `yaml:"app"`
	HttpServer *HttpServer `yaml:"httpServer"`
	GrpcServer *GrpcServer `yaml:"grpcServer"`
	GrpcClient *GrpcClient `yaml:"grpcClient"`
	TcpServer  *TcpServer  `yaml:"tcpServer"`
	TcpClient  *TcpClient  `yaml:"tcpClient"`
	UdpServer  *UdpServer  `yaml:"udpServer"`
	UdpClient  *UdpClient  `yaml:"udpClient"`
	Redis      []*Redis    `yaml:"redis"`
	Mysql      []*Mysql    `yaml:"mysql"`
	MqType     string      `yaml:"mqType"`
	Nsq        *Nsq        `yaml:"nsq"`
	Rabbit     *Rabbit     `yaml:"rabbit"`
	Kafka      *Kafka      `yaml:"kafka"`
	Mongo      []*Mongo    `yaml:"mongo"`
	TTF        string      `yaml:"ttf"`
	Cluster    *Cluster    `yaml:"cluster"`
	LogCentre  *LogCentre  `yaml:"logCentre"`
	Jwt        *Jwt        `yaml:"jwt"`
	Minio      *Minio      `yaml:"minio"`
	Mq         string      `yaml:"mq"`
	User       []*User     `yaml:"user"`
}

type GrpcClient

type GrpcClient struct {
	Prod string `yaml:"prod"`
}

GrpcClient grpc客户端

type GrpcServer

type GrpcServer struct {
	Open bool   `yaml:"open"`
	Prod string `yaml:"prod"`
	Log  bool   `yaml:"log"`
}

GrpcServer grpc服务

type HttpServer

type HttpServer struct {
	Open bool   `yaml:"open"`
	Prod string `yaml:"prod"`
}

HttpServer http服务

type Jwt

type Jwt struct {
	Secret string `yaml:"secret"`
	Expire int    `yaml:"expire"`
}

Jwt jwt配置

type Kafka

type Kafka struct {
	Addr []string `yaml:"addr"`
}

Kafka 消息队列kafka配置

type LogCentre

type LogCentre struct {
	Host string `yaml:"host"`
	Port int    `yaml:"prod"`
}

LogCentre 日志服务收集日志配置

type Minio

type Minio struct {
	Host   string `yaml:"host"`
	Access string `yaml:"access"`
	Secret string `yaml:"secret"`
}

Minio 对象存储 minio配置

type Mongo

type Mongo struct {
	Host     string `yaml:"host"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
}

Mongo mongo配置

type MqType

type MqType struct {
}

MqType 消息队列类型

type Mysql

type Mysql struct {
	DBName   string `yaml:"dbname"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
}

Mysql mysql配置

type Nsq

type Nsq struct {
	Producer string `yaml:"producer"`
	Consumer string `yaml:"consumer"`
}

Nsq 消息队列nsq配置

type Rabbit

type Rabbit struct {
	Addr     string `yaml:"addr"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
}

Rabbit 消息队列rabbit配置

type Redis

type Redis struct {
	Name      string `yaml:"name"` // 自定义一个昵称
	Host      string `yaml:"host"`
	Port      string `yaml:"port"`
	DB        string `yaml:"db"`
	Password  string `yaml:"password"`
	MaxIdle   int    `yaml:"maxIdle"`
	MaxActive int    `yaml:"maxActive"`
}

Redis redis配置

type TcpClient

type TcpClient struct {
	Prod string `yaml:"prod"`
}

TcpClient tcp客户端

type TcpServer

type TcpServer struct {
	Open bool   `yaml:"open"`
	Prod string `yaml:"prod"`
}

TcpServer tcp服务

type UdpClient

type UdpClient struct {
	Prod string `yaml:"prod"`
}

UdpClient udp客户端

type UdpServer

type UdpServer struct {
	Open bool   `yaml:"open"`
	Prod string `yaml:"prod"`
}

UdpServer udp服务

type User

type User struct {
	Name     string `yaml:"name"`
	Password string `yaml:"password"`
}

User 默认用户

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL