package main import ( "encoding/json" "log" "git.x2erp.com/qdy/go-db/factory/database" "git.x2erp.com/qdy/go-db/factory/mongodb" "git.x2erp.com/qdy/go-svc-worker/internal/domain" "git.x2erp.com/qdy/go-base/config" "git.x2erp.com/qdy/go-base/consul" "git.x2erp.com/qdy/go-base/container" "git.x2erp.com/qdy/go-base/ctx" "git.x2erp.com/qdy/go-base/logger" "git.x2erp.com/qdy/go-base/middleware" "git.x2erp.com/qdy/go-base/types" "git.x2erp.com/qdy/go-base/webx" "git.x2erp.com/qdy/go-base/webx/health" "git.x2erp.com/qdy/go-base/webx/router" ) var ( appName = "svc-worker" appVersion = "1" ) func main() { //0.日志 //logger.InitBootLog() logBootFactory := logger.InitBootLog() //1.获取配置文件 cfg := config.GetConfig() cfg.SetAppName(appName) cfg.SetAppVersion(appVersion) //2.创建关闭容器 ctr := container.NewContainer(cfg) //注册日志,实现自动关闭 container.Reg(ctr, logBootFactory) //3.创建数据库工厂--如果需求 dbFactory := container.Create(ctr, database.CreateDBFactory) // 赋值认证中间件参数 middleware.JWTAuthMiddlewareInit(cfg) //测试数据库连接 dbFactory.TestConnection() // 创建mongodb mongoDBFactory := container.Create(ctr, mongodb.CreateFactory) mongoDBFactory.TestConnection() //得到webservice服务工厂 webxFactory := webx.GetWebServiceFactory() //建立hhtpService服务 webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig()) //建立路由-api routerService := router.NewWebService(webServcie.GetRouter()) //注册路由--api registerDefaultRouter(routerService, mongoDBFactory) // 注册健康检查-api health.RegisterConsulHealthCheck(routerService) //启动服务 webServcie.Run() //启用运行日志 container.Create(ctr, logger.InitRuntimeLogger) //注册到注册中心 container.Create(ctr, consul.Register) //等待关闭 webServcie.WaitForServiceShutdown(ctr) } func registerDefaultRouter(ws *router.RouterService, mongoDBFactory *mongodb.MongoDBFactory) { // // GET示例:路径参数绑定 // ws.GET("/api/users/{id}", // func(id string, reqCtx *ctx.RequestContext) (UserResponse, error) { // log.Print("ctx TenantID:", reqCtx.TenantID) // // id 自动从路径绑定 // // 注意:webx版本没有自动注入dbFactory // return getUser(id, dbFactory) // 需要修改getUser函数以获取dbFactory // }, // ).Use(middleware.JWTAuthMiddleware).Register() // POST示例:Body参数绑定 ws.POST("/api/tenant/config", func(req domain.TenantConfig, reqCtx *ctx.RequestContext) (*types.QueryResult[interface{}], error) { log.Print("ctx TenantID:", reqCtx.TenantID) //log.Print("mongoDBFactory:", mongoDBFactory.GetConfig().URI) //log.Print("dbFactory:", dbFactory.GetDBName()) jsonBytes, _ := json.Marshal(req) log.Printf("TenantConfig :%s", string(jsonBytes)) ok := mongoDBFactory.InsertOne("tenant_configs", req) log.Print("TenantConfig InsertOne:", ok) // req 自动从JSON Body绑定 return &types.QueryResult[interface{}]{ Success: ok, Data: mongoDBFactory.GetConfig().URI, }, nil }, ).Use(middleware.JWTAuthMiddleware).Register() // 您的业务路由 ws.POST("/api/query/yaml", func() (interface{}, error) { return queryYaml(nil) // 需要修改queryYaml函数以获取dbFactory }, ).Use(middleware.JWTAuthMiddleware).Register() ws.POST("/api/init/config/template", func() (interface{}, error) { return initConfigTemplate(nil) // 需要修改initConfigTemplate函数以获取dbFactory }, ).Use(middleware.JWTAuthMiddleware).Register() } func queryYaml(dbFactory *database.DBFactory) (interface{}, error) { // 您的业务逻辑 return map[string]interface{}{"message": "query yaml success"}, nil } func initConfigTemplate(dbFactory *database.DBFactory) (interface{}, error) { // 您的业务逻辑 return map[string]interface{}{"message": "init config success"}, nil } // getSQLWithPagination 生成带分页的SQL语句(参数模式) // 返回SQL语句和参数映射 func getSQLWithPaginationSQL(startRow, endRow int) (string, []interface{}) { sql := `SELECT CLOTHING_ID, CLOTHING_YEAR, CLOTHING_NAME FROM ( SELECT a.*, ROWNUM as rn FROM ( SELECT * FROM X6_STOCK_DEV.A3_CLOTHING ORDER BY CLOTHING_ID ) a WHERE ROWNUM <= :1 ) WHERE rn > :2` // 创建参数映射 params := []interface{}{ endRow, startRow - 1, // WHERE rn > :start_row 所以是startRow-1 } return sql, params }