소스 검색

改成支持单数据库和单服务

qdy 2 달 전
부모
커밋
eee1997f56
1개의 변경된 파일92개의 추가작업 그리고 34개의 파일을 삭제
  1. 92
    34
      main.go

+ 92
- 34
main.go 파일 보기

@@ -3,19 +3,23 @@ package main
3 3
 import (
4 4
 	"log"
5 5
 
6
-	"git.x2erp.com/qdy/go-db/dbstart"
6
+	"git.x2erp.com/qdy/go-base/config"
7
+	"git.x2erp.com/qdy/go-base/consul"
8
+	"git.x2erp.com/qdy/go-base/container"
9
+	"git.x2erp.com/qdy/go-base/webx"
10
+	"git.x2erp.com/qdy/go-base/webx/health"
11
+	"git.x2erp.com/qdy/go-base/webx/router"
12
+
7 13
 	"git.x2erp.com/qdy/go-db/factory/database"
8 14
 
9
-	"git.x2erp.com/qdy/go-base/bootstraps"
10 15
 	"git.x2erp.com/qdy/go-base/ctx"
11 16
 	"git.x2erp.com/qdy/go-base/logger"
12 17
 	"git.x2erp.com/qdy/go-base/middleware"
13
-	"git.x2erp.com/qdy/go-base/webx"
14 18
 )
15 19
 
16 20
 var (
17
-	serviceName    = "svc-ai"
18
-	serviceVersion = "1"
21
+	appName    = "svc-ai"
22
+	appVersion = "1"
19 23
 )
20 24
 
21 25
 // 定义结构体
@@ -33,43 +37,97 @@ type UserResponse struct {
33 37
 }
34 38
 
35 39
 func main() {
36
-	// 1. 初始化基础启动器
37
-	boot := bootstraps.NewBootstrapper(serviceName, serviceVersion)
38 40
 
39
-	// 2. 初始化数据库(都在bootstraps包中)
40
-	dbBoot := dbstart.NewDBBootstrapper(boot.GetConfig())
41
-	dbBoot.Init()
41
+	//0.日志
42
+	//logger.InitBootLog()
43
+	logBootFactory := logger.InitBootLog()
44
+
45
+	//1.获取配置文件
46
+	cfg := config.GetConfig()
47
+	cfg.SetAppName(appName)
48
+	cfg.SetAppVersion(appVersion)
49
+
50
+	//2.创建关闭容器
51
+	ctr := container.NewContainer(cfg)
52
+
53
+	//注册日志,实现自动关闭
54
+	container.Reg(ctr, logBootFactory)
55
+
56
+	//3.创建数据库工厂--如果需求
57
+	dbFactory := container.Create(ctr, database.CreateDBFactory)
42 58
 
43
-	agentDB := dbBoot.GetDBFactory("agent")
44 59
 	// 赋值认证中间件参数
45
-	middleware.JWTAuthMiddlewareInit(boot.GetConfig())
60
+	middleware.JWTAuthMiddlewareInit(cfg)
61
+
62
+	//测试数据库连接
63
+	dbFactory.TestConnection()
64
+
65
+	//得到webservice服务工厂
66
+	webxFactory := webx.GetWebServiceFactory()
67
+
68
+	//建立hhtpService服务
69
+	webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig())
70
+
71
+	//建立路由-api
72
+	routerService := router.NewWebService(webServcie.GetRouter())
73
+
74
+	//注册路由--api
75
+	registerDorisRouter(routerService, dbFactory)
76
+
77
+	// 注册健康检查-api
78
+	health.RegisterConsulHealthCheck(routerService)
79
+
80
+	//启动服务
81
+	webServcie.Run()
82
+
83
+	//启用运行日志
84
+	container.Create(ctr, logger.InitRuntimeLogger)
85
+
86
+	//注册到注册中心
87
+	container.Create(ctr, consul.Register)
88
+	//等待关闭
89
+	webServcie.WaitForServiceShutdown(ctr)
90
+
91
+	//consul.Register(service.serviceName, service.Ip, service.Port, b.Cfg.GetConsulConfig())
92
+	//	service.NewWebService()
93
+
94
+	// // 1. 初始化基础启动器
95
+	// //boot := bootstraps.NewBootstrapper(serviceName, serviceVersion)
96
+
97
+	// // 2. 初始化数据库(都在bootstraps包中)
98
+	// dbBoot := dbstart.NewDBBootstrapper(boot.GetConfig())
99
+	// dbBoot.Init()
100
+
101
+	// agentDB := dbBoot.GetDBFactory("agent")
102
+	// // 赋值认证中间件参数
103
+	// middleware.JWTAuthMiddlewareInit(boot.GetConfig())
46 104
 
47
-	// 3. 启动服务
48
-	//boot.StartService("default")
105
+	// // 3. 启动服务
106
+	// //boot.StartService("default")
49 107
 
50
-	router := boot.GetRouter("default")
51
-	// 3. 创建 WebService 并传入数据库接口
52
-	ws := webx.NewWebService(router)
108
+	// router := boot.GetRouter("default")
109
+	// // 3. 创建 WebService 并传入数据库接口
110
+	// ws := webx.NewWebService(router)
53 111
 
54
-	// ✅ 为默认服务注册健康检查(端口 6060)
55
-	log.Println("注册默认服务健康检查...")
56
-	webx.RegisterDefaultHealthCheck(ws, "svc-ai")
112
+	// // ✅ 为默认服务注册健康检查(端口 6060)
113
+	// log.Println("注册默认服务健康检查...")
114
+	// webx.RegisterDefaultHealthCheck(ws, "svc-ai")
57 115
 
58
-	//使用默认数据库
59
-	registerDefaultRouter(ws, dbBoot.GetDefaultDBFactory())
116
+	// //使用默认数据库
117
+	// registerDefaultRouter(ws, dbBoot.GetDefaultDBFactory())
60 118
 
61
-	//启动第2个服务
62
-	routerDoris := boot.GetRouter("doris")
63
-	wsDoris := webx.NewWebService(routerDoris)
64
-	registerDorisRouter(wsDoris, agentDB)
119
+	// //启动第2个服务
120
+	// routerDoris := boot.GetRouter("doris")
121
+	// wsDoris := webx.NewWebService(routerDoris)
122
+	// registerDorisRouter(wsDoris, agentDB)
65 123
 
66
-	//注册健康检查
67
-	webx.RegisterDefaultHealthCheck(wsDoris, "svc-doris")
68
-	// 6. 运行服务
69
-	boot.Run(dbBoot)
124
+	// //注册健康检查
125
+	// webx.RegisterDefaultHealthCheck(wsDoris, "svc-doris")
126
+	// // 6. 运行服务
127
+	// boot.Run(dbBoot)
70 128
 }
71 129
 
72
-func registerDorisRouter(ws *webx.WebService, dbFactory *database.DBFactory) {
130
+func registerDorisRouter(ws *router.RouterService, dbFactory *database.DBFactory) {
73 131
 	// GET示例:路径参数绑定
74 132
 	ws.GET("/app/users/{id}",
75 133
 		func(id string, reqCtx *ctx.RequestContext) (UserResponse, error) {
@@ -105,7 +163,7 @@ func registerDorisRouter(ws *webx.WebService, dbFactory *database.DBFactory) {
105 163
 
106 164
 }
107 165
 
108
-func registerDefaultRouter(ws *webx.WebService, dbFactory *database.DBFactory) {
166
+func registerDefaultRouter(ws *router.RouterService, dbFactory *database.DBFactory) {
109 167
 	// GET示例:路径参数绑定
110 168
 	ws.GET("/api/users/{id}",
111 169
 		func(id string, reqCtx *ctx.RequestContext) (UserResponse, error) {
@@ -151,7 +209,7 @@ func registerDefaultRouter(ws *webx.WebService, dbFactory *database.DBFactory) {
151 209
 
152 210
 func getUser(id string, dbFactory *database.DBFactory) (UserResponse, error) {
153 211
 	if dbFactory != nil {
154
-		dbFactory.TestConnection(dbFactory.GetDBType())
212
+		dbFactory.TestConnection()
155 213
 	}
156 214
 
157 215
 	// 查询数据库...
@@ -165,7 +223,7 @@ func getUser(id string, dbFactory *database.DBFactory) (UserResponse, error) {
165 223
 // 业务函数示例
166 224
 func getUserOracle(id string, dbFactory *database.DBFactory) (UserResponse, error) {
167 225
 	if dbFactory != nil {
168
-		dbFactory.TestConnection(dbFactory.GetDBType())
226
+		dbFactory.TestConnection()
169 227
 	}
170 228
 
171 229
 	sql, queryParams := getSQLWithPaginationSQL(0, 10)

Loading…
취소
저장