Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/config"
  6. "git.x2erp.com/qdy/go-base/consul"
  7. "git.x2erp.com/qdy/go-base/container"
  8. "git.x2erp.com/qdy/go-base/ctx"
  9. "git.x2erp.com/qdy/go-base/logger"
  10. "git.x2erp.com/qdy/go-base/middleware"
  11. "git.x2erp.com/qdy/go-base/model/request"
  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. dbsFactory := container.Create(ctr, database.CreateDBSFactory)
  35. // 赋值认证中间件参数
  36. middleware.JWTAuthMiddlewareInit(cfg)
  37. //得到webservice服务工厂
  38. webxFactory := webx.GetWebServiceFactory()
  39. //建立hhtpService服务
  40. webServcie, _ := webxFactory.CreateService(cfg.GetServiceConfig())
  41. //建立路由-api
  42. routerService := router.NewWebService(webServcie.GetRouter())
  43. //注册路由--api
  44. registerDefaultRouter(routerService, dbsFactory)
  45. // 注册健康检查-api
  46. health.RegisterConsulHealthCheck(routerService)
  47. //启动服务
  48. webServcie.Run()
  49. //启用运行日志
  50. container.Create(ctr, logger.InitRuntimeLogger)
  51. //注册到注册中心
  52. container.Create(ctr, consul.Register)
  53. //等待关闭
  54. webServcie.WaitForServiceShutdown(ctr)
  55. }
  56. func registerDefaultRouter(ws *router.RouterService, dbsFactory *database.DBSFactory) {
  57. ws.POST("/api/query/json/{dbname}",
  58. func(dbname string, req request.QueryRequest, reqCtx *ctx.RequestContext) (*response.QueryResult[[]map[string]interface{}], error) {
  59. return functions.QueryToJSON(dbname, dbsFactory, req, reqCtx)
  60. },
  61. ).Use(middleware.JWTAuthMiddleware).Register()
  62. ws.POST("/api/query/csv/{dbname}",
  63. func(dbname string, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
  64. return functions.QueryToCSV(dbname, dbsFactory, req, reqCtx)
  65. },
  66. ).Use(middleware.JWTAuthMiddleware).Register()
  67. ws.POST("/api/query/csv/param/{dbname}",
  68. func(dbname string, req request.QueryRequest, reqCtx *ctx.RequestContext) ([]byte, error) {
  69. return functions.QueryPositionalToCSV(dbname, dbsFactory, req, reqCtx)
  70. },
  71. ).Use(middleware.JWTAuthMiddleware).Register()
  72. }
  73. func getDBFactory(dbname string, dbsFactory *database.DBSFactory) (*database.DBFactory, error) {
  74. return dbsFactory.CreateDBFactory(dbname)
  75. }