安装所需的库
go get gopkg.in/ini.v1
我这里集成到gin中配合之前的解密一起使用
package confimport ("fmt""gin__tte/utils/encryption""log""gopkg.in/ini.v1" )const SERVER_NAME = "gin_system"var (SECRET stringMODE stringRUNPORT stringSTORAGE_TYPE stringMYSQL_USER stringMYSQL_PWD stringMYSQL_HOST stringMYSQL_PORT stringMYSQL_DATABASE stringREDIS_HOST stringREDIS_PORT stringREDIS_PWD stringREDIS_LIBRARY intMINIO_HOST stringMINIO_PORT stringMINIO_USER stringMINIO_PWD stringMINIO_BUCKET stringMINIO_SECURE boolCOS_SECRET_ID stringCOS_SECRET_KEY stringCOS_REGION stringCOS_SCHEME stringCOS_BUCKET stringCOS_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自定义域名访问