2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
package beego
|
2014-06-09 02:11:37 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-10 09:11:02 +00:00
|
|
|
"encoding/json"
|
2014-06-09 09:33:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2014-06-10 09:11:02 +00:00
|
|
|
"io/ioutil"
|
2014-06-09 02:11:37 +00:00
|
|
|
"os"
|
2016-04-27 15:57:22 +00:00
|
|
|
"path/filepath"
|
2017-04-25 13:00:49 +00:00
|
|
|
"regexp"
|
2015-06-08 09:25:46 +00:00
|
|
|
"sort"
|
2017-04-25 13:00:49 +00:00
|
|
|
"strconv"
|
2014-06-09 09:33:04 +00:00
|
|
|
"strings"
|
2017-04-25 13:00:49 +00:00
|
|
|
"unicode"
|
2014-06-10 09:11:02 +00:00
|
|
|
|
2017-04-25 15:39:42 +00:00
|
|
|
"github.com/astaxie/beego/context/param"
|
2016-03-24 09:39:29 +00:00
|
|
|
"github.com/astaxie/beego/logs"
|
2014-06-10 09:11:02 +00:00
|
|
|
"github.com/astaxie/beego/utils"
|
2014-06-09 02:11:37 +00:00
|
|
|
)
|
|
|
|
|
2019-04-22 14:18:37 +00:00
|
|
|
var globalRouterTemplate = `package {{.routersDir}}
|
2014-06-09 02:11:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego"
|
2018-09-07 04:14:13 +00:00
|
|
|
"github.com/astaxie/beego/context/param"{{.globalimport}}
|
2014-06-09 02:11:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-06-08 09:25:46 +00:00
|
|
|
{{.globalinfo}}
|
2014-06-09 02:11:37 +00:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-06-10 09:11:02 +00:00
|
|
|
var (
|
2015-09-08 13:45:45 +00:00
|
|
|
lastupdateFilename = "lastupdate.tmp"
|
2014-11-05 14:23:54 +00:00
|
|
|
commentFilename string
|
2014-06-10 09:11:02 +00:00
|
|
|
pkgLastupdate map[string]int64
|
|
|
|
genInfoList map[string][]ControllerComments
|
2018-09-07 04:14:13 +00:00
|
|
|
|
|
|
|
routerHooks = map[string]int{
|
|
|
|
"beego.BeforeStatic": BeforeStatic,
|
|
|
|
"beego.BeforeRouter": BeforeRouter,
|
|
|
|
"beego.BeforeExec": BeforeExec,
|
|
|
|
"beego.AfterExec": AfterExec,
|
|
|
|
"beego.FinishRouter": FinishRouter,
|
|
|
|
}
|
|
|
|
|
|
|
|
routerHooksMapping = map[int]string{
|
|
|
|
BeforeStatic: "beego.BeforeStatic",
|
|
|
|
BeforeRouter: "beego.BeforeRouter",
|
|
|
|
BeforeExec: "beego.BeforeExec",
|
|
|
|
AfterExec: "beego.AfterExec",
|
|
|
|
FinishRouter: "beego.FinishRouter",
|
|
|
|
}
|
2014-06-10 09:11:02 +00:00
|
|
|
)
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2016-09-21 11:33:12 +00:00
|
|
|
const commentPrefix = "commentsRouter_"
|
2014-11-05 14:23:54 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
func init() {
|
2014-06-10 09:11:02 +00:00
|
|
|
pkgLastupdate = make(map[string]int64)
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
func parserPkg(pkgRealpath, pkgpath string) error {
|
2016-05-05 11:28:09 +00:00
|
|
|
rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_")
|
2016-05-06 05:26:48 +00:00
|
|
|
commentFilename, _ = filepath.Rel(AppPath, pkgRealpath)
|
2016-09-21 11:33:12 +00:00
|
|
|
commentFilename = commentPrefix + rep.Replace(commentFilename) + ".go"
|
2014-06-10 09:11:02 +00:00
|
|
|
if !compareFile(pkgRealpath) {
|
2016-03-24 09:39:29 +00:00
|
|
|
logs.Info(pkgRealpath + " no changed")
|
2014-06-10 09:11:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-11-06 08:25:47 +00:00
|
|
|
genInfoList = make(map[string][]ControllerComments)
|
2014-06-09 09:33:04 +00:00
|
|
|
fileSet := token.NewFileSet()
|
|
|
|
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
|
|
|
|
name := info.Name()
|
|
|
|
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
|
|
|
}, parser.ParseComments)
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pkg := range astPkgs {
|
|
|
|
for _, fl := range pkg.Files {
|
|
|
|
for _, d := range fl.Decls {
|
|
|
|
switch specDecl := d.(type) {
|
|
|
|
case *ast.FuncDecl:
|
2014-08-05 00:56:04 +00:00
|
|
|
if specDecl.Recv != nil {
|
2015-09-10 09:35:57 +00:00
|
|
|
exp, ok := specDecl.Recv.List[0].Type.(*ast.StarExpr) // Check that the type is correct first beforing throwing to parser
|
|
|
|
if ok {
|
2017-04-25 13:00:49 +00:00
|
|
|
parserComments(specDecl, fmt.Sprint(exp.X), pkgpath)
|
2015-09-10 09:35:57 +00:00
|
|
|
}
|
2014-08-05 00:56:04 +00:00
|
|
|
}
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 15:57:22 +00:00
|
|
|
genRouterCode(pkgRealpath)
|
2014-06-10 09:11:02 +00:00
|
|
|
savetoFile(pkgRealpath)
|
2014-06-09 09:33:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
type parsedComment struct {
|
|
|
|
routerPath string
|
|
|
|
methods []string
|
|
|
|
params map[string]parsedParam
|
2018-09-07 04:14:13 +00:00
|
|
|
filters []parsedFilter
|
|
|
|
imports []parsedImport
|
|
|
|
}
|
|
|
|
|
|
|
|
type parsedImport struct {
|
|
|
|
importPath string
|
|
|
|
importAlias string
|
|
|
|
}
|
|
|
|
|
|
|
|
type parsedFilter struct {
|
|
|
|
pattern string
|
|
|
|
pos int
|
|
|
|
filter string
|
|
|
|
params []bool
|
2017-04-25 13:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type parsedParam struct {
|
|
|
|
name string
|
|
|
|
datatype string
|
|
|
|
location string
|
|
|
|
defValue string
|
|
|
|
required bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
|
|
|
if f.Doc != nil {
|
2018-06-28 07:54:17 +00:00
|
|
|
parsedComments, err := parseComment(f.Doc.List)
|
2017-04-25 13:00:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-28 07:54:17 +00:00
|
|
|
for _, parsedComment := range parsedComments {
|
|
|
|
if parsedComment.routerPath != "" {
|
|
|
|
key := pkgpath + ":" + controllerName
|
|
|
|
cc := ControllerComments{}
|
|
|
|
cc.Method = f.Name.String()
|
|
|
|
cc.Router = parsedComment.routerPath
|
|
|
|
cc.AllowHTTPMethods = parsedComment.methods
|
|
|
|
cc.MethodParams = buildMethodParams(f.Type.Params.List, parsedComment)
|
2018-09-07 04:14:13 +00:00
|
|
|
cc.FilterComments = buildFilters(parsedComment.filters)
|
|
|
|
cc.ImportComments = buildImports(parsedComment.imports)
|
2018-06-28 07:54:17 +00:00
|
|
|
genInfoList[key] = append(genInfoList[key], cc)
|
|
|
|
}
|
2017-04-25 22:00:25 +00:00
|
|
|
}
|
2017-04-25 13:00:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 04:14:13 +00:00
|
|
|
func buildImports(pis []parsedImport) []*ControllerImportComments {
|
|
|
|
var importComments []*ControllerImportComments
|
|
|
|
|
|
|
|
for _, pi := range pis {
|
|
|
|
importComments = append(importComments, &ControllerImportComments{
|
|
|
|
ImportPath: pi.importPath,
|
|
|
|
ImportAlias: pi.importAlias,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return importComments
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildFilters(pfs []parsedFilter) []*ControllerFilterComments {
|
|
|
|
var filterComments []*ControllerFilterComments
|
|
|
|
|
|
|
|
for _, pf := range pfs {
|
|
|
|
var (
|
|
|
|
returnOnOutput bool
|
|
|
|
resetParams bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(pf.params) >= 1 {
|
|
|
|
returnOnOutput = pf.params[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pf.params) >= 2 {
|
|
|
|
resetParams = pf.params[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
filterComments = append(filterComments, &ControllerFilterComments{
|
|
|
|
Filter: pf.filter,
|
|
|
|
Pattern: pf.pattern,
|
|
|
|
Pos: pf.pos,
|
|
|
|
ReturnOnOutput: returnOnOutput,
|
|
|
|
ResetParams: resetParams,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return filterComments
|
|
|
|
}
|
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
func buildMethodParams(funcParams []*ast.Field, pc *parsedComment) []*param.MethodParam {
|
|
|
|
result := make([]*param.MethodParam, 0, len(funcParams))
|
|
|
|
for _, fparam := range funcParams {
|
2017-06-09 07:15:36 +00:00
|
|
|
for _, pName := range fparam.Names {
|
|
|
|
methodParam := buildMethodParam(fparam, pName.Name, pc)
|
|
|
|
result = append(result, methodParam)
|
|
|
|
}
|
2017-04-25 13:00:49 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-06-09 07:15:36 +00:00
|
|
|
func buildMethodParam(fparam *ast.Field, name string, pc *parsedComment) *param.MethodParam {
|
2017-04-25 13:00:49 +00:00
|
|
|
options := []param.MethodParamOption{}
|
|
|
|
if cparam, ok := pc.params[name]; ok {
|
|
|
|
//Build param from comment info
|
|
|
|
name = cparam.name
|
|
|
|
if cparam.required {
|
|
|
|
options = append(options, param.IsRequired)
|
|
|
|
}
|
|
|
|
switch cparam.location {
|
|
|
|
case "body":
|
|
|
|
options = append(options, param.InBody)
|
|
|
|
case "header":
|
|
|
|
options = append(options, param.InHeader)
|
|
|
|
case "path":
|
|
|
|
options = append(options, param.InPath)
|
|
|
|
}
|
|
|
|
if cparam.defValue != "" {
|
|
|
|
options = append(options, param.Default(cparam.defValue))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if paramInPath(name, pc.routerPath) {
|
|
|
|
options = append(options, param.InPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return param.New(name, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func paramInPath(name, route string) bool {
|
|
|
|
return strings.HasSuffix(route, ":"+name) ||
|
|
|
|
strings.Contains(route, ":"+name+"/")
|
|
|
|
}
|
|
|
|
|
|
|
|
var routeRegex = regexp.MustCompile(`@router\s+(\S+)(?:\s+\[(\S+)\])?`)
|
|
|
|
|
2018-06-28 07:54:17 +00:00
|
|
|
func parseComment(lines []*ast.Comment) (pcs []*parsedComment, err error) {
|
|
|
|
pcs = []*parsedComment{}
|
|
|
|
params := map[string]parsedParam{}
|
2018-09-07 04:14:13 +00:00
|
|
|
filters := []parsedFilter{}
|
|
|
|
imports := []parsedImport{}
|
2018-06-28 07:54:17 +00:00
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
for _, c := range lines {
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
2018-06-28 07:54:17 +00:00
|
|
|
if strings.HasPrefix(t, "@Param") {
|
2017-04-25 13:00:49 +00:00
|
|
|
pv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Param")))
|
|
|
|
if len(pv) < 4 {
|
|
|
|
logs.Error("Invalid @Param format. Needs at least 4 parameters")
|
|
|
|
}
|
|
|
|
p := parsedParam{}
|
2017-04-25 20:42:35 +00:00
|
|
|
names := strings.SplitN(pv[0], "=>", 2)
|
|
|
|
p.name = names[0]
|
|
|
|
funcParamName := p.name
|
2017-04-25 13:00:49 +00:00
|
|
|
if len(names) > 1 {
|
2017-04-25 20:42:35 +00:00
|
|
|
funcParamName = names[1]
|
2017-04-25 13:00:49 +00:00
|
|
|
}
|
|
|
|
p.location = pv[1]
|
|
|
|
p.datatype = pv[2]
|
|
|
|
switch len(pv) {
|
|
|
|
case 5:
|
|
|
|
p.required, _ = strconv.ParseBool(pv[3])
|
|
|
|
case 6:
|
|
|
|
p.defValue = pv[3]
|
|
|
|
p.required, _ = strconv.ParseBool(pv[4])
|
|
|
|
}
|
2018-06-28 07:54:17 +00:00
|
|
|
params[funcParamName] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 04:14:13 +00:00
|
|
|
for _, c := range lines {
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
|
|
|
if strings.HasPrefix(t, "@Import") {
|
|
|
|
iv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Import")))
|
|
|
|
if len(iv) == 0 || len(iv) > 2 {
|
|
|
|
logs.Error("Invalid @Import format. Only accepts 1 or 2 parameters")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := parsedImport{}
|
|
|
|
p.importPath = iv[0]
|
|
|
|
|
|
|
|
if len(iv) == 2 {
|
|
|
|
p.importAlias = iv[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
imports = append(imports, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filterLoop:
|
|
|
|
for _, c := range lines {
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
|
|
|
if strings.HasPrefix(t, "@Filter") {
|
|
|
|
fv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Filter")))
|
|
|
|
if len(fv) < 3 {
|
|
|
|
logs.Error("Invalid @Filter format. Needs at least 3 parameters")
|
|
|
|
continue filterLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
p := parsedFilter{}
|
|
|
|
p.pattern = fv[0]
|
|
|
|
posName := fv[1]
|
|
|
|
if pos, exists := routerHooks[posName]; exists {
|
|
|
|
p.pos = pos
|
|
|
|
} else {
|
|
|
|
logs.Error("Invalid @Filter pos: ", posName)
|
|
|
|
continue filterLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
p.filter = fv[2]
|
|
|
|
fvParams := fv[3:]
|
|
|
|
for _, fvParam := range fvParams {
|
|
|
|
switch fvParam {
|
|
|
|
case "true":
|
|
|
|
p.params = append(p.params, true)
|
|
|
|
case "false":
|
|
|
|
p.params = append(p.params, false)
|
|
|
|
default:
|
|
|
|
logs.Error("Invalid @Filter param: ", fvParam)
|
|
|
|
continue filterLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters = append(filters, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 07:54:17 +00:00
|
|
|
for _, c := range lines {
|
|
|
|
var pc = &parsedComment{}
|
|
|
|
pc.params = params
|
2018-09-07 04:14:13 +00:00
|
|
|
pc.filters = filters
|
|
|
|
pc.imports = imports
|
2018-06-28 07:54:17 +00:00
|
|
|
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
|
|
|
if strings.HasPrefix(t, "@router") {
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
|
|
|
matches := routeRegex.FindStringSubmatch(t)
|
|
|
|
if len(matches) == 3 {
|
|
|
|
pc.routerPath = matches[1]
|
|
|
|
methods := matches[2]
|
|
|
|
if methods == "" {
|
|
|
|
pc.methods = []string{"get"}
|
|
|
|
//pc.hasGet = true
|
|
|
|
} else {
|
|
|
|
pc.methods = strings.Split(methods, ",")
|
|
|
|
//pc.hasGet = strings.Contains(methods, "get")
|
|
|
|
}
|
|
|
|
pcs = append(pcs, pc)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Router information is missing")
|
2017-04-25 13:00:49 +00:00
|
|
|
}
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 13:00:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// direct copy from bee\g_docs.go
|
2017-10-11 06:35:31 +00:00
|
|
|
// analysis params return []string
|
2017-04-25 13:00:49 +00:00
|
|
|
// @Param query form string true "The email for login"
|
|
|
|
// [query form string true "The email for login"]
|
|
|
|
func getparams(str string) []string {
|
|
|
|
var s []rune
|
|
|
|
var j int
|
|
|
|
var start bool
|
|
|
|
var r []string
|
|
|
|
var quoted int8
|
2017-05-17 17:50:41 +00:00
|
|
|
for _, c := range str {
|
2017-04-25 13:00:49 +00:00
|
|
|
if unicode.IsSpace(c) && quoted == 0 {
|
|
|
|
if !start {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
start = false
|
|
|
|
j++
|
|
|
|
r = append(r, string(s))
|
|
|
|
s = make([]rune, 0)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start = true
|
|
|
|
if c == '"' {
|
|
|
|
quoted ^= 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s = append(s, c)
|
|
|
|
}
|
|
|
|
if len(s) > 0 {
|
|
|
|
r = append(r, string(s))
|
|
|
|
}
|
|
|
|
return r
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 15:57:22 +00:00
|
|
|
func genRouterCode(pkgRealpath string) {
|
|
|
|
os.Mkdir(getRouterDir(pkgRealpath), 0755)
|
2016-03-24 09:39:29 +00:00
|
|
|
logs.Info("generate router from comments")
|
2015-09-08 13:45:45 +00:00
|
|
|
var (
|
2018-09-07 04:14:13 +00:00
|
|
|
globalinfo string
|
|
|
|
globalimport string
|
|
|
|
sortKey []string
|
2015-09-08 13:45:45 +00:00
|
|
|
)
|
|
|
|
for k := range genInfoList {
|
2015-06-08 09:25:46 +00:00
|
|
|
sortKey = append(sortKey, k)
|
|
|
|
}
|
|
|
|
sort.Strings(sortKey)
|
|
|
|
for _, k := range sortKey {
|
|
|
|
cList := genInfoList[k]
|
2017-07-15 09:26:20 +00:00
|
|
|
sort.Sort(ControllerCommentsSlice(cList))
|
2014-06-09 09:33:04 +00:00
|
|
|
for _, c := range cList {
|
|
|
|
allmethod := "nil"
|
2014-06-09 09:46:13 +00:00
|
|
|
if len(c.AllowHTTPMethods) > 0 {
|
2014-06-09 09:33:04 +00:00
|
|
|
allmethod = "[]string{"
|
2014-06-09 09:46:13 +00:00
|
|
|
for _, m := range c.AllowHTTPMethods {
|
2014-06-09 09:33:04 +00:00
|
|
|
allmethod += `"` + m + `",`
|
|
|
|
}
|
|
|
|
allmethod = strings.TrimRight(allmethod, ",") + "}"
|
|
|
|
}
|
2018-09-07 04:14:13 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
params := "nil"
|
2014-06-09 09:46:13 +00:00
|
|
|
if len(c.Params) > 0 {
|
2014-06-09 09:33:04 +00:00
|
|
|
params = "[]map[string]string{"
|
2014-06-09 09:46:13 +00:00
|
|
|
for _, p := range c.Params {
|
2014-06-09 09:33:04 +00:00
|
|
|
for k, v := range p {
|
|
|
|
params = params + `map[string]string{` + k + `:"` + v + `"},`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
params = strings.TrimRight(params, ",") + "}"
|
|
|
|
}
|
2018-09-07 04:14:13 +00:00
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
methodParams := "param.Make("
|
|
|
|
if len(c.MethodParams) > 0 {
|
|
|
|
lines := make([]string, 0, len(c.MethodParams))
|
|
|
|
for _, m := range c.MethodParams {
|
|
|
|
lines = append(lines, fmt.Sprint(m))
|
|
|
|
}
|
|
|
|
methodParams += "\n " +
|
|
|
|
strings.Join(lines, ",\n ") +
|
|
|
|
",\n "
|
|
|
|
}
|
|
|
|
methodParams += ")"
|
2018-09-07 04:14:13 +00:00
|
|
|
|
|
|
|
imports := ""
|
|
|
|
if len(c.ImportComments) > 0 {
|
|
|
|
for _, i := range c.ImportComments {
|
2019-03-24 06:41:28 +00:00
|
|
|
var s string
|
2018-09-07 04:14:13 +00:00
|
|
|
if i.ImportAlias != "" {
|
2019-03-24 06:41:28 +00:00
|
|
|
s = fmt.Sprintf(`
|
2018-09-07 04:14:13 +00:00
|
|
|
%s "%s"`, i.ImportAlias, i.ImportPath)
|
|
|
|
} else {
|
2019-03-24 06:41:28 +00:00
|
|
|
s = fmt.Sprintf(`
|
2018-09-07 04:14:13 +00:00
|
|
|
"%s"`, i.ImportPath)
|
|
|
|
}
|
2019-03-24 06:41:28 +00:00
|
|
|
if !strings.Contains(globalimport, s) {
|
|
|
|
imports += s
|
|
|
|
}
|
2018-09-07 04:14:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters := ""
|
|
|
|
if len(c.FilterComments) > 0 {
|
|
|
|
for _, f := range c.FilterComments {
|
|
|
|
filters += fmt.Sprintf(` &beego.ControllerFilter{
|
|
|
|
Pattern: "%s",
|
|
|
|
Pos: %s,
|
|
|
|
Filter: %s,
|
|
|
|
ReturnOnOutput: %v,
|
|
|
|
ResetParams: %v,
|
|
|
|
},`, f.Pattern, routerHooksMapping[f.Pos], f.Filter, f.ReturnOnOutput, f.ResetParams)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if filters == "" {
|
|
|
|
filters = "nil"
|
|
|
|
} else {
|
|
|
|
filters = fmt.Sprintf(`[]*beego.ControllerFilter{
|
|
|
|
%s
|
|
|
|
}`, filters)
|
|
|
|
}
|
|
|
|
|
2019-03-24 06:41:28 +00:00
|
|
|
globalimport += imports
|
2018-09-07 04:14:13 +00:00
|
|
|
|
2014-06-10 09:11:02 +00:00
|
|
|
globalinfo = globalinfo + `
|
2018-09-07 04:14:13 +00:00
|
|
|
beego.GlobalControllerRouter["` + k + `"] = append(beego.GlobalControllerRouter["` + k + `"],
|
|
|
|
beego.ControllerComments{
|
|
|
|
Method: "` + strings.TrimSpace(c.Method) + `",
|
|
|
|
` + "Router: `" + c.Router + "`" + `,
|
|
|
|
AllowHTTPMethods: ` + allmethod + `,
|
|
|
|
MethodParams: ` + methodParams + `,
|
|
|
|
Filters: ` + filters + `,
|
|
|
|
Params: ` + params + `})
|
2014-06-10 09:11:02 +00:00
|
|
|
`
|
2014-06-10 03:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 04:14:13 +00:00
|
|
|
|
2014-06-10 03:02:41 +00:00
|
|
|
if globalinfo != "" {
|
2016-05-06 05:26:48 +00:00
|
|
|
f, err := os.Create(filepath.Join(getRouterDir(pkgRealpath), commentFilename))
|
2014-06-10 03:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
2014-06-10 03:02:41 +00:00
|
|
|
defer f.Close()
|
2018-09-07 04:14:13 +00:00
|
|
|
|
2019-04-22 14:18:37 +00:00
|
|
|
routersDir := AppConfig.DefaultString("routersdir", "routers")
|
2018-09-07 04:14:13 +00:00
|
|
|
content := strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1)
|
2019-04-22 14:18:37 +00:00
|
|
|
content = strings.Replace(content, "{{.routersDir}}", routersDir, -1)
|
2018-09-07 04:14:13 +00:00
|
|
|
content = strings.Replace(content, "{{.globalimport}}", globalimport, -1)
|
|
|
|
f.WriteString(content)
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
}
|
2014-06-10 09:11:02 +00:00
|
|
|
|
|
|
|
func compareFile(pkgRealpath string) bool {
|
2016-05-06 05:26:48 +00:00
|
|
|
if !utils.FileExists(filepath.Join(getRouterDir(pkgRealpath), commentFilename)) {
|
2014-08-02 02:11:45 +00:00
|
|
|
return true
|
|
|
|
}
|
2015-12-09 15:35:04 +00:00
|
|
|
if utils.FileExists(lastupdateFilename) {
|
|
|
|
content, err := ioutil.ReadFile(lastupdateFilename)
|
2014-06-10 09:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
json.Unmarshal(content, &pkgLastupdate)
|
2014-06-30 07:57:36 +00:00
|
|
|
lastupdate, err := getpathTime(pkgRealpath)
|
2014-06-10 09:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if v, ok := pkgLastupdate[pkgRealpath]; ok {
|
2014-06-30 07:57:36 +00:00
|
|
|
if lastupdate <= v {
|
2014-06-10 09:11:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func savetoFile(pkgRealpath string) {
|
2014-06-30 07:57:36 +00:00
|
|
|
lastupdate, err := getpathTime(pkgRealpath)
|
2014-06-10 09:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-06-30 07:57:36 +00:00
|
|
|
pkgLastupdate[pkgRealpath] = lastupdate
|
2014-06-10 09:11:02 +00:00
|
|
|
d, err := json.Marshal(pkgLastupdate)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-12-09 15:35:04 +00:00
|
|
|
ioutil.WriteFile(lastupdateFilename, d, os.ModePerm)
|
2014-06-10 09:11:02 +00:00
|
|
|
}
|
2014-06-30 07:57:36 +00:00
|
|
|
|
|
|
|
func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
|
|
|
|
fl, err := ioutil.ReadDir(pkgRealpath)
|
|
|
|
if err != nil {
|
|
|
|
return lastupdate, err
|
|
|
|
}
|
|
|
|
for _, f := range fl {
|
|
|
|
if lastupdate < f.ModTime().UnixNano() {
|
|
|
|
lastupdate = f.ModTime().UnixNano()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastupdate, nil
|
|
|
|
}
|
2016-04-27 15:57:22 +00:00
|
|
|
|
|
|
|
func getRouterDir(pkgRealpath string) string {
|
|
|
|
dir := filepath.Dir(pkgRealpath)
|
|
|
|
for {
|
2019-04-22 14:18:37 +00:00
|
|
|
routersDir := AppConfig.DefaultString("routersdir", "routers")
|
|
|
|
d := filepath.Join(dir, routersDir)
|
2016-05-06 05:26:48 +00:00
|
|
|
if utils.FileExists(d) {
|
2016-04-27 15:57:22 +00:00
|
|
|
return d
|
|
|
|
}
|
2016-05-06 05:26:48 +00:00
|
|
|
|
|
|
|
if r, _ := filepath.Rel(dir, AppPath); r == "." {
|
2016-04-27 15:57:22 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
// Parent dir.
|
|
|
|
dir = filepath.Dir(dir)
|
|
|
|
}
|
|
|
|
}
|