Skip to content

Commit

Permalink
Merge pull request #20 from cbluebird/main
Browse files Browse the repository at this point in the history
feat(viper):使用viper代替原来的env读取
  • Loading branch information
cbluebird authored Feb 18, 2024
2 parents 16503ad + ead7216 commit 4047b10
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 85 deletions.
10 changes: 0 additions & 10 deletions .env.example

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
.env
funnel
funnel.exe
.DS_Store
.DS_Store
config.yaml
log
29 changes: 21 additions & 8 deletions app/apis/rootUrls.go
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")
5 changes: 0 additions & 5 deletions app/controller/controllerHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func ErrorHandle(context *gin.Context, err error) {
exp = errors.SessionExpired
break
}
case errors.ERR_OAUTH_ERROR:
{
exp = errors.OauthError
break
}
}

println(err.Error())
Expand Down
21 changes: 21 additions & 0 deletions config.example.yaml
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:
19 changes: 19 additions & 0 deletions config/config/viper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"github.com/spf13/viper"

Check failure on line 4 in config/config/viper.go

View workflow job for this annotation

GitHub Actions / build-release

no required module provides package github.com/spf13/viper; to add it:
"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)
}
}
20 changes: 0 additions & 20 deletions config/configRegister.go

This file was deleted.

47 changes: 10 additions & 37 deletions config/redis.go
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
}
32 changes: 32 additions & 0 deletions config/redis/config.go
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
}
6 changes: 2 additions & 4 deletions main.go
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"))
}

0 comments on commit 4047b10

Please sign in to comment.