-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from cbluebird/main
feat(viper):使用viper代替原来的env读取
- Loading branch information
Showing
10 changed files
with
108 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ | |
.env | ||
funnel | ||
funnel.exe | ||
.DS_Store | ||
.DS_Store | ||
config.yaml | ||
log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
package apis | ||
|
||
import "os" | ||
import ( | ||
"funnel/config/config" | ||
) | ||
|
||
var LIBRARY_URL = os.Getenv("LIBRARY_URL") | ||
var CAPTCHA_BREAKER_URL = os.Getenv("CAPTCHA_BREAKER_BK_URL") | ||
var CAPTCHA_NEW_BREAKER_URL = os.Getenv("CAPTCHA_BREAKER_NEW_URL") | ||
var ZF_URL = os.Getenv("ZF_URL") | ||
//var LIBRARY_URL = os.Getenv("LIBRARY_URL") | ||
//var CAPTCHA_BREAKER_URL = os.Getenv("CAPTCHA_BREAKER_BK_URL") | ||
//var CAPTCHA_NEW_BREAKER_URL = os.Getenv("CAPTCHA_BREAKER_NEW_URL") | ||
//var ZF_URL = os.Getenv("ZF_URL") | ||
// | ||
//var ZF_Main_URL = os.Getenv("ZF_URL") | ||
//var ZF_BK_URL = os.Getenv("ZF_URL_BK") | ||
// | ||
//var CANTEEN_URL = os.Getenv("CANTEEN_URL") | ||
|
||
var ZF_Main_URL = os.Getenv("ZF_URL") | ||
var ZF_BK_URL = os.Getenv("ZF_URL_BK") | ||
var LIBRARY_URL string = config.Config.GetString("library") | ||
|
||
var CANTEEN_URL = os.Getenv("CANTEEN_URL") | ||
var CAPTCHA_BREAKER_URL string = config.Config.GetString("captcha.breaker_url") | ||
var CAPTCHA_NEW_BREAKER_URL string = config.Config.GetString("captcha.new_breaker_url") | ||
|
||
var ZF_URL string = config.Config.GetString("zf.url") | ||
var ZF_Main_URL string = config.Config.GetString("zf.main_url") | ||
var ZF_BK_URL string = config.Config.GetString("zf.bk_url") | ||
|
||
var CANTEEN_URL string = config.Config.GetString("canteen") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
port: | ||
|
||
redis: | ||
host: "127.0.0.1" | ||
port: 6379 | ||
db: 0 | ||
user: root | ||
pass: | ||
|
||
library: | ||
|
||
captcha: | ||
breaker_url: | ||
new_breaker_url: | ||
|
||
zf: | ||
url: | ||
main_url: | ||
bk_url: | ||
|
||
canteen: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/spf13/viper" | ||
"log" | ||
) | ||
|
||
var Config = viper.New() | ||
|
||
func init() { | ||
Config.SetConfigName("config") | ||
Config.SetConfigType("yaml") | ||
Config.AddConfigPath(".") | ||
Config.WatchConfig() // 自动将配置读入Config变量 | ||
err := Config.ReadInConfig() | ||
if err != nil { | ||
log.Fatal("Config not find", err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,20 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
|
||
redisConfig "funnel/config/redis" | ||
"github.com/go-redis/redis" | ||
) | ||
|
||
var Redis = redis.Client{} | ||
|
||
func RedisInit() *redis.Client { | ||
REDIS_HOST := "localhost" | ||
REDIS_PORT := "6379" | ||
REDIS_PASSWORD := "" | ||
REDIS_DB := 0 | ||
|
||
if os.Getenv("REDIS_HOST") != "" { | ||
REDIS_HOST = os.Getenv("REDIS_HOST") | ||
} | ||
|
||
if os.Getenv("REDIS_PORT") != "" { | ||
REDIS_PORT = os.Getenv("REDIS_PORT") | ||
} | ||
var Redis *redis.Client | ||
var RedisInfo redisConfig.RedisConfig | ||
|
||
if os.Getenv("REDIS_DB") != "" { | ||
REDIS_DB, _ = strconv.Atoi(os.Getenv("REDIS_DB")) | ||
} | ||
func init() { | ||
info := redisConfig.GetConfig() | ||
|
||
if os.Getenv("REDIS_PASSWORD") != "" { | ||
REDIS_PASSWORD = os.Getenv("REDIS_PASSWORD") | ||
} | ||
|
||
RedisClient := redis.NewClient(&redis.Options{ | ||
Addr: REDIS_HOST + ":" + REDIS_PORT, | ||
Password: REDIS_PASSWORD, | ||
DB: REDIS_DB, | ||
Redis = redis.NewClient(&redis.Options{ | ||
Addr: info.Host + ":" + info.Port, | ||
Password: info.Password, | ||
DB: info.DB, | ||
}) | ||
|
||
_, err := RedisClient.Ping().Result() | ||
|
||
if err != nil { | ||
panic("redis ping error") | ||
} | ||
Redis = *RedisClient | ||
return RedisClient | ||
RedisInfo = info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package redis | ||
|
||
import "funnel/config/config" | ||
|
||
type RedisConfig struct { | ||
Host string | ||
Port string | ||
DB int | ||
Password string | ||
} | ||
|
||
func GetConfig() RedisConfig { | ||
Info := RedisConfig{ | ||
Host: "localhost", | ||
Port: "6379", | ||
DB: 0, | ||
Password: "", | ||
} | ||
if config.Config.IsSet("redis.host") { | ||
Info.Host = config.Config.GetString("redis.host") | ||
} | ||
if config.Config.IsSet("redis.port") { | ||
Info.Port = config.Config.GetString("redis.port") | ||
} | ||
if config.Config.IsSet("redis.db") { | ||
Info.DB = config.Config.GetInt("redis.db") | ||
} | ||
if config.Config.IsSet("redis.pass") { | ||
Info.Password = config.Config.GetString("redis.pass") | ||
} | ||
return Info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"funnel/config" | ||
"funnel/config/config" | ||
"funnel/router" | ||
"github.com/gin-gonic/gin" | ||
"os" | ||
) | ||
|
||
func main() { | ||
r := gin.Default() | ||
config.SetupConfigs(r) | ||
router.SetupRouter(r) | ||
_ = r.Run(":" + os.Getenv("ROUTER_POST")) | ||
_ = r.Run(":" + config.Config.GetString("port")) | ||
} |