Skip to content

Commit

Permalink
refactor(config): Rename global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Northes committed Dec 20, 2022
1 parent 0141785 commit 1816519
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
)

var Share *AppConf
var Conf *AppConf

type AppConf struct {
*Site `mapstructure:"site"`
Expand Down Expand Up @@ -66,19 +66,19 @@ func Init(f string) error {
if err != nil {
return err
}
if err = viper.Unmarshal(&Share); err != nil {
if err = viper.Unmarshal(&Conf); err != nil {
return err
}

// 创建目录
err = os.MkdirAll(Share.File.Avatar, 0660)
err = os.MkdirAll(Conf.File.Avatar, 0660)
if err != nil {
return err
}

return nil
}

func GetSitePort() string {
return fmt.Sprintf(":%s", strconv.Itoa(Share.Site.Port))
func (conf *AppConf) GetSitePort() string {
return fmt.Sprintf(":%s", strconv.Itoa(conf.Site.Port))
}
4 changes: 2 additions & 2 deletions controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func HomeHandler(c *gin.Context) {
msg := fmt.Sprintf("APIHut.\nDocs: %s", config.Share.Site.DocsUrl)
msg := fmt.Sprintf("APIHut.\nDocs: %s", config.Conf.Site.DocsUrl)
c.String(http.StatusOK, msg)
}

func NotFound(c *gin.Context) {
path := c.Request.URL.Path
msg := fmt.Sprintf("API Not Found. \nYou path is: %s , check it again please. \nOur docs: %s", path, config.Share.Site.DocsUrl)
msg := fmt.Sprintf("API Not Found. \nYou path is: %s , check it again please. \nOur docs: %s", path, config.Conf.Site.DocsUrl)
c.String(http.StatusOK, msg)
}

Expand Down
6 changes: 4 additions & 2 deletions dao/redis/redis.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package redis

import (
"apihut-server/config"
"fmt"

"apihut-server/config"

"github.com/go-redis/redis"
)

var client *redis.Client

func Init() (err error) {
cfg := config.Share.Redis
cfg := config.Conf.Redis
if !cfg.Enable {
return nil
}
Expand Down
8 changes: 5 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package logger

import (
"os"

"apihut-server/config"

"github.com/gin-gonic/gin"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)

var l *zap.Logger

func Init() (err error) {
cfg := config.Share.Logger
cfg := config.Conf.Logger
writerSyncer := logWriter(cfg.FileName, cfg.MaxSize, cfg.MaxBackups, cfg.MaxAge)
encoder := logEncoder()
level := new(zapcore.Level)
Expand All @@ -21,7 +23,7 @@ func Init() (err error) {
return err
}
var core zapcore.Core
if config.Share.Site.Mode != gin.ReleaseMode {
if config.Conf.Site.Mode != gin.ReleaseMode {
// 开发模式同时输出终端和文件
console := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core = zapcore.NewTee(
Expand Down
2 changes: 1 addition & 1 deletion logic/avatar/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func getFilePath(req *models.AvatarReq) string {
ext = consts.RepoOutput.PNG.String()
}

return path.Join(config.Share.File.Avatar, fmt.Sprintf(
return path.Join(config.Conf.File.Avatar, fmt.Sprintf(
"%s-%d-%d.%s",
req.GetHash(),
req.GetSize(),
Expand Down
4 changes: 2 additions & 2 deletions logic/ip_bank/gaode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type gaodeRsp struct {
}

func GaodeInit() IIPCtrl {
return &gaode{key: config.Share.Open.Gaode.Key}
return &gaode{key: config.Conf.Open.Gaode.Key}
}

func (g *gaode) GetIP(ip net.IP) (*models.IPBank, error) {
v := url.Values{}
v.Set("key", config.Share.Open.Gaode.Key)
v.Set("key", config.Conf.Open.Gaode.Key)
v.Set("ip", ip.String())
v.Set("type", strconv.Itoa(IPVersion(ip.String())))
v.Set("parameters", "")
Expand Down
4 changes: 2 additions & 2 deletions logic/ip_bank/tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type tencentRsp struct {
}

func TencentInit() IIPCtrl {
return &tencent{key: config.Share.Open.Tencent.Key}
return &tencent{key: config.Conf.Open.Tencent.Key}
}

func (t *tencent) GetIP(ip net.IP) (*models.IPBank, error) {
v := url.Values{}
v.Set("key", config.Share.Open.Tencent.Key)
v.Set("key", config.Conf.Open.Tencent.Key)
v.Set("ip", ip.String())

u, err := url.Parse("https://apis.map.qq.com/ws/location/v1/ip")
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func main() {
return
}
// 初始化全文索引
err = bleve.Init(config.Share.Bleve.Index)
err = bleve.Init(config.Conf.Bleve.Index)
if err != nil {
logger.L().DPanic("bleve panic", zap.Error(err))
return
}

_ = r.Run(config.GetSitePort())
_ = r.Run(config.Conf.GetSitePort())
}
2 changes: 1 addition & 1 deletion routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func SetupRouter() *gin.Engine {
gin.SetMode(config.Share.Mode)
gin.SetMode(config.Conf.Mode)

r := gin.New()
r.StaticFile("favicon.ico", "./static/favicon.ico")
Expand Down

0 comments on commit 1816519

Please sign in to comment.