gin框架学习-配置文件(config)

数据库、redis、fastDFS等处理已配置文件的形式可配置app.json:

{
  "app_name": "Batac",
  "app_model": "debug",
  "app_host": "localhost",
  "app_port": "8080",
  "sms": {
    "sign_name": "云裳",
    "template_code": "YS_123456",
    "app_key": "",
    "app_secret": "",
    "region_id": ""
  },
  "database": {
    "driver": "mysql",
    "user": "Batac",
    "password": "password",
    "host": "127.0.0.1",
    "port": "3306",
    "db_name": "ys_batac",
    "charset": "utf8",
    "show_sql": false
  },
  "redis_config": {
    "addr": "127.0.0.1",
    "port": "6379",
    "password": "password",
    "db": 0
  }
}

在config模块读取文件获取配置:

package tool

import (
	"bufio"
	"encoding/json"
	"os"
)

type Config struct {
	AppName string	`json:"app_name"`
	AppModel string `json:"app_model"`
	AppHost string	`json:"app_host"`
	AppPort string	`json:"app_port"`
	Database DatabaseConfig `json:"database"`
	RedisConfig RedisConfig `json:"redis_config"`
}

type DatabaseConfig struct {
	Driver string `json:"driver"`
	User string `json:"user"`
	Password string `json:"password"`
	Host string `json:"host"`
	Port string `json:"port"`
	DbName string `json:"db_name"`
	Chartset string `json:"charset"`
	ShowSql bool `json:"show_sql"`
}

//Redis属性定义
type RedisConfig struct {
	Addr string `json:"addr"`
	Port string `json:"port"`
	Password string `json:"password"`
	Db int `json:"db"`
}

//方法或者变量首字母大写, 表示对外可见
func GetConfig() *Config  {
	return cfg
}

//全局变量 对外不可见
var cfg * Config = nil

func ParseConfig(path string)(*Config, error)  {
	file, err := os.Open(path)
	defer file.Close()

	if err != nil {
		panic(err)
	}
	reader := bufio.NewReader(file)
	decoder := json.NewDecoder(reader)
	if  err = decoder.Decode(&cfg); err != nil{
		return nil, err
	}
	return cfg, nil
}

猜你喜欢

转载自blog.csdn.net/Batac_Lee/article/details/109625932