goconfig

package module
v0.0.0-...-316c293 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: Apache-2.0 Imports: 16 Imported by: 3

README

go-config

介绍

go 开发中常用的一些配置

安装
go get -u github.com/goworkeryyt/go-config
例子

默认的程序根目录下必须包含 resources 文件夹,且文件夹内必须有 active.yaml和四种不同环境的开发文件至少一种

package main

import (
	"fmt"
	"github.com/goworkeryyt/go-config"
	"github.com/goworkeryyt/go-config/redis"
	"os"
)

func main() {
	// 获取全局配置
	globalConfig := goconfig.GlobalConfig()
	if globalConfig == nil{
		fmt.Println("未能读取配置")
		os.Exit(1)
	}
	// 读取 redis的配置,方法一
	redis1 := globalConfig.Redis
	fmt.Println(redis1)

	// 获取redis的配置,方法二
	redis2 := redis.Redis{}
	globalConfig.SubItem("redis",&redis2)
	fmt.Println(redis2)
	
	// 获取其他定义的配置,方法同理
}


目录结构
configs
├
├── captcha(验证码图片尺寸配置)
├
├── consul(注册中心配置)
├
├── database(数据库配置)
├
├── email(邮件配置)
├
├── ftp(文件服务器配置)
├
├── jwt(jwt token生成和校验配置)
├
├── mqtt(mqtt物联网配置)
├
├── pay(支付相关配置 支付宝和微信)
├
├── profile(多配置文件相关配置)
├
├── redis(redis缓存数据库相关配置)
├
├── resources(项目整合配置文件示例)
│   ├── active.yaml      配置指定要激活启用的配置文件
│   └── dev_config.yaml  开发环境配置文件
│   └── fat_config.yaml  功能验收测试环境配置文件
│   └── pro_config.yaml  生产环境配置文件
│   └── uat_config.yaml  用户验收测试环境配置文件
├
├── server(服务端口等相关配置)
├
├── zap(日志相关配置)

Documentation

Index

Constants

View Source
const (

	// ConfigSuffix 配置文件默认后缀
	ConfigSuffix = "_config"

	// ConfigType 配置文件类型
	ConfigType = "yaml"

	// ConfigPath 配置文件所在路径
	ConfigPath = "./resources"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {

	/** 服务实例配置 */
	Server server.Server `mapstructure:"server"             json:"server"            yaml:"server"`

	/** 注册中心配置 */
	Consul consul.Consul `mapstructure:"consul"             json:"consul"            yaml:"consul"`

	/** 验证码配置 */
	Captcha captcha.Captcha `mapstructure:"captcha"            json:"captcha"           yaml:"captcha"`

	/** MySQL 数据库配置 */
	MySQL database.MySQL `mapstructure:"mysql"              json:"mysql"             yaml:"mysql"`

	/** Postgre 数据库配置 */
	PostgreSQL database.PostgreSQL `mapstructure:"postgre"            json:"postgre"           yaml:"postgre"`

	/** sqlite 数据库配置 */
	SQLite database.SQLite `mapstructure:"sqlite"             json:"sqlite"            yaml:"sqlite"`

	/** redis 缓存数据库配置 */
	Redis redis.Redis `mapstructure:"redis"              json:"redis"             yaml:"redis"`

	/** 邮件发送相关配置 */
	Email email.Email `mapstructure:"email"              json:"email"             yaml:"email"`

	/** 文件服务器配置 */
	Ftp ftp.Ftp `mapstructure:"ftp"                json:"local"             yaml:"ftp"`

	/** jwt token 相关配置 */
	JWT jwt.JWT `mapstructure:"jwt"                json:"jwt"               yaml:"jwt"`

	/** mqtt 代理服务器相关配置 */
	Mqtt mqtt.Mqtt `mapstructure:"mqtt"               json:"mqtt"              yaml:"mqtt"`

	/** 日志相关配置 */
	Zap zap.Zap `mapstructure:"zap"                json:"zap"               yaml:"zap"`

	/** 支付相关配置-支付宝 */
	AliPay pay.Alipay `mapstructure:"alipay"             json:"alipay"            yaml:"alipay"`

	/** 支付相关配置-微信 */
	Weichat pay.Weichat `mapstructure:"weichat"            json:"weichat"           yaml:"weichat"`

	/** 可自己取一些扩展配置 */
	Viper *viper.Viper
}

Config 项目单节点统一配置

func GlobalConfig

func GlobalConfig(envArr ...string) *Config

GlobalConfig 获取全局配置

func (*Config) SubItem

func (c *Config) SubItem(subKey string, v interface{})

SubItem 从配置中获取指定的配置子项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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