Przeglądaj źródła

Release v0.1.12.30

qdy 2 miesięcy temu
commit
1fb58868b5
6 zmienionych plików z 286 dodań i 213 usunięć
  1. 1
    0
      .gitignore
  2. 22
    0
      internal/domain/tenant_config.go
  3. 122
    179
      main.go
  4. 38
    34
      service/agent_to_doris.go
  5. 34
    0
      tables/worker_config.go
  6. 69
    0
      test/my_mongodb_test.go

+ 1
- 0
.gitignore Wyświetl plik

@@ -0,0 +1 @@
1
+logs/

+ 22
- 0
internal/domain/tenant_config.go Wyświetl plik

@@ -0,0 +1,22 @@
1
+package domain
2
+
3
+import (
4
+	"git.x2erp.com/qdy/go-base/ctx"
5
+	"git.x2erp.com/qdy/go-base/models"
6
+)
7
+
8
+// 租户配置信息
9
+type TenantConfig struct {
10
+	models.BsonStringModel `bson:",inline"`
11
+	AgentUrl               string `bson:"agent_url,omitempty"`
12
+	AgentConcurrency       int    `bson:"agent_concurrency,omitempty"`
13
+}
14
+
15
+// 构造函数,设置默认值
16
+func NewTenantConfig(ctx ctx.RequestContext, tenantId string, url string, concurrency int) *TenantConfig {
17
+	return &TenantConfig{
18
+		BsonStringModel:  models.NewStringIDModelWithID(ctx, tenantId),
19
+		AgentConcurrency: concurrency,
20
+		AgentUrl:         url,
21
+	}
22
+}

+ 122
- 179
main.go Wyświetl plik

@@ -3,222 +3,165 @@ package main
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"log"
6
-	"net/http"
7
-	"strings"
8
-	"time"
6
+
7
+	"git.x2erp.com/qdy/go-db/factory/database"
8
+	"git.x2erp.com/qdy/go-db/factory/mongodb"
9
+	"git.x2erp.com/qdy/go-svc-worker/internal/domain"
9 10
 
10 11
 	"git.x2erp.com/qdy/go-base/config"
12
+	"git.x2erp.com/qdy/go-base/consul"
13
+	"git.x2erp.com/qdy/go-base/container"
14
+	"git.x2erp.com/qdy/go-base/ctx"
11 15
 	"git.x2erp.com/qdy/go-base/logger"
12
-	"git.x2erp.com/qdy/go-base/myservice"
16
+	"git.x2erp.com/qdy/go-base/middleware"
13 17
 	"git.x2erp.com/qdy/go-base/types"
14
-	"git.x2erp.com/qdy/go-db/factory/database"
15
-	"git.x2erp.com/qdy/go-svc-worker/service"
16
-	"go-micro.dev/v4/metadata"
18
+	"git.x2erp.com/qdy/go-base/webx"
19
+	"git.x2erp.com/qdy/go-base/webx/health"
20
+	"git.x2erp.com/qdy/go-base/webx/router"
17 21
 )
18 22
 
19
-// 定义业务服务
20
-type DBFactory struct {
21
-	dbFactory *database.DBFactory
22
-}
23
-
24
-// 配置
25 23
 var (
26
-	serviceName    string
27
-	serviceVersion string
24
+	appName    = "svc-worker"
25
+	appVersion = "1"
28 26
 )
29 27
 
30 28
 func main() {
29
+	//0.日志
30
+	//logger.InitBootLog()
31
+	logBootFactory := logger.InitBootLog()
31 32
 
32
-	// ========== 第一阶段:强制写入文件的启动日志 ==========
33
-	// 这一步确保即使配置加载失败,也有日志记录
34
-	if err := logger.InitBootLog("svc-worker"); err != nil {
35
-		// 连启动日志都初始化失败,只能输出到控制台
36
-		log.Fatal("无法初始化启动日志: ", err)
37
-	}
33
+	//1.获取配置文件
34
+	cfg := config.GetConfig()
35
+	cfg.SetAppName(appName)
36
+	cfg.SetAppVersion(appVersion)
38 37
 
39
-	logger.BootLog("开始加载配置...")
40
-	cfg, err := config.GetConfig()
41
-	if err != nil {
42
-		log.Fatalf("Failed to create RabbitMQ factory: %v", err)
43
-	}
38
+	//2.创建关闭容器
39
+	ctr := container.NewContainer(cfg)
44 40
 
45
-	serviceConfig := cfg.GetService()
46
-	microConfig := cfg.GetMicro()
47
-	serviceName = serviceConfig.ServiceName
48
-	serviceVersion = serviceConfig.ServiceVersion
41
+	//注册日志,实现自动关闭
42
+	container.Reg(ctr, logBootFactory)
49 43
 
50
-	log.Printf("serviceName: %s", serviceName)
51
-	log.Printf("Port: %d", serviceConfig.Port)
52
-	log.Printf("Consul: %s", microConfig.RegistryAddress)
44
+	//3.创建数据库工厂--如果需求
45
+	dbFactory := container.Create(ctr, database.CreateDBFactory)
53 46
 
54
-	// 2. 初始化数据库
55
-	dbFactory, err := database.GetDBFactory()
56
-	if err != nil {
57
-		log.Fatal("数据库连接失败:", err)
58
-	}
59
-	defer func() {
60
-		if err := dbFactory.Close(); err != nil {
61
-			logger.Info("数据库关闭错误: %v", err)
62
-		}
63
-	}()
47
+	// 赋值认证中间件参数
48
+	middleware.JWTAuthMiddlewareInit(cfg)
64 49
 
65
-	// 3. 创建服务实例
66
-	dbfactory := &DBFactory{dbFactory: dbFactory}
50
+	//测试数据库连接
51
+	dbFactory.TestConnection()
67 52
 
68
-	// 4. 使用 micro.Start 启动服务
69
-	webService := myservice.Start(cfg)
53
+	// 创建mongodb
54
+	mongoDBFactory := container.Create(ctr, mongodb.CreateFactory)
55
+	mongoDBFactory.TestConnection()
70 56
 
71
-	// 7. 注册HTTP路由
72
-	webService.Handle("/", http.HandlerFunc(rootHandler))
73
-	webService.Handle("/health", http.HandlerFunc(dbfactory.healthHandler))
74
-	webService.Handle("/info", http.HandlerFunc(infoHandler))
75
-	webService.Handle("/api/data/agent/to/doris", authMiddleware(http.HandlerFunc(dbfactory.agentToDorisHandler)))
57
+	//得到webservice服务工厂
58
+	webxFactory := webx.GetWebServiceFactory()
76 59
 
77
-	logger.InitRuntimeLogger(serviceName, cfg.GetLog())
60
+	//建立hhtpService服务
61
+	webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig())
78 62
 
79
-	log.Println("日志系统初始化完成")
63
+	//建立路由-api
64
+	routerService := router.NewWebService(webServcie.GetRouter())
80 65
 
81
-	// 程序退出时停止ES写入器
82
-	defer logger.StopESWriter()
83
-	logger.Debug("测试日志是否写入ES-----")
66
+	//注册路由--api
67
+	registerDefaultRouter(routerService, mongoDBFactory)
84 68
 
85
-	//关闭-启动日志输出文件功能
86
-	logger.CloseBootLogger()
69
+	// 注册健康检查-api
70
+	health.RegisterConsulHealthCheck(routerService)
87 71
 
88
-	if err := webService.Run(); err != nil {
89
-		log.Fatal("服务运行失败:", err)
90
-	}
72
+	//启动服务
73
+	webServcie.Run()
91 74
 
92
-}
75
+	//启用运行日志
76
+	container.Create(ctr, logger.InitRuntimeLogger)
93 77
 
94
-// 根处理器
95
-func rootHandler(w http.ResponseWriter, r *http.Request) {
96
-	if r.URL.Path != "/" {
97
-		http.NotFound(w, r)
98
-		return
99
-	}
100
-	respondJSON(w, http.StatusOK, map[string]string{
101
-		"service": serviceName,
102
-		"status":  "running",
103
-		"mode":    "http-microservice",
104
-	})
78
+	//注册到注册中心
79
+	container.Create(ctr, consul.Register)
80
+	//等待关闭
81
+	webServcie.WaitForServiceShutdown(ctr)
105 82
 }
106 83
 
107
-// 健康检查处理器
108
-func (s *DBFactory) healthHandler(w http.ResponseWriter, r *http.Request) {
109
-	if err := s.dbFactory.TestConnection(s.dbFactory.GetDBType()); err != nil {
110
-		respondJSON(w, http.StatusServiceUnavailable, map[string]string{
111
-			"status": "down",
112
-			"error":  err.Error(),
113
-		})
114
-		return
115
-	}
116
-	respondJSON(w, http.StatusOK, map[string]string{
117
-		"status": "up",
118
-		"time":   time.Now().Format(time.RFC3339),
119
-	})
120
-}
121
-
122
-// 信息处理器
123
-func infoHandler(w http.ResponseWriter, r *http.Request) {
124
-	respondJSON(w, http.StatusOK, map[string]interface{}{
125
-		"service": serviceName,
126
-		"version": serviceVersion,
127
-		"api": map[string]string{
128
-			"POST /api/data/agent/to/doris": "同步数据到Doris",
129
-			"GET /health":                   "健康检查",
130
-			"GET /info":                     "服务信息",
131
-			"GET /":                         "根路径",
132
-		},
133
-		"features": []string{
134
-			"服务发现(Consul)",
135
-			"负载均衡",
136
-			"健康检查",
137
-			"HTTP API网关",
84
+func registerDefaultRouter(ws *router.RouterService, mongoDBFactory *mongodb.MongoDBFactory) {
85
+	// // GET示例:路径参数绑定
86
+	// ws.GET("/api/users/{id}",
87
+	// 	func(id string, reqCtx *ctx.RequestContext) (UserResponse, error) {
88
+
89
+	// 		log.Print("ctx TenantID:", reqCtx.TenantID)
90
+	// 		// id 自动从路径绑定
91
+	// 		// 注意:webx版本没有自动注入dbFactory
92
+	// 		return getUser(id, dbFactory) // 需要修改getUser函数以获取dbFactory
93
+	// 	},
94
+	// ).Use(middleware.JWTAuthMiddleware).Register()
95
+
96
+	// POST示例:Body参数绑定
97
+	ws.POST("/api/tenant/config",
98
+		func(req domain.TenantConfig, reqCtx *ctx.RequestContext) (*types.QueryResult[interface{}], error) {
99
+			log.Print("ctx TenantID:", reqCtx.TenantID)
100
+			//log.Print("mongoDBFactory:", mongoDBFactory.GetConfig().URI)
101
+			//log.Print("dbFactory:", dbFactory.GetDBName())
102
+			jsonBytes, _ := json.Marshal(req)
103
+			log.Printf("TenantConfig :%s", string(jsonBytes))
104
+
105
+			ok := mongoDBFactory.InsertOne("tenant_configs", req)
106
+			log.Print("TenantConfig InsertOne:", ok)
107
+			// req 自动从JSON Body绑定
108
+			return &types.QueryResult[interface{}]{
109
+				Success: ok,
110
+				Data:    mongoDBFactory.GetConfig().URI,
111
+			}, nil
138 112
 		},
139
-	})
140
-}
141
-
142
-// AgentToDoris处理器
143
-func (s *DBFactory) agentToDorisHandler(w http.ResponseWriter, r *http.Request) {
144
-	if r.Method != "POST" {
145
-		respondJSON(w, http.StatusMethodNotAllowed, types.QueryResult{
146
-			Error:   "只支持POST请求",
147
-			Success: false,
148
-		})
149
-		return
150
-	}
113
+	).Use(middleware.JWTAuthMiddleware).Register()
151 114
 
152
-	// 解析请求
153
-	var requestData types.QueryRequest
154
-	if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
155
-		respondJSON(w, http.StatusBadRequest, map[string]string{
156
-			"error": "无效的JSON数据",
157
-		})
158
-		return
159
-	}
115
+	// 您的业务路由
116
+	ws.POST("/api/query/yaml",
117
+		func() (interface{}, error) {
118
+			return queryYaml(nil) // 需要修改queryYaml函数以获取dbFactory
119
+		},
120
+	).Use(middleware.JWTAuthMiddleware).Register()
160 121
 
161
-	// 处理业务逻辑
162
-	result := service.ServiceAgentToDoris(s.dbFactory, requestData)
122
+	ws.POST("/api/init/config/template",
123
+		func() (interface{}, error) {
124
+			return initConfigTemplate(nil) // 需要修改initConfigTemplate函数以获取dbFactory
125
+		},
126
+	).Use(middleware.JWTAuthMiddleware).Register()
163 127
 
164
-	respondJSON(w, http.StatusOK, result)
165 128
 }
166 129
 
167
-// 认证中间件
168
-func authMiddleware(next http.Handler) http.Handler {
169
-	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
170
-
171
-		// JWT令牌认证
172
-		token := r.Header.Get("Authorization")
173
-		if token != "" && strings.HasPrefix(token, "Bearer ") {
174
-			token = token[7:]
175
-		}
176
-
177
-		// 双重认证:API密钥或JWT
178
-		if token == "" {
179
-			respondJSON(w, http.StatusUnauthorized, map[string]string{
180
-				"error": "需要API密钥或Bearer令牌",
181
-			})
182
-			return
183
-		}
184
-
185
-		// 验证JWT令牌
186
-		if token != "" && !isValidJWT(token) {
187
-			respondJSON(w, http.StatusUnauthorized, map[string]string{
188
-				"error": "无效的访问令牌",
189
-			})
190
-			return
191
-		}
192
-
193
-		// 将认证信息添加到上下文
194
-		ctx := r.Context()
195
-		if token != "" {
196
-			ctx = metadata.Set(ctx, "Authorization", "Bearer "+token)
197
-		}
198
-
199
-		next.ServeHTTP(w, r.WithContext(ctx))
200
-	})
130
+func queryYaml(dbFactory *database.DBFactory) (interface{}, error) {
131
+	// 您的业务逻辑
132
+	return map[string]interface{}{"message": "query yaml success"}, nil
201 133
 }
202 134
 
203
-// JWT验证
204
-func isValidJWT(token string) bool {
205
-	// TODO: 实现JWT验证逻辑
206
-	// 可以使用 github.com/golang-jwt/jwt/v5
207
-	// 临时实现:检查token是否有效格式
208
-	//if len(token) < 10 {
209
-	//	return false
210
-	//	}
211
-	return true // 临时返回true,实际需要验证签名和过期时间
135
+func initConfigTemplate(dbFactory *database.DBFactory) (interface{}, error) {
136
+	// 您的业务逻辑
137
+	return map[string]interface{}{"message": "init config success"}, nil
212 138
 }
213 139
 
214
-// JSON响应辅助函数
215
-func respondJSON(w http.ResponseWriter, status int, data interface{}) {
216
-	w.Header().Set("Content-Type", "application/json")
217
-	w.Header().Set("X-Service-Name", serviceName)
218
-	w.Header().Set("X-Service-Version", serviceVersion)
219
-	w.WriteHeader(status)
140
+// getSQLWithPagination 生成带分页的SQL语句(参数模式)
141
+// 返回SQL语句和参数映射
142
+func getSQLWithPaginationSQL(startRow, endRow int) (string, []interface{}) {
143
+	sql := `SELECT
144
+    CLOTHING_ID,
145
+    CLOTHING_YEAR,
146
+    CLOTHING_NAME
147
+
148
+   FROM (
149
+    SELECT a.*, ROWNUM as rn
150
+    FROM (
151
+        SELECT *
152
+        FROM X6_STOCK_DEV.A3_CLOTHING 
153
+     
154
+        ORDER BY CLOTHING_ID
155
+    ) a
156
+    WHERE ROWNUM <= :1
157
+)
158
+WHERE rn > :2`
220 159
 
221
-	if err := json.NewEncoder(w).Encode(data); err != nil {
222
-		log.Printf("JSON编码错误: %v", err)
160
+	// 创建参数映射
161
+	params := []interface{}{
162
+		endRow,
163
+		startRow - 1, // WHERE rn > :start_row 所以是startRow-1
223 164
 	}
165
+
166
+	return sql, params
224 167
 }

service/agentToDoris.go → service/agent_to_doris.go Wyświetl plik

@@ -2,35 +2,41 @@ package service
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"log"
6 5
 	"strings"
7 6
 	"time"
8 7
 
8
+	"git.x2erp.com/qdy/go-base/logger"
9 9
 	"git.x2erp.com/qdy/go-base/types"
10
-	"git.x2erp.com/qdy/go-db/factory/database"
11 10
 	"git.x2erp.com/qdy/go-db/factory/doris"
12 11
 	"git.x2erp.com/qdy/go-db/factory/http"
13 12
 )
14 13
 
15 14
 // queryToCSVAndInsert 处理单次查询并将结果插入到Doris
16
-func queryToCSVAndInsert(dbFactory *database.DBFactory, queryRequest types.QueryRequest) (*types.QueryResult, error) {
17
-
15
+func queryToCSVAndInsert(queryRequest types.QueryRequest) *types.QueryResult[interface{}] {
18 16
 	// 记录查询开始时间
19 17
 	queryStartTime := time.Now()
20 18
 
21 19
 	// 1. 获取HTTP工厂实例
22 20
 	httpFactory, err := http.GetHTTPFactory()
23 21
 	if err != nil {
24
-		return nil, fmt.Errorf("failed to get HTTP factory: %v", err)
22
+		logger.Errorf("Failed to get HTTP factory: %v", err)
23
+		return &types.QueryResult[interface{}]{
24
+			Success: false,
25
+			Error:   fmt.Sprintf("failed to get HTTP factory: %v", err),
26
+		}
25 27
 	}
26
-	log.Println("HTTP factory created successfully")
28
+	logger.Debug("HTTP factory created successfully")
27 29
 
28 30
 	// 2. 获取Doris工厂实例
29 31
 	dorisFactory, err := doris.GetDorisFactory(httpFactory)
30 32
 	if err != nil {
31
-		return nil, fmt.Errorf("failed to get Doris factory: %v", err)
33
+		logger.Errorf("Failed to get Doris factory: %v", err)
34
+		return &types.QueryResult[interface{}]{
35
+			Success: false,
36
+			Error:   fmt.Sprintf("failed to get Doris factory: %v", err),
37
+		}
32 38
 	}
33
-	log.Println("Doris factory created successfully")
39
+	logger.Debug("Doris factory created successfully")
34 40
 
35 41
 	// 3. 创建HTTP客户端
36 42
 	httpClient := httpFactory.CreateClient()
@@ -41,6 +47,7 @@ func queryToCSVAndInsert(dbFactory *database.DBFactory, queryRequest types.Query
41 47
 		PositionalParams: queryRequest.PositionalParams,
42 48
 		WriterHeader:     queryRequest.WriterHeader,
43 49
 	}
50
+
44 51
 	// 4. 发送POST请求到 /api/query/csv 获取CSV格式数据
45 52
 	resp, err := httpClient.PostWithAuth(
46 53
 		queryRequest.AgentUrl,
@@ -53,29 +60,32 @@ func queryToCSVAndInsert(dbFactory *database.DBFactory, queryRequest types.Query
53 60
 	queryDuration := queryEndTime.Sub(queryStartTime)
54 61
 
55 62
 	if err != nil {
56
-		return &types.QueryResult{
63
+		logger.Errorf("Query request failed: %v", err)
64
+		return &types.QueryResult[interface{}]{
57 65
 			Success:   false,
58 66
 			Error:     fmt.Sprintf("查询失败: %v", err),
59 67
 			QueryTime: queryDuration,
60
-		}, nil
68
+		}
61 69
 	}
62 70
 
63 71
 	if resp.StatusCode() != 200 {
64
-		return &types.QueryResult{
72
+		logger.Errorf("Query request failed with status code: %d", resp.StatusCode())
73
+		return &types.QueryResult[interface{}]{
65 74
 			Success:   false,
66 75
 			Error:     fmt.Sprintf("查询请求失败, 状态码: %d", resp.StatusCode()),
67 76
 			QueryTime: queryDuration,
68
-		}, nil
77
+		}
69 78
 	}
70 79
 
71 80
 	// 5. 获取CSV数据
72 81
 	csvData := string(resp.Body())
73 82
 	if len(csvData) == 0 {
74
-		return &types.QueryResult{
75
-			Success:   false,
83
+		logger.Warn("No data queried")
84
+		return &types.QueryResult[interface{}]{
85
+			Success:   true,
76 86
 			Error:     "没有查询到数据",
77 87
 			QueryTime: queryDuration,
78
-		}, nil
88
+		}
79 89
 	}
80 90
 
81 91
 	// 估算数据行数(CSV行数)
@@ -90,7 +100,7 @@ func queryToCSVAndInsert(dbFactory *database.DBFactory, queryRequest types.Query
90 100
 		totalRows = strings.Count(csvData, "\n")
91 101
 	}
92 102
 
93
-	log.Printf("查询成功,获取到 %d 行数据\n", totalRows)
103
+	logger.Debug("Query successful, retrieved %d rows of data", totalRows)
94 104
 
95 105
 	// 6. 插入数据到Doris
96 106
 	database := queryRequest.DorisDatabase
@@ -103,39 +113,33 @@ func queryToCSVAndInsert(dbFactory *database.DBFactory, queryRequest types.Query
103 113
 	saveDuration := saveEndTime.Sub(saveStartTime)
104 114
 
105 115
 	if err != nil {
106
-		return &types.QueryResult{
116
+		logger.Errorf("Failed to insert data into Doris: %v", err)
117
+		return &types.QueryResult[interface{}]{
107 118
 			Success:   false,
108 119
 			Error:     fmt.Sprintf("数据插入Doris失败: %v", err),
109 120
 			QueryTime: queryDuration,
110 121
 			SaveTime:  saveDuration,
111 122
 			Count:     totalRows,
112
-		}, nil
123
+		}
113 124
 	}
114 125
 
115
-	return &types.QueryResult{
126
+	logger.Debug("Data successfully inserted into Doris: database=%s, table=%s, rows=%d",
127
+		database, table, totalRows)
128
+
129
+	return &types.QueryResult[interface{}]{
116 130
 		Success:   true,
117 131
 		QueryTime: queryDuration,
118 132
 		SaveTime:  saveDuration,
119 133
 		Count:     totalRows,
120
-	}, nil
134
+	}
121 135
 }
122 136
 
123 137
 // ServiceHandler 服务处理器(供router使用)
124
-func ServiceAgentToDoris(dbFactory *database.DBFactory, queryRequest types.QueryRequest) *types.QueryResult {
125
-
138
+func ServiceAgentToDoris(queryRequest types.QueryRequest) *types.QueryResult[interface{}] {
126 139
 	// 添加调试日志,确认 queryRequest 是否有数据
127
-	//log.Printf("SQL: %s", queryRequest.SQL)
128
-	//log.Printf("AgentUrl: %s", queryRequest.AgentUrl)
129
-	//log.Printf("参数数量: %d", len(queryRequest.PositionalParams))
140
+	logger.Debug("Processing query request: SQL=%s, AgentUrl=%s, Params count=%d",
141
+		queryRequest.SQL, queryRequest.AgentUrl, len(queryRequest.PositionalParams))
130 142
 
131 143
 	// 执行查询并插入
132
-	executionResult, err := queryToCSVAndInsert(dbFactory, queryRequest)
133
-	if err != nil {
134
-		return &types.QueryResult{
135
-			Success: false,
136
-			Error:   "服务内部错误: " + err.Error(),
137
-		}
138
-	}
139
-
140
-	return executionResult
144
+	return queryToCSVAndInsert(queryRequest)
141 145
 }

+ 34
- 0
tables/worker_config.go Wyświetl plik

@@ -0,0 +1,34 @@
1
+package tables
2
+
3
+import (
4
+	"time"
5
+
6
+	"git.x2erp.com/qdy/go-db/sqldef"
7
+)
8
+
9
+func init() {
10
+	sqldef.AddRegistration(func(r *sqldef.Registry) {
11
+		// 配置表
12
+		tb := sqldef.NewTable("worker_config", "配置表").
13
+			ID("worker_config_id", 32).Comment("配置主键,主键").End().
14
+			String("package_name", 32).NotNull().Comment("包名称").End().
15
+			String("struct_name", 64).NotNull().Comment("结构体名称").End().
16
+			String("config_tag", 128).Comment("描述)").End().
17
+			JSON("config_value").Comment("配置数据)").End().
18
+			String("creator", 32).NotNull().Comment("创建人").End().
19
+			DateTime("created_at").NotNull().Default("CURRENT_TIMESTAMP").Comment("创建时间").End()
20
+		r.RegisterTable(tb.Build())
21
+	})
22
+}
23
+
24
+type WorkerConfig struct {
25
+	WorkerConfigID string         `gorm:"column:worker_config_id;type:varchar(32);primaryKey;not null;comment:配置主键,主键"`
26
+	PackageName    string         `gorm:"column:package_name;type:varchar(32);not null;comment:包名称"`
27
+	StructName     string         `gorm:"column:struct_name;type:varchar(64);not null;comment:结构体名称"`
28
+	ConfigTag      *string        `gorm:"column:config_tag;type:varchar(128);comment:描述)"`
29
+	ConfigValue    map[string]any `gorm:"column:config_value;type:json;comment:配置数据)"`
30
+	Creator        string         `gorm:"column:creator;type:varchar(32);not null;comment:创建人"`
31
+	CreatedAt      time.Time      `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP;comment:创建时间"`
32
+}
33
+
34
+func (WorkerConfig) TableName() string { return "worker_config" }

+ 69
- 0
test/my_mongodb_test.go Wyświetl plik

@@ -0,0 +1,69 @@
1
+package test
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+	"io"
8
+	"net/http"
9
+	"testing"
10
+
11
+	"git.x2erp.com/qdy/go-svc-worker/internal/domain"
12
+)
13
+
14
+func TestTenantConfigAPI_Simple(t *testing.T) {
15
+	fmt.Println("=== 简单POST测试 ===")
16
+
17
+	// 1. 创建测试数据(赋值)
18
+	config := domain.TenantConfig{
19
+		//ID:               "zhx-1",
20
+		AgentUrl:         "http://localhost:8090/api",
21
+		AgentConcurrency: 50,
22
+	}
23
+	config.ID = "zhx-1"
24
+
25
+	fmt.Printf("测试数据: %v\n", config)
26
+
27
+	// 2. 转换为JSON
28
+	jsonData, err := json.Marshal(config)
29
+	if err != nil {
30
+		fmt.Printf("JSON转换错误: %v\n", err)
31
+		return
32
+	}
33
+
34
+	fmt.Printf("JSON数据: %s\n", string(jsonData))
35
+
36
+	// 3. 创建POST请求
37
+	url := "http://localhost:9090/api/tenant/config" // 你的服务端口
38
+
39
+	// 创建请求
40
+	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
41
+	if err != nil {
42
+		fmt.Printf("创建请求失败: %v\n", err)
43
+		return
44
+	}
45
+
46
+	// 设置请求头
47
+	req.Header.Set("Content-Type", "application/json")
48
+	req.Header.Set("Authorization", "Bearer aaa") // 这里放你的真实token
49
+
50
+	// 创建HTTP客户端并发送请求
51
+	client := &http.Client{}
52
+	resp, err := client.Do(req)
53
+	if err != nil {
54
+		fmt.Printf("POST请求失败: %v\n", err)
55
+		return
56
+	}
57
+	defer resp.Body.Close()
58
+
59
+	// 4. 读取响应
60
+	body, err := io.ReadAll(resp.Body)
61
+	if err != nil {
62
+		fmt.Printf("读取响应失败: %v\n", err)
63
+		return
64
+	}
65
+
66
+	// 5. 打印结果
67
+	fmt.Printf("状态码: %d\n", resp.StatusCode)
68
+	fmt.Printf("响应: %s\n", string(body))
69
+}

Ładowanie…
Anuluj
Zapisz