Ingen beskrivning
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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "git.x2erp.com/qdy/go-base/bootstraps"
  6. "git.x2erp.com/qdy/go-base/middleware"
  7. "git.x2erp.com/qdy/go-db/factory/database"
  8. "git.x2erp.com/qdy/go-db/myhandle"
  9. "git.x2erp.com/qdy/go-db/sqldef"
  10. "git.x2erp.com/qdy/go-svc-agent/functions"
  11. "go-micro.dev/v4/web"
  12. // 导入表定义包,触发 init() 函数
  13. _ "git.x2erp.com/qdy/go-svc-configure/tables" // 确保这里的路径正确
  14. )
  15. var (
  16. serviceName = "svc-agent"
  17. serviceVersion = "1.0.0"
  18. )
  19. func main() {
  20. // 创建服务启动器
  21. bootstrapper := bootstraps.NewServiceBootstrapper(serviceName, serviceVersion)
  22. //加载配置
  23. bootstrapper.InitConfig()
  24. //构建数据库工厂
  25. bootstrapper.InitDatabase()
  26. defer bootstrapper.CleanupDatabase()
  27. //创建表到数据库
  28. cretetabel(bootstrapper.DbFactory)
  29. // 启动服务,传入路由注册函数
  30. bootstrapper.Run(registerRoutes)
  31. }
  32. // 注册所有路由
  33. func registerRoutes(webService web.Service, dbFactory *database.DBFactory) {
  34. // 查询接口 - JSON
  35. webService.Handle("/api/query/json", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  36. myhandle.QueryHandlerJson(w, r, dbFactory, functions.QueryToJSON)
  37. })))
  38. // 查询接口 - CSV
  39. webService.Handle("/api/query/csv", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  40. myhandle.QueryHandlerBytes(w, r, dbFactory, functions.QueryToCSV)
  41. })))
  42. // 查询接口 - CSV with positional params
  43. webService.Handle("/api/query/csv/param", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  44. myhandle.QueryHandlerBytes(w, r, dbFactory, functions.QueryPositionalToCSV)
  45. })))
  46. }
  47. func cretetabel(factory *database.DBFactory) {
  48. // 获取数据库连接和类型
  49. db := factory.GetDB()
  50. dbType := factory.GetDBType()
  51. // 创建表同步器
  52. syncer, err := sqldef.NewTableSyncer(db, dbType)
  53. if err != nil {
  54. log.Fatalf("创建 - 建立器失败: %v", err)
  55. }
  56. // 创建表
  57. if err := syncer.RecreateTables(); err != nil {
  58. log.Fatalf("建表失败: %v", err)
  59. }
  60. log.Println("数据库表建立完成!")
  61. }