go 讀取.ini配置檔案

Wchime發表於2024-11-15

安裝所需的庫

go get gopkg.in/ini.v1

我這裡整合到gin中配合之前的解密一起使用

package conf

import (
    "fmt"
    "gin__tte/utils/encryption"
    "log"

    "gopkg.in/ini.v1"
)

const SERVER_NAME = "gin_system"

var (
    SECRET       string
    MODE         string
    RUNPORT      string
    STORAGE_TYPE string

    MYSQL_USER     string
    MYSQL_PWD      string
    MYSQL_HOST     string
    MYSQL_PORT     string
    MYSQL_DATABASE string

    REDIS_HOST    string
    REDIS_PORT    string
    REDIS_PWD     string
    REDIS_LIBRARY int

    MINIO_HOST   string
    MINIO_PORT   string
    MINIO_USER   string
    MINIO_PWD    string
    MINIO_BUCKET string
    MINIO_SECURE bool

    COS_SECRET_ID  string
    COS_SECRET_KEY string
    COS_REGION     string
    COS_SCHEME     string
    COS_BUCKET     string
    COS_DOMAIN     string
)

func init() {
    conf, err := ini.Load("config.ini")
    if err != nil {
        fmt.Println("配置檔案讀取錯誤,請檢查檔案路徑:", err)
        log.Fatalln("配置檔案讀取錯誤,請檢查檔案路徑")
    }

    SECRET = conf.Section("serve").Key("secret").MustString("123456")
    MODE = conf.Section("serve").Key("mode").MustString("debug")
    RUNPORT = conf.Section("serve").Key("port").MustString("0.0.0.0:8080")
    STORAGE_TYPE = conf.Section("serve").Key("storage").MustString("minio")

    MYSQL_HOST = conf.Section("mysql").Key("host").MustString("127.0.0.1")
    MYSQL_PORT = conf.Section("mysql").Key("port").MustString("3306")
    MYSQL_USER = conf.Section("mysql").Key("user").MustString("")
    MYSQL_PWD = conf.Section("mysql").Key("password").MustString("")
    MYSQL_DATABASE = conf.Section("mysql").Key("database").MustString("")

    REDIS_HOST = conf.Section("redis").Key("host").MustString("127.0.0.1")
    REDIS_PORT = conf.Section("redis").Key("port").MustString("6379")
    REDIS_PWD = conf.Section("redis").Key("password").MustString("")
    REDIS_LIBRARY = conf.Section("redis").Key("library").MustInt(0)

    MINIO_HOST = conf.Section("minio").Key("host").MustString("127.0.0.1")
    MINIO_PORT = conf.Section("minio").Key("port").MustString("9000")
    MINIO_USER = conf.Section("minio").Key("user").MustString("")
    MINIO_PWD = conf.Section("minio").Key("password").MustString("")
    MINIO_BUCKET = conf.Section("minio").Key("bucket").MustString("")
    MINIO_SECURE = conf.Section("minio").Key("secure").MustBool(false)

    COS_SECRET_ID = conf.Section("cos").Key("secret_id").MustString("")
    COS_SECRET_KEY = conf.Section("cos").Key("secret_key").MustString("")
    COS_REGION = conf.Section("cos").Key("region").MustString("")
    COS_SCHEME = conf.Section("cos").Key("scheme").MustString("")
    COS_BUCKET = conf.Section("cos").Key("bucket").MustString("")
    COS_DOMAIN = conf.Section("cos").Key("domain").MustString("")

    MYSQL_USER = encryption.GetDataAes(SECRET, MYSQL_USER)
    REDIS_PWD = encryption.GetDataAes(SECRET, REDIS_PWD)
    MYSQL_PWD = encryption.GetDataAes(SECRET, MYSQL_PWD)
    // MINIO_USER = encryption.GetDataAes(SECRET, MINIO_USER)
    // MINIO_PWD = encryption.GetDataAes(SECRET, MINIO_PWD)

    if MYSQL_USER == "" || MYSQL_PWD == "" || REDIS_PWD == "" {
        log.Fatalln("配置檔案解密錯誤,請檢查檔案")
    }

}

我的ini檔案

[serve]
secret = R1546eOPjJzXwYoGC0rxIONR
mode = debug
port = 0.0.0.0:8081
storage = minio


[mysql]
host = 192.168.1.123
port = 3306
user = HDqnWhtu8Zyuv8OXShf27Q==
password = BKuhzXY4VmElqJG1Annvjw==
database = gin_tte


[redis]
host = 192.168.1.123
port = 6379
password = 54vFJmKuP4MGYBH6oG72dg==
library = 1

[minio]
host = 192.168.1.123
port = 9000
user = ZVF8VV2KKWH687VZ41MY
password = Vya75gyOQmAWnNKVkuLvwendrpaJDswK0LinK
bucket = media
secure = false

[cos]
secret_id = 你的SecretID
secret_key = 你的SecretKey
region = 你的region在go中好像沒有用到
scheme = https
bucket = 你的bucket在go中好像沒有用到
domain = 你的domain自定義域名訪問

相關文章