| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package main
-
- import (
- "git.x2erp.com/qdy/go-db/factory/mongodb"
- "git.x2erp.com/qdy/go-svc-core/internal/model"
- "git.x2erp.com/qdy/go-svc-core/internal/service"
-
- "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/model/response"
- "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-core"
- 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()
-
- ws.POST("/api/tenant/config",
- func(req model.TenantConfig, reqCtx *ctx.RequestContext) (*response.QueryResult[interface{}], error) {
-
- return service.SaveTenantConfig(req, mongoDBFactory, reqCtx)
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- ws.POST("/api/etl/config",
- func(req model.ETLConfig, reqCtx *ctx.RequestContext) (*response.QueryResult[interface{}], error) {
-
- return service.SaveETLConfig(req, mongoDBFactory, reqCtx)
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- }
-
- // 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
- }
|