package main import ( "log" "net/http" "git.x2erp.com/qdy/go-base/bootstraps" "git.x2erp.com/qdy/go-base/middleware" "git.x2erp.com/qdy/go-db/factory/database" "git.x2erp.com/qdy/go-db/myhandle" "git.x2erp.com/qdy/go-db/sqldef" "git.x2erp.com/qdy/go-svc-agent/functions" "go-micro.dev/v4/web" // 导入表定义包,触发 init() 函数 _ "git.x2erp.com/qdy/go-svc-configure/tables" // 确保这里的路径正确 ) var ( serviceName = "svc-agent" serviceVersion = "1.0.0" ) func main() { // 创建服务启动器 bootstrapper := bootstraps.NewServiceBootstrapper(serviceName, serviceVersion) //加载配置 bootstrapper.InitConfig() //构建数据库工厂 bootstrapper.InitDatabase() defer bootstrapper.CleanupDatabase() //创建表到数据库 cretetabel(bootstrapper.DbFactory) // 启动服务,传入路由注册函数 bootstrapper.Run(registerRoutes) } // 注册所有路由 func registerRoutes(webService web.Service, dbFactory *database.DBFactory) { // 查询接口 - JSON webService.Handle("/api/query/json", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { myhandle.QueryHandlerJson(w, r, dbFactory, functions.QueryToJSON) }))) // 查询接口 - CSV webService.Handle("/api/query/csv", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { myhandle.QueryHandlerBytes(w, r, dbFactory, functions.QueryToCSV) }))) // 查询接口 - CSV with positional params webService.Handle("/api/query/csv/param", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { myhandle.QueryHandlerBytes(w, r, dbFactory, functions.QueryPositionalToCSV) }))) } func cretetabel(factory *database.DBFactory) { // 获取数据库连接和类型 db := factory.GetDB() dbType := factory.GetDBType() // 创建表同步器 syncer, err := sqldef.NewTableSyncer(db, dbType) if err != nil { log.Fatalf("创建 - 建立器失败: %v", err) } // 创建表 if err := syncer.RecreateTables(); err != nil { log.Fatalf("建表失败: %v", err) } log.Println("数据库表建立完成!") }