説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "git.x2erp.com/qdy/go-db/factory/mongodb"
  4. "git.x2erp.com/qdy/go-svc-core/internal/model"
  5. "git.x2erp.com/qdy/go-svc-core/internal/service"
  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/ctx"
  10. "git.x2erp.com/qdy/go-base/logger"
  11. "git.x2erp.com/qdy/go-base/middleware"
  12. "git.x2erp.com/qdy/go-base/model/response"
  13. "git.x2erp.com/qdy/go-base/webx"
  14. "git.x2erp.com/qdy/go-base/webx/health"
  15. "git.x2erp.com/qdy/go-base/webx/router"
  16. )
  17. var (
  18. appName = "svc-core"
  19. appVersion = "1"
  20. )
  21. func main() {
  22. //0.日志
  23. //logger.InitBootLog()
  24. logBootFactory := logger.InitBootLog()
  25. //1.获取配置文件
  26. cfg := config.GetConfig()
  27. cfg.SetAppName(appName)
  28. cfg.SetAppVersion(appVersion)
  29. //2.创建关闭容器
  30. ctr := container.NewContainer(cfg)
  31. //注册日志,实现自动关闭
  32. container.Reg(ctr, logBootFactory)
  33. //3.创建数据库工厂--如果需求
  34. //dbFactory := container.Create(ctr, database.CreateDBFactory)
  35. // 赋值认证中间件参数
  36. middleware.JWTAuthMiddlewareInit(cfg)
  37. //测试数据库连接
  38. //dbFactory.TestConnection()
  39. // 创建mongodb
  40. mongoDBFactory := container.Create(ctr, mongodb.CreateFactory)
  41. mongoDBFactory.TestConnection()
  42. //得到webservice服务工厂
  43. webxFactory := webx.GetWebServiceFactory()
  44. //建立hhtpService服务
  45. webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig())
  46. //建立路由-api
  47. routerService := router.NewWebService(webServcie.GetRouter())
  48. //注册路由--api
  49. registerDefaultRouter(routerService, mongoDBFactory)
  50. // 注册健康检查-api
  51. health.RegisterConsulHealthCheck(routerService)
  52. //启动服务
  53. webServcie.Run()
  54. //启用运行日志
  55. container.Create(ctr, logger.InitRuntimeLogger)
  56. //注册到注册中心
  57. container.Create(ctr, consul.Register)
  58. //等待关闭
  59. webServcie.WaitForServiceShutdown(ctr)
  60. }
  61. func registerDefaultRouter(ws *router.RouterService, mongoDBFactory *mongodb.MongoDBFactory) {
  62. // // GET示例:路径参数绑定
  63. // ws.GET("/api/users/{id}",
  64. // func(id string, reqCtx *ctx.RequestContext) (UserResponse, error) {
  65. // log.Print("ctx TenantID:", reqCtx.TenantID)
  66. // // id 自动从路径绑定
  67. // // 注意:webx版本没有自动注入dbFactory
  68. // return getUser(id, dbFactory) // 需要修改getUser函数以获取dbFactory
  69. // },
  70. // ).Use(middleware.JWTAuthMiddleware).Register()
  71. ws.POST("/api/tenant/config",
  72. func(req model.TenantConfig, reqCtx *ctx.RequestContext) (*response.QueryResult[interface{}], error) {
  73. return service.SaveTenantConfig(req, mongoDBFactory, reqCtx)
  74. },
  75. ).Use(middleware.JWTAuthMiddleware).Register()
  76. ws.POST("/api/etl/config",
  77. func(req model.ETLConfig, reqCtx *ctx.RequestContext) (*response.QueryResult[interface{}], error) {
  78. return service.SaveETLConfig(req, mongoDBFactory, reqCtx)
  79. },
  80. ).Use(middleware.JWTAuthMiddleware).Register()
  81. }
  82. // getSQLWithPagination 生成带分页的SQL语句(参数模式)
  83. // 返回SQL语句和参数映射
  84. func GetSQLWithPaginationSQL(startRow, endRow int) (string, []interface{}) {
  85. sql := `SELECT
  86. CLOTHING_ID,
  87. CLOTHING_YEAR,
  88. CLOTHING_NAME
  89. FROM (
  90. SELECT a.*, ROWNUM as rn
  91. FROM (
  92. SELECT *
  93. FROM X6_STOCK_DEV.A3_CLOTHING
  94. ORDER BY CLOTHING_ID
  95. ) a
  96. WHERE ROWNUM <= :1
  97. )
  98. WHERE rn > :2`
  99. // 创建参数映射
  100. params := []interface{}{
  101. endRow,
  102. startRow - 1, // WHERE rn > :start_row 所以是startRow-1
  103. }
  104. return sql, params
  105. }