优化结构
This commit is contained in:
+21
-34
@@ -6,11 +6,10 @@ import (
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"kra/internal/app"
|
||||
"kra/internal/conf"
|
||||
"kra/internal/config"
|
||||
"kra/internal/server/router"
|
||||
"kra/internal/service"
|
||||
"kra/internal/service/dto"
|
||||
@@ -21,8 +20,6 @@ import (
|
||||
platformtask "kra/pkg/task"
|
||||
|
||||
"github.com/go-kratos/kratos/v3"
|
||||
"github.com/go-kratos/kratos/v3/config"
|
||||
"github.com/go-kratos/kratos/v3/config/file"
|
||||
"github.com/go-kratos/kratos/v3/log"
|
||||
kratoshttp "github.com/go-kratos/kratos/v3/transport/http"
|
||||
|
||||
@@ -47,8 +44,8 @@ func init() {
|
||||
|
||||
// runtimeContributions is the binary-level list of modules with constructed
|
||||
// route or task dependencies. Adding another runtime module is explicit here.
|
||||
func runtimeContributions(systemRoutes *router.Routes, systemTasks *worker.TaskMethods) app.RuntimeContributions {
|
||||
return app.RuntimeContributions{
|
||||
func runtimeContributions(systemRoutes *router.Routes, systemTasks *worker.TaskMethods) app.Composition {
|
||||
return app.Composition{
|
||||
Routes: []module.RouteRegistrar{systemRoutes},
|
||||
Tasks: []platformtask.Contributor{systemTasks},
|
||||
}
|
||||
@@ -73,7 +70,7 @@ func newApp(logger *slog.Logger, hs *kratoshttp.Server, scheduler *worker.TaskSc
|
||||
)
|
||||
}
|
||||
|
||||
func zapSettings(admin *conf.AdminBackend) (string, logging.Options) {
|
||||
func zapSettings(admin *config.Admin) (string, logging.Options) {
|
||||
options := logging.Options{Level: "info", Format: "json", EncodeLevel: "LowercaseLevelEncoder", LogInConsole: true, ShowLine: true, RetentionDay: 7}
|
||||
root := "logs"
|
||||
if admin == nil || admin.Zap == nil {
|
||||
@@ -87,14 +84,14 @@ func zapSettings(admin *conf.AdminBackend) (string, logging.Options) {
|
||||
return root, options
|
||||
}
|
||||
|
||||
func httpAddress(server *conf.Server) string {
|
||||
if server != nil && server.Http != nil && server.Http.Addr != "" {
|
||||
return server.Http.Addr
|
||||
func httpAddress(server *config.Server) string {
|
||||
if server != nil && server.HTTP != nil && server.HTTP.Addr != "" {
|
||||
return server.HTTP.Addr
|
||||
}
|
||||
return ":8000"
|
||||
}
|
||||
|
||||
func swaggerAddress(server *conf.Server, admin *conf.AdminBackend) string {
|
||||
func swaggerAddress(server *config.Server, admin *config.Admin) string {
|
||||
address := httpAddress(server)
|
||||
if strings.HasPrefix(address, ":") {
|
||||
address = "127.0.0.1" + address
|
||||
@@ -113,45 +110,35 @@ func swaggerAddress(server *conf.Server, admin *conf.AdminBackend) string {
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
c := config.New(
|
||||
config.WithSource(
|
||||
file.NewSource(flagconf),
|
||||
),
|
||||
)
|
||||
defer c.Close()
|
||||
|
||||
if err := c.Load(); err != nil {
|
||||
runtime, err := config.LoadStore(flagconf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var bc conf.Bootstrap
|
||||
if err := c.Scan(&bc); err != nil {
|
||||
panic(err)
|
||||
defer runtime.Close()
|
||||
bc := runtime.Snapshot()
|
||||
if bc == nil {
|
||||
bc = &config.Config{}
|
||||
}
|
||||
logRoot, logOptions := zapSettings(bc.Admin)
|
||||
loggerAttrs := []any{slog.String("service.id", id), slog.String("service.name", Name), slog.String("service.version", Version)}
|
||||
if bc.Admin != nil && bc.Admin.App != nil {
|
||||
loggerAttrs = append(loggerAttrs, slog.String("node", bc.Admin.App.Node), slog.String("app_id", bc.Admin.App.AppId), slog.String("env", bc.Admin.App.Env))
|
||||
loggerAttrs = append(loggerAttrs, slog.String("node", bc.Admin.App.Node), slog.String("app_id", bc.Admin.App.AppID), slog.String("env", bc.Admin.App.Env))
|
||||
}
|
||||
logger, loggerControl := logging.NewReloadableZapLogger(logRoot, "application.log", logOptions, loggerAttrs...)
|
||||
defer loggerControl.Close()
|
||||
slog.SetDefault(logger)
|
||||
log.SetDefault(logger)
|
||||
if bc.Admin != nil {
|
||||
bc.Admin.ConfigPath = flagconf
|
||||
if info, err := os.Stat(flagconf); err == nil && info.IsDir() {
|
||||
bc.Admin.ConfigPath = filepath.Join(flagconf, "config.yaml")
|
||||
unsubscribeLogger := runtime.Subscribe(func(value *config.Config) {
|
||||
var admin *config.Admin
|
||||
if value != nil {
|
||||
admin = value.Admin
|
||||
}
|
||||
}
|
||||
|
||||
runtime := conf.NewRuntime(bc.Data, bc.Admin)
|
||||
unsubscribeLogger := runtime.Subscribe(func(_ *conf.Data, admin *conf.AdminBackend) {
|
||||
root, options := zapSettings(admin)
|
||||
loggerControl.Reload(root, options)
|
||||
})
|
||||
defer unsubscribeLogger()
|
||||
|
||||
app, cleanup, err := wireApp(bc.Server, runtime, logger, loggerControl, Version)
|
||||
application, cleanup, err := wireApp(bc.Server, runtime, logger, loggerControl, Version)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -159,7 +146,7 @@ func main() {
|
||||
logger.Info("Kra administration service initialized", "mod", "system", "version", Version, "http_address", httpAddress(bc.Server), "swagger", swaggerAddress(bc.Server, bc.Admin), "admin_frontend", "http://127.0.0.1:8080")
|
||||
|
||||
// start and wait for stop signal
|
||||
if err := app.Run(); err != nil {
|
||||
if err := application.Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user