config setup配置提到外面

This commit is contained in:
linwenxiang committed 2020-12-10 10:24:06 +08:00
1 parent 5f873914e0
commit 0c170fe1c6
4 files changed
+11 -9

No files matched your search

+4 -2
View File
@@ -3,14 +3,15 @@ package api
import ( import (
"context" "context"
"fmt" "fmt"
"go-admin/tools/trace"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/config/source/file"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go-admin/app/admin/router" "go-admin/app/admin/router"
"go-admin/app/jobs" "go-admin/app/jobs"
"go-admin/common/database" "go-admin/common/database"
@@ -20,6 +21,7 @@ import (
"go-admin/pkg/logger" "go-admin/pkg/logger"
"go-admin/tools" "go-admin/tools"
"go-admin/tools/config" "go-admin/tools/config"
"go-admin/tools/trace"
) )
var ( var (
@@ -56,7 +58,7 @@ func init() {
func setup() { func setup() {
//1. 读取配置 //1. 读取配置
config.Setup(configYml) config.Setup(file.NewSource, file.WithPath(configYml))
go config.Watch() go config.Watch()
//2. 设置日志 //2. 设置日志
global.Logger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "bus") global.Logger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "bus")
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/go-admin-team/go-admin-core/config/source/file"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go-admin/tools/config" "go-admin/tools/config"
@@ -27,7 +28,7 @@ func init() {
} }
func run() { func run() {
config.Setup(configYml) config.Setup(file.NewSource, file.WithPath(configYml))
application, errs := json.MarshalIndent(config.ApplicationConfig, "", " ") //转换成JSON返回的是byte[] application, errs := json.MarshalIndent(config.ApplicationConfig, "", " ") //转换成JSON返回的是byte[]
if errs != nil { if errs != nil {
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/go-admin-team/go-admin-core/config/source/file"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go-admin/cmd/migrate/migration" "go-admin/cmd/migrate/migration"
@@ -46,7 +47,7 @@ func run() {
if !generate { if !generate {
//1. 读取配置 //1. 读取配置
config.Setup(configYml) config.Setup(file.NewSource, file.WithPath(configYml))
//2. 设置日志 //2. 设置日志
global.Logger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "bus") global.Logger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "bus")
global.JobLogger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "job") global.JobLogger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "job")
+3 -5
View File
@@ -3,7 +3,7 @@ package config
import ( import (
"fmt" "fmt"
"github.com/go-admin-team/go-admin-core/config" "github.com/go-admin-team/go-admin-core/config"
"github.com/go-admin-team/go-admin-core/config/source/file" "github.com/go-admin-team/go-admin-core/config/source"
"log" "log"
) )
@@ -33,15 +33,13 @@ var (
) )
// Setup 载入配置文件 // Setup 载入配置文件
func Setup(path string) { func Setup(f func(opts ...source.Option) source.Source, options ...source.Option) {
c, err := config.NewConfig() c, err := config.NewConfig()
if err != nil { if err != nil {
log.Fatal(fmt.Sprintf("New config object fail: %s", err.Error())) log.Fatal(fmt.Sprintf("New config object fail: %s", err.Error()))
} }
err = c.Load( err = c.Load(
file.NewSource( f(options...),
file.WithPath(path),
),
) )
if err != nil { if err != nil {
log.Fatal(fmt.Sprintf("Load config source fail: %s", err.Error())) log.Fatal(fmt.Sprintf("Load config source fail: %s", err.Error()))