Skip to content

Commit

Permalink
feat(config): Add config file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Northes committed Oct 2, 2022
1 parent 1b30273 commit ad3491d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package config

import (
"fmt"
"github.com/spf13/viper"
"os"
"strconv"

"github.com/spf13/viper"
)

var Share *AppConf
Expand Down Expand Up @@ -57,8 +58,8 @@ type Logger struct {
MaxBackups int `mapstructure:"max_backups"`
}

func Init() error {
viper.SetConfigFile("./conf/config.yaml")
func Init(f string) error {
viper.SetConfigFile(f)
err := viper.ReadInConfig()
if err != nil {
return err
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package main

import (
"flag"

"apihut-server/config"
"apihut-server/dao/mysql"
"apihut-server/dao/redis"
"apihut-server/logger"
"apihut-server/routers"
)

var configFile string

func init() {
flag.StringVar(&configFile, "f", "./conf/config.yaml", "Config file")
}

func main() {
flag.Parse()

var err error
// 初始化配置
err = config.Init()
err = config.Init(configFile)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit ad3491d

Please sign in to comment.