feat:Routes are added directly to the structure

This commit is contained in:
wenjianzhang
2020-07-22 18:08:39 +08:00
parent 563b7ea10c
commit bad111a633
3 changed files with 73 additions and 10 deletions
+21
View File
@@ -8,6 +8,7 @@ import (
tools2 "go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
"log"
"net/http"
"text/template"
)
@@ -90,6 +91,26 @@ func GenCode(c *gin.Context) {
tools2.FileCreate(b3, "./router/"+tab.PackageName+".go")
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.PackageName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.PackageName+"/index.vue")
oldTest:="// {{认证路由自动补充在此处请勿删除}}"
newText:="// {{认证路由自动补充在此处请勿删除}} \r\n register"+tab.ClassName+"Router(v1,authMiddleware)"
helper := tools2.ReplaceHelper{
Root: "./router/router.go",
OldText:oldTest,
NewText: newText,
}
if helper.OldText == helper.NewText{
log.Println("error !! the NewText isEqual the OldText")
return
}
if err := helper.DoWrok(); err != nil {
log.Print("error:", err.Error())
} else {
log.Print("done!")
}
app.OK(c, "", "代码生成成功!")
}
+7 -10
View File
@@ -23,22 +23,19 @@ func InitExamplesRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gi
// 无需认证的路由示例
func examplesNoCheckRoleRouter(r *gin.Engine) {
//v1 := r.Group("/api/v1")
//v1.GET("/examples/list", examples.apis)
v1 := r.Group("/api/v1")
v1.GET("/nilcheckrole", nil)
// {{无需认证路由自动补充在此处请勿删除}}
}
// 需要认证的路由示例
func examplesCheckRoleRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddleware) {
//v1 := r.Group("/api/v1")
//v1auth := v1.Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
//{
// registerDemoRouter(v1auth)
//}
v1 := r.Group("/api/v1")
v1.GET("/checkrole",nil)
// {{认证路由自动补充在此处请勿删除}}
}
//func registerDemoRouter(v1 gin.IRoutes) {
// v1.GET("/demo", Demo)
//}
+45
View File
@@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@@ -112,3 +113,47 @@ func FileZip(dst, src string,notContPath string) (err error) {
return nil
})
}
type ReplaceHelper struct {
Root string //路径
OldText string //需要替换的文本
NewText string //新的文本
}
func (h *ReplaceHelper) DoWrok() error {
return filepath.Walk(h.Root, h.walkCallback)
}
func (h ReplaceHelper) walkCallback(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f == nil {
return nil
}
if f.IsDir() {
log.Println("DIR:",path)
return nil
}
//文件类型需要进行过滤
buf, err := ioutil.ReadFile(path)
if err != nil {
//err
return err
}
content := string(buf)
log.Printf("h.OldText: %s \n",h.OldText)
log.Printf("h.NewText: %s \n",h.NewText)
//替换
newContent := strings.Replace(content, h.OldText, h.NewText, -1)
//重新写入
ioutil.WriteFile(path, []byte(newContent), 0)
return err
}