Skip to content

Commit

Permalink
微调
Browse files Browse the repository at this point in the history
  • Loading branch information
Heartfilia committed Apr 22, 2024
1 parent 80249a6 commit 787b5ad
Show file tree
Hide file tree
Showing 12 changed files with 533 additions and 381 deletions.
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ HttpsServices:
DefaultTimeOut: 30 # 当执行端没有返回值时,等待%d秒返回超时
CloseLog: false # 关闭一些日志
CloseWebLog: false # 关闭Web服务访问的日志
Mode: release # release:发布版本 debug:调试版 test:测试版本
Cors: false # 是否开启CorsMiddleWare中间件--默认不开启
37 changes: 30 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package config

import (
"JsRpc/utils"
"errors"
"gopkg.in/yaml.v3"
"os"
)

func InitConf(path string, conf *ConfStruct) (err error) {
fileContent, err := os.ReadFile(path)
if err != nil {
return err
var DefaultTimeout = 30

func InitConf(path string) (ConfStruct, error) {
defaultConf := ConfStruct{
BasicListen: `:12080`,
HttpsServices: HttpsConfig{
IsEnable: false,
HttpsListen: `:12443`,
},
DefaultTimeOut: DefaultTimeout,
}
if !utils.IsExists(path) {
return defaultConf, errors.New("config path not found")
}
if err = yaml.Unmarshal(fileContent, &conf); err != nil {
return err

file, _ := os.Open(path) // 因为上面已经判断了 文件是存在的 所以这里不用捕获错误
defer func(file *os.File) {
err := file.Close()
if err != nil {
}
}(file)
conf := ConfStruct{}
decoder := yaml.NewDecoder(file)
err := decoder.Decode(&conf)
if err != nil {
return defaultConf, err
}
return
return conf, nil
}

type ConfStruct struct {
Expand All @@ -22,6 +43,8 @@ type ConfStruct struct {
DefaultTimeOut int `yaml:"DefaultTimeOut"`
CloseLog bool `yaml:"CloseLog"`
CloseWebLog bool `yaml:"CloseWebLog"`
Mode string `yaml:"Mode"`
Cors bool `yaml:"Cors"`
}

// HttpsConfig 代表HTTPS相关配置的结构体
Expand Down
Loading

0 comments on commit 787b5ad

Please sign in to comment.