[2024-07-11](INIT): 配置文件
This commit is contained in:
parent
78ebb59142
commit
d13230a68f
36
configs/configs.go
Normal file
36
configs/configs.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package configs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Configs struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Settings 定义配置结构体
|
||||||
|
var Settings Configs
|
||||||
|
|
||||||
|
func readYamlFile(configFilePath string) (yamlFile []byte, err error) {
|
||||||
|
// 读取YAML文件
|
||||||
|
if yamlFile, err = os.ReadFile(configFilePath); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return yamlFile, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfig(configFilePath string) {
|
||||||
|
var (
|
||||||
|
yamlFile []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
// 读取配置文件
|
||||||
|
if yamlFile, err = readYamlFile(configFilePath); err != nil {
|
||||||
|
log.Panic("读取配置文件失败: ", err)
|
||||||
|
}
|
||||||
|
// 解析YAML数据到配置结构体
|
||||||
|
if err = yaml.Unmarshal(yamlFile, &Settings); err != nil {
|
||||||
|
log.Panic("解析配置文件失败: ", err)
|
||||||
|
}
|
||||||
|
}
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module picgo
|
|||||||
|
|
||||||
go 1.22
|
go 1.22
|
||||||
|
|
||||||
require github.com/gorilla/mux v1.8.1
|
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
5
go.sum
5
go.sum
@ -1,2 +1,3 @@
|
|||||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
Loading…
Reference in New Issue
Block a user