美文网首页
配置文件的处理

配置文件的处理

作者: 彳亍口巴 | 来源:发表于2020-11-23 13:57 被阅读0次

1、在根目录下面创建conf文件夹,再创建一个app.ini文件,在文件里面写上所需要的配置信息

#debug or release
RUN_MODE=debug


[app]
PageSize = 10
JwtSecret = 233

RuntimeRootPath = runtime/

PrefixUrl = http://127.0.0.1:8000
ImageSavePath = upload/images/
# MB
ImageMaxSize = 5
ImageAllowExts = .jpg,.jpeg,.png
ExportSavePath = export/
# 生成的二维码保存的路径
QrCodeSavePath=qrcode/
FontSavePath = fonts/

LogSavePath = logs/
LogSaveName = log
LogFileExt = log
TimeFormat = 20060102

[server]
RunMode = debug
HttpPort = 8000
ReadTimeout = 60
WriteTimeout = 60

[database]
Type = mysql
User = root
Password = 762513499
Host = 127.0.0.1:7625
Name = blog
TablePrefix = blog_
#Redis缓存配置
[redis]
Host = 192.168.25.128:6379
Password =123456
MaxIdle = 30
MaxActive = 30
IdleTimeout = 200

2、创建setting.go文件,里面根据配置文件去定义对应的结构体,再进行结构体的映射管理

import (
    "github.com/go-ini/ini"
    "log"
    "time"
)

/**
使用MapTo来设置配置参数,进行结构体的映射,将所有的配置项统管到setting中
*/
//
type App struct {
    JwtSecret string
    PageSize int  // 在配置的时候没有说明是字符串还是整数,在这里可以定义
    RuntimeRootPath string

    PrefixUrl string
    ImageSavePath string
    ImageMaxSize int
    ImageAllowExts []string // 数组,可以通过英文逗号,会自动进行分割

    ExportSavePath string

    // 二维码生成的保存路径
    QrCodeSavePath string
    FontSavePath string

    LogSavePath string
    LogSaveName string
    LogFileExt string
    TimeFormat string
}

var AppSetting=&App{} // 取结构体的地址

type Server struct {
    RunMode string
    HttpPort int
    ReadTimeout time.Duration
    WriteTimeout time.Duration
}

var ServerSetting = &Server{}

// 数据库配置对应的结构体
type Database struct {
    Type string
    User string
    Password string
    Host string
    Name string
    TablePrefix string
}

var DatabaseSetting = &Database{}

// Redis配置对应的结构体
type Redis struct {
    Host string
    Password string
    MaxIdle int
    MaxActive int
    IdleTimeout time.Duration
}

var RedisSetting =&Redis{}

func Setup() {
    Cfg, err := ini.Load("conf/app.ini")
    if err != nil {
        log.Fatalf("Fail to parse 'conf/app.ini': %v", err)
    }

    err = Cfg.Section("app").MapTo(AppSetting)
    if err != nil {
        log.Fatalf("Cfg.MapTo AppSetting err: %v", err)
    }
    // 让app.ini 中的最大值为5M,否则的话只是5b
    AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024

    err = Cfg.Section("server").MapTo(ServerSetting)
    if err != nil {
        log.Fatalf("Cfg.MapTo ServerSetting err: %v", err)
    }
    // 设置时间
    ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
    ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second

    err = Cfg.Section("database").MapTo(DatabaseSetting)
    if err != nil {
        log.Fatalf("Cfg.MapTo DatabaseSetting err: %v", err)
    }

    err = Cfg.Section("redis").MapTo(RedisSetting)
    if err != nil {
        log.Fatalf("Cfg.MapTo DatabaseSetting err: %v", err)
    }
}

至此就完成了通过结构体的映射去实现配置的功能,之后要进行使用的时候,需要现在main函数中调用Setup函数进行初始化,之后就可以直接调用了

import (
    "fmt"
    "go-gin-example2/conf"
)

func main() {
    conf.Setup()
    fmt.Println(conf.AppSetting.ImageAllowExts)
}

相关文章

网友评论

      本文标题:配置文件的处理

      本文链接:https://www.haomeiwen.com/subject/jpamiktx.html