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