Преглед изворни кода

feature:更新生成 swagger 功能

liuyaqi пре 3 година
родитељ
комит
81a6c7bf34

+ 7 - 7
opms_parent/app/handler/contract/ctr_contract.go

@@ -12,7 +12,7 @@ import (
 
 type CtrContract struct{}
 
-// swagger:route 合同,测试tag 合同详情
+// Swagger:CtrContract 合同,测试tag 合同详情
 func (c *CtrContract) Get(ctx context.Context, req *model.IdRequiredReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.Get request %#v ", *req)
 	s, err := service.NewCtrContractService(ctx)
@@ -30,7 +30,7 @@ func (c *CtrContract) Get(ctx context.Context, req *model.IdRequiredReq, rsp *co
 	return nil
 }
 
-// swagger:route 合同,测试tag 查询合同
+// Swagger:CtrContract 合同,测试tag 查询合同
 func (c *CtrContract) List(ctx context.Context, req *model.CtrContractListReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.List request %#v ", *req)
 	s, err := service.NewCtrContractService(ctx)
@@ -54,7 +54,7 @@ func (c *CtrContract) List(ctx context.Context, req *model.CtrContractListReq, r
 	return nil
 }
 
-// swagger:route 合同,测试tag 查询合同动态列表
+// Swagger:CtrContract 合同,测试tag 查询合同动态列表
 func (c *CtrContract) DynamicsList(ctx context.Context, req *model.CtrContractDynamicsListReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.GetDynamicsList request %#v ", *req)
 	s, err := service.NewCtrContractService(ctx)
@@ -78,7 +78,7 @@ func (c *CtrContract) DynamicsList(ctx context.Context, req *model.CtrContractDy
 	return nil
 }
 
-// swagger:route 合同,测试tag 新增合同
+// Swagger:CtrContract 合同,测试tag 新增合同
 func (c *CtrContract) Add(ctx context.Context, req *model.CtrContractAddReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.Add request %#v ", *req)
 	s, err := service.NewCtrContractService(ctx)
@@ -96,7 +96,7 @@ func (c *CtrContract) Add(ctx context.Context, req *model.CtrContractAddReq, rsp
 	return nil
 }
 
-// swagger:route 合同,测试tag 更新合同
+// Swagger:CtrContract 合同,测试tag 更新合同
 func (c *CtrContract) Update(ctx context.Context, req *model.CtrContractUpdateReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.Update request %#v ", *req)
 	s, err := service.NewCtrContractService(ctx)
@@ -113,7 +113,7 @@ func (c *CtrContract) Update(ctx context.Context, req *model.CtrContractUpdateRe
 	return nil
 }
 
-// swagger:route 合同,测试tag 转移合同
+// Swagger:CtrContract 合同,测试tag 转移合同
 func (c *CtrContract) Transfer(ctx context.Context, req *model.CtrContractTransferReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.Transfer request %#v ", *req)
 	s, err := service.NewCtrContractService(ctx)
@@ -131,7 +131,7 @@ func (c *CtrContract) Transfer(ctx context.Context, req *model.CtrContractTransf
 }
 
 // 多行注释测试
-// swagger:route 合同,测试tag 删除合同
+// Swagger:CtrContract 合同,测试tag 删除合同
 // 多行注释测试
 func (c *CtrContract) Delete(ctx context.Context, req *model.IdsReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContract.Delete request %#v ", *req)

+ 4 - 4
opms_parent/app/handler/contract/ctr_contract_append.go

@@ -12,7 +12,7 @@ import (
 
 type CtrContractAppend struct{}
 
-// swagger:route 合同附件 查询合同附件
+// Swagger:CtrContractAppend 合同附件 查询合同附件
 func (c *CtrContractAppend) List(ctx context.Context, req *model.CtrContractAppendListReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractAppend.List request %#v ", *req)
 	s, err := service.NewCtrContractAppendService(ctx)
@@ -36,7 +36,7 @@ func (c *CtrContractAppend) List(ctx context.Context, req *model.CtrContractAppe
 	return nil
 }
 
-// swagger:route 合同附件 添加合同附件
+// Swagger:CtrContractAppend 合同附件 添加合同附件
 func (c *CtrContractAppend) Add(ctx context.Context, req *model.CtrContractAppendAddReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractAppend.Add request %#v ", *req)
 	s, err := service.NewCtrContractAppendService(ctx)
@@ -54,7 +54,7 @@ func (c *CtrContractAppend) Add(ctx context.Context, req *model.CtrContractAppen
 	return nil
 }
 
-// swagger:route 合同附件 更新合同附件
+// Swagger:CtrContractAppend 合同附件 更新合同附件
 func (c *CtrContractAppend) Update(ctx context.Context, req *model.CtrContractAppendUpdateReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractAppend.Update request %#v ", *req)
 	s, err := service.NewCtrContractAppendService(ctx)
@@ -71,7 +71,7 @@ func (c *CtrContractAppend) Update(ctx context.Context, req *model.CtrContractAp
 	return nil
 }
 
-// swagger:route 合同附件 删除合同附件
+// Swagger:CtrContractAppend 合同附件 删除合同附件
 func (c *CtrContractAppend) Delete(ctx context.Context, req *model.IdsReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractAppend.Delete request %#v ", *req)
 	s, err := service.NewCtrContractAppendService(ctx)

+ 4 - 4
opms_parent/app/handler/contract/ctr_contract_collection.go

@@ -12,7 +12,7 @@ import (
 
 type CtrContractCollection struct{}
 
-// swagger:route 合同回款 查询合同回款
+// Swagger:CtrContractCollection 合同回款 查询合同回款
 func (c *CtrContractCollection) List(ctx context.Context, req *model.CtrContractCollectionListReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollection.List request %#v ", *req)
 	s, err := service.NewCtrContractCollectionService(ctx)
@@ -36,7 +36,7 @@ func (c *CtrContractCollection) List(ctx context.Context, req *model.CtrContract
 	return nil
 }
 
-// swagger:route 合同回款 添加合同回款
+// Swagger:CtrContractCollection 合同回款 添加合同回款
 func (c *CtrContractCollection) Add(ctx context.Context, req *model.CtrContractCollectionAddReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollection.Add request %#v ", *req)
 	s, err := service.NewCtrContractCollectionService(ctx)
@@ -54,7 +54,7 @@ func (c *CtrContractCollection) Add(ctx context.Context, req *model.CtrContractC
 	return nil
 }
 
-// swagger:route 合同回款 更新合同回款
+// Swagger:CtrContractCollection 合同回款 更新合同回款
 func (c *CtrContractCollection) Update(ctx context.Context, req *model.CtrContractCollectionUpdateReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollection.Update request %#v ", *req)
 	s, err := service.NewCtrContractCollectionService(ctx)
@@ -71,7 +71,7 @@ func (c *CtrContractCollection) Update(ctx context.Context, req *model.CtrContra
 	return nil
 }
 
-// swagger:route 合同回款 删除合同回款
+// Swagger:CtrContractCollection 合同回款 删除合同回款
 func (c *CtrContractCollection) Delete(ctx context.Context, req *model.IdsReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollection.Delete request %#v ", *req)
 	s, err := service.NewCtrContractCollectionService(ctx)

+ 5 - 5
opms_parent/app/handler/contract/ctr_contract_collection_plan.go

@@ -12,7 +12,7 @@ import (
 
 type CtrContractCollectionPlan struct{}
 
-// swagger:route 合同回款计划 合同回款计划详情
+// Swagger:CtrContractCollectionPlan 合同回款计划 合同回款计划详情
 func (c *CtrContractCollectionPlan) Get(ctx context.Context, req *model.IdRequiredReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollectionPlan.Get request %#v ", *req)
 	s, err := service.NewCtrContractCollectionPlanService(ctx)
@@ -30,7 +30,7 @@ func (c *CtrContractCollectionPlan) Get(ctx context.Context, req *model.IdRequir
 	return nil
 }
 
-// swagger:route 合同回款计划 查询合同回款计划
+// Swagger:CtrContractCollectionPlan 合同回款计划 查询合同回款计划
 func (c *CtrContractCollectionPlan) List(ctx context.Context, req *model.CtrContractCollectionPlanListReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollectionPlan.List request %#v ", *req)
 	s, err := service.NewCtrContractCollectionPlanService(ctx)
@@ -54,7 +54,7 @@ func (c *CtrContractCollectionPlan) List(ctx context.Context, req *model.CtrCont
 	return nil
 }
 
-// swagger:route 合同回款计划 添加合同回款计划
+// Swagger:CtrContractCollectionPlan 合同回款计划 添加合同回款计划
 func (c *CtrContractCollectionPlan) Add(ctx context.Context, req *model.CtrContractCollectionPlanAddReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollectionPlan.Add request %#v ", *req)
 	s, err := service.NewCtrContractCollectionPlanService(ctx)
@@ -72,7 +72,7 @@ func (c *CtrContractCollectionPlan) Add(ctx context.Context, req *model.CtrContr
 	return nil
 }
 
-// swagger:route 合同回款计划 更新合同回款计划
+// Swagger:CtrContractCollectionPlan 合同回款计划 更新合同回款计划
 func (c *CtrContractCollectionPlan) Update(ctx context.Context, req *model.CtrContractCollectionPlanUpdateReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollectionPlan.Update request %#v ", *req)
 	s, err := service.NewCtrContractCollectionPlanService(ctx)
@@ -89,7 +89,7 @@ func (c *CtrContractCollectionPlan) Update(ctx context.Context, req *model.CtrCo
 	return nil
 }
 
-// swagger:route 合同回款计划 删除合同回款计划
+// Swagger:CtrContractCollectionPlan 合同回款计划 删除合同回款计划
 func (c *CtrContractCollectionPlan) Delete(ctx context.Context, req *model.IdsReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractCollectionPlan.Delete request %#v ", *req)
 	s, err := service.NewCtrContractCollectionPlanService(ctx)

+ 4 - 4
opms_parent/app/handler/contract/ctr_contract_invoice.go

@@ -12,7 +12,7 @@ import (
 
 type CtrContractInvoice struct{}
 
-// swagger:route 合同发票 查询合同发票
+// Swagger:CtrContractInvoice 合同发票 查询合同发票
 func (c *CtrContractInvoice) List(ctx context.Context, req *model.CtrContractInvoiceListReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractInvoice.List request %#v ", *req)
 	s, err := service.NewCtrContractInvoiceService(ctx)
@@ -36,7 +36,7 @@ func (c *CtrContractInvoice) List(ctx context.Context, req *model.CtrContractInv
 	return nil
 }
 
-// swagger:route 合同发票 添加合同发票
+// Swagger:CtrContractInvoice 合同发票 添加合同发票
 func (c *CtrContractInvoice) Add(ctx context.Context, req *model.CtrContractInvoiceAddReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractInvoice.Add request %#v ", *req)
 	s, err := service.NewCtrContractInvoiceService(ctx)
@@ -54,7 +54,7 @@ func (c *CtrContractInvoice) Add(ctx context.Context, req *model.CtrContractInvo
 	return nil
 }
 
-// swagger:route 合同发票 更新合同发票
+// Swagger:CtrContractInvoice 合同发票 更新合同发票
 func (c *CtrContractInvoice) Update(ctx context.Context, req *model.CtrContractInvoiceUpdateReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractInvoice.Update request %#v ", *req)
 	s, err := service.NewCtrContractInvoiceService(ctx)
@@ -71,7 +71,7 @@ func (c *CtrContractInvoice) Update(ctx context.Context, req *model.CtrContractI
 	return nil
 }
 
-// swagger:route 合同发票 删除合同发票
+// Swagger:CtrContractInvoice 合同发票 删除合同发票
 func (c *CtrContractInvoice) Delete(ctx context.Context, req *model.IdsReq, rsp *comm_def.CommonMsg) error {
 	g.Log().Infof("CtrContractInvoice.Delete request %#v ", *req)
 	s, err := service.NewCtrContractInvoiceService(ctx)

+ 2 - 1
opms_parent/go.mod

@@ -13,7 +13,8 @@ require (
 	golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect
 	golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 // indirect
 	golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
-	gopkg.in/yaml.v3 v3.0.0 // indirect
+	golang.org/x/tools v0.1.1
+	gopkg.in/yaml.v3 v3.0.0
 )
 
 replace dashoo.cn/opms_libary => ../opms_libary

+ 2 - 2
opms_parent/main.go

@@ -87,7 +87,7 @@ func swaggerui() {
 	if !g.Config().GetBool("setting.swagger") {
 		return
 	}
-	glog.Info("start swagger at :8080")
-	glog.Error(http.ListenAndServe(":8080",
+	glog.Info("start swagger at :18080")
+	glog.Error(http.ListenAndServe(":18080",
 		http.FileServer(http.Dir("./swaggerui/"))))
 }

+ 4 - 4
opms_parent/swaggerui/readme.md

@@ -1,12 +1,12 @@
 
 
-- 在 handler 中要生成文档的函数上添加注释
+- 在 handler 中要生成文档的函数上,按照以下格式添加注释
 
-    `// swagger:route tag1,tag2 接口名称`
+    `// Swagger:ServicePath tag1,tag2 接口名称`
 
     例如
 
-    `// swagger:route 合同 转移合同`
+    `// Swagger:CtrContract 合同 转移合同`
 
 - 然后运行下面的命令生成 swagger.yml
 
@@ -16,4 +16,4 @@
 
     `swagger = true`
 
-    启动服务即可在 8080 端口访问到 swagger 文档
+    启动服务即可在 18080 端口访问到 swagger 文档

+ 6 - 1
opms_parent/swaggerui/swagger.go

@@ -402,7 +402,8 @@ func detectPath(fd *ast.FuncDecl) {
 	debugLog("//////////%v %s %s %s %s", fd, fname, recv, req, fd.Doc.Text())
 	commentText := fd.Doc.Text()
 	for _, c := range strings.Split(commentText, "\n") {
-		if !strings.HasPrefix(c, "swagger:route") {
+		c = strings.TrimSpace(c)
+		if !strings.HasPrefix(c, "Swagger:") {
 			continue
 		}
 		names := strings.Split(c, " ")
@@ -411,6 +412,10 @@ func detectPath(fd *ast.FuncDecl) {
 		if len(names) == 3 {
 			tags = strings.Split(names[1], ",")
 			summary = names[2]
+			recvNamed := strings.Split(names[0], ":")
+			if len(recvNamed) == 2 {
+				recv = recvNamed[1]
+			}
 		}
 		path := fmt.Sprintf("/%s.%s", recv, fname)
 		operationId := recv + fname