No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import (
  3. "git.x2erp.com/qdy/go-db/factory/database"
  4. "git.x2erp.com/qdy/go-svc-agent/functions"
  5. "git.x2erp.com/qdy/go-base/auth"
  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/graceful"
  11. "git.x2erp.com/qdy/go-base/logger"
  12. "git.x2erp.com/qdy/go-base/model/request/dorisreq"
  13. "git.x2erp.com/qdy/go-base/model/response"
  14. "git.x2erp.com/qdy/go-base/webx"
  15. "git.x2erp.com/qdy/go-base/webx/health"
  16. "git.x2erp.com/qdy/go-base/webx/router"
  17. )
  18. var (
  19. appName = "svc-core"
  20. appVersion = "1"
  21. )
  22. func main() {
  23. //0.日志
  24. //logger.InitBootLog()
  25. logBootFactory := logger.InitBootLog()
  26. //1.获取配置文件
  27. cfg := config.GetConfig()
  28. cfg.SetAppName(appName)
  29. cfg.SetAppVersion(appVersion)
  30. //2.创建关闭容器
  31. ctr := container.NewContainer(cfg)
  32. //注册日志,实现自动关闭
  33. container.Reg(ctr, logBootFactory)
  34. //3.创建数据库工厂--如果需求
  35. dbsFactory := container.Create(ctr, database.CreateDBSFactory)
  36. // 赋值认证中间件参数
  37. //authJWTAuthMiddlewareInit(cfg)
  38. //得到webservice服务工厂
  39. webxFactory := webx.GetWebServiceFactory()
  40. //建立hhtpService服务
  41. webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig())
  42. //建立路由-api
  43. routerService := router.NewWebService(webServcie.GetRouter())
  44. //注册路由--api
  45. registerDefaultRouter(routerService, dbsFactory)
  46. // 注册健康检查-api
  47. health.RegisterConsulHealthCheck(routerService)
  48. //启动服务
  49. webServcie.Run()
  50. //启用运行日志
  51. container.Create(ctr, logger.InitRuntimeLogger)
  52. //注册到注册中心
  53. container.Create(ctr, consul.Register)
  54. //等待关闭
  55. graceful.WaitForShutdown(cfg.GetServiceConfig().ServiceName, ctr, webServcie.GetServer())
  56. }
  57. func registerDefaultRouter(ws *router.RouterService, dbsFactory *database.DBSFactory) {
  58. ws.POST("/api/query/json/{dbname}",
  59. func(dbname string, req dorisreq.QueryRequest, reqCtx *ctx.RequestContext) (*response.QueryResult[[]map[string]interface{}], error) {
  60. return functions.QueryToJSON(dbname, dbsFactory, req, reqCtx)
  61. },
  62. ).Use(auth.TokenAuth).Register()
  63. ws.POST("/api/query/csv/{dbname}",
  64. func(dbname string, req dorisreq.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
  65. return functions.QueryToCSV(dbname, dbsFactory, req, reqCtx)
  66. },
  67. ).Use(auth.TokenAuth).Register()
  68. ws.POST("/api/query/csv/param/{dbname}",
  69. func(dbname string, req dorisreq.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
  70. return functions.QueryPositionalToCSV(dbname, dbsFactory, req, reqCtx)
  71. },
  72. ).Use(auth.TokenAuth).Register()
  73. }