| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package main
-
- import (
- "log"
-
- "git.x2erp.com/qdy/go-db/dbstart"
- "git.x2erp.com/qdy/go-db/factory/database"
-
- "git.x2erp.com/qdy/go-base/bootstraps"
- "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/webx"
- )
-
- var (
- serviceName = "svc-ai"
- serviceVersion = "1"
- )
-
- // 定义结构体
- type CreateUserRequest struct {
- Name string `json:"name"`
- Email string `json:"email"`
- Age int `json:"age"`
- }
-
- type UserResponse struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Email string `json:"email"`
- Data []map[string]interface{}
- }
-
- func main() {
- // 1. 初始化基础启动器
- boot := bootstraps.NewBootstrapper(serviceName, serviceVersion)
-
- // 2. 初始化数据库(都在bootstraps包中)
- dbBoot := dbstart.NewDBBootstrapper(boot.GetConfig())
- dbBoot.Init()
-
- agentDB := dbBoot.GetDBFactory("agent")
- // 赋值认证中间件参数
- middleware.JWTAuthMiddlewareInit(boot.GetConfig())
-
- // 3. 启动服务
- //boot.StartService("default")
-
- router := boot.GetRouter("default")
- // 3. 创建 WebService 并传入数据库接口
- ws := webx.NewWebService(router)
-
- //使用默认数据库
- registerDefaultRouter(ws, dbBoot.GetDefaultDBFactory())
-
- //启动第2个服务
- routerDoris := boot.GetRouter("doris")
- wsDoris := webx.NewWebService(routerDoris)
- registerDorisRouter(wsDoris, agentDB)
-
- // 6. 运行服务
- boot.Run(dbBoot)
- }
-
- func registerDorisRouter(ws *webx.WebService, dbFactory *database.DBFactory) {
- // GET示例:路径参数绑定
- ws.GET("/app/users/{id}",
- func(id string, reqCtx *ctx.RequestContext) (UserResponse, error) {
-
- log.Print("ctx TenantID:", reqCtx.TenantID)
- // id 自动从路径绑定
- // 注意:webx版本没有自动注入dbFactory
- return getUserOracle(id, dbFactory) // 需要修改getUser函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- // POST示例:Body参数绑定
- ws.POST("/app/users",
- func(req CreateUserRequest, reqCtx *ctx.RequestContext) (UserResponse, error) {
- log.Print("ctx TenantID:", reqCtx.TenantID)
- // req 自动从JSON Body绑定
- return createUser(req, nil) // 需要修改createUser函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- // 您的业务路由
- ws.POST("/app/query/yaml",
- func() (interface{}, error) {
- return queryYaml(nil) // 需要修改queryYaml函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- ws.POST("/app/init/config/template",
- func() (interface{}, error) {
- return initConfigTemplate(nil) // 需要修改initConfigTemplate函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- }
-
- func registerDefaultRouter(ws *webx.WebService, dbFactory *database.DBFactory) {
- // 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()
-
- // POST示例:Body参数绑定
- ws.POST("/api/users",
- func(req CreateUserRequest, reqCtx *ctx.RequestContext) (UserResponse, error) {
- log.Print("ctx TenantID:", reqCtx.TenantID)
- // req 自动从JSON Body绑定
- return createUser(req, nil) // 需要修改createUser函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- // 您的业务路由
- ws.POST("/api/query/yaml",
- func() (interface{}, error) {
- return queryYaml(nil) // 需要修改queryYaml函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- ws.POST("/api/init/config/template",
- func() (interface{}, error) {
- return initConfigTemplate(nil) // 需要修改initConfigTemplate函数以获取dbFactory
- },
- ).Use(middleware.JWTAuthMiddleware).Register()
-
- }
-
- // 4. 创建路由器
- // 首先需要创建标准库的ServeMux
- //mux := http.NewServeMux()
- //router := webx.NewRouter(boot.GetRouter())
- //router := webx.NewWebService(mux)
-
- // 5. 注册路由(Spring风格!)
-
- func getUser(id string, dbFactory *database.DBFactory) (UserResponse, error) {
- if dbFactory != nil {
- dbFactory.TestConnection(dbFactory.GetDBType())
- }
-
- // 查询数据库...
- return UserResponse{
- ID: id,
- Name: "掌扇过去-AAAAA",
- Email: "zhangsan@example.com",
- }, nil
- }
-
- // 业务函数示例
- func getUserOracle(id string, dbFactory *database.DBFactory) (UserResponse, error) {
- if dbFactory != nil {
- dbFactory.TestConnection(dbFactory.GetDBType())
- }
-
- sql, queryParams := getSQLWithPaginationSQL(0, 10)
-
- jsonData := dbFactory.QueryPositionalToJSON(sql, queryParams, nil)
- // 查询数据库...
- return UserResponse{
- ID: id,
- Name: "掌扇过去",
- Email: "zhangsan@example.com",
- Data: jsonData.Data,
- }, nil
- }
-
- func createUser(req CreateUserRequest, dbFactory *database.DBFactory) (UserResponse, error) {
- logger.Debug("CreateUserRequest:%v", req)
- return UserResponse{
- ID: "1",
- Name: req.Name,
- Email: req.Email,
- }, nil
- }
-
- func queryYaml(dbFactory *database.DBFactory) (interface{}, error) {
- // 您的业务逻辑
- return map[string]interface{}{"message": "query yaml success"}, nil
- }
-
- func initConfigTemplate(dbFactory *database.DBFactory) (interface{}, error) {
- // 您的业务逻辑
- return map[string]interface{}{"message": "init config success"}, nil
- }
-
- // 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
- }
|