mirror of
https://github.com/astaxie/beego.git
synced 2024-11-24 04:30:55 +00:00
Merge pull request #4107 from AllenX2018/fix-comment-router-issue
add comment router path configuration
This commit is contained in:
commit
19aae0b7e1
1
go.mod
1
go.mod
@ -32,6 +32,7 @@ require (
|
|||||||
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b // indirect
|
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b // indirect
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
|
||||||
|
golang.org/x/tools v0.0.0-20200117065230-39095c1d176c
|
||||||
google.golang.org/grpc v1.31.0 // indirect
|
google.golang.org/grpc v1.31.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.2.8
|
gopkg.in/yaml.v2 v2.2.8
|
||||||
)
|
)
|
||||||
|
@ -97,6 +97,7 @@ func initBeforeHTTPRun() {
|
|||||||
registerTemplate,
|
registerTemplate,
|
||||||
registerAdmin,
|
registerAdmin,
|
||||||
registerGzip,
|
registerGzip,
|
||||||
|
registerCommentRouter,
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, hk := range hooks {
|
for _, hk := range hooks {
|
||||||
|
@ -86,6 +86,7 @@ type WebConfig struct {
|
|||||||
TemplateLeft string
|
TemplateLeft string
|
||||||
TemplateRight string
|
TemplateRight string
|
||||||
ViewsPath string
|
ViewsPath string
|
||||||
|
CommentRouterPath string
|
||||||
EnableXSRF bool
|
EnableXSRF bool
|
||||||
XSRFKey string
|
XSRFKey string
|
||||||
XSRFExpire int
|
XSRFExpire int
|
||||||
@ -245,6 +246,7 @@ func newBConfig() *Config {
|
|||||||
TemplateLeft: "{{",
|
TemplateLeft: "{{",
|
||||||
TemplateRight: "}}",
|
TemplateRight: "}}",
|
||||||
ViewsPath: "views",
|
ViewsPath: "views",
|
||||||
|
CommentRouterPath: "controllers",
|
||||||
EnableXSRF: false,
|
EnableXSRF: false,
|
||||||
XSRFKey: "beegoxsrf",
|
XSRFKey: "beegoxsrf",
|
||||||
XSRFExpire: 0,
|
XSRFExpire: 0,
|
||||||
|
10
pkg/hooks.go
10
pkg/hooks.go
@ -102,3 +102,13 @@ func registerGzip() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registerCommentRouter() error {
|
||||||
|
if BConfig.RunMode == DEV {
|
||||||
|
if err := parserPkg(filepath.Join(WorkPath, BConfig.WebConfig.CommentRouterPath)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -19,8 +19,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"golang.org/x/tools/go/packages"
|
||||||
"go/token"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -76,7 +75,7 @@ func init() {
|
|||||||
pkgLastupdate = make(map[string]int64)
|
pkgLastupdate = make(map[string]int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parserPkg(pkgRealpath, pkgpath string) error {
|
func parserPkg(pkgRealpath string) error {
|
||||||
rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_")
|
rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_")
|
||||||
commentFilename, _ = filepath.Rel(AppPath, pkgRealpath)
|
commentFilename, _ = filepath.Rel(AppPath, pkgRealpath)
|
||||||
commentFilename = commentPrefix + rep.Replace(commentFilename) + ".go"
|
commentFilename = commentPrefix + rep.Replace(commentFilename) + ".go"
|
||||||
@ -85,24 +84,23 @@ func parserPkg(pkgRealpath, pkgpath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
genInfoList = make(map[string][]ControllerComments)
|
genInfoList = make(map[string][]ControllerComments)
|
||||||
fileSet := token.NewFileSet()
|
pkgs, err := packages.Load(&packages.Config{
|
||||||
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
|
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedSyntax,
|
||||||
name := info.Name()
|
Dir: pkgRealpath,
|
||||||
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
}, "./...")
|
||||||
}, parser.ParseComments)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, pkg := range astPkgs {
|
for _, pkg := range pkgs {
|
||||||
for _, fl := range pkg.Files {
|
for _, fl := range pkg.Syntax {
|
||||||
for _, d := range fl.Decls {
|
for _, d := range fl.Decls {
|
||||||
switch specDecl := d.(type) {
|
switch specDecl := d.(type) {
|
||||||
case *ast.FuncDecl:
|
case *ast.FuncDecl:
|
||||||
if specDecl.Recv != nil {
|
if specDecl.Recv != nil {
|
||||||
exp, ok := specDecl.Recv.List[0].Type.(*ast.StarExpr) // Check that the type is correct first beforing throwing to parser
|
exp, ok := specDecl.Recv.List[0].Type.(*ast.StarExpr) // Check that the type is correct first beforing throwing to parser
|
||||||
if ok {
|
if ok {
|
||||||
parserComments(specDecl, fmt.Sprint(exp.X), pkgpath)
|
parserComments(specDecl, fmt.Sprint(exp.X), pkg.PkgPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -566,8 +564,17 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
|
|||||||
return lastupdate, err
|
return lastupdate, err
|
||||||
}
|
}
|
||||||
for _, f := range fl {
|
for _, f := range fl {
|
||||||
if lastupdate < f.ModTime().UnixNano() {
|
var t int64
|
||||||
lastupdate = f.ModTime().UnixNano()
|
if f.IsDir() {
|
||||||
|
t, err = getpathTime(filepath.Join(pkgRealpath, f.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return lastupdate, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t = f.ModTime().UnixNano()
|
||||||
|
}
|
||||||
|
if lastupdate < t {
|
||||||
|
lastupdate = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lastupdate, nil
|
return lastupdate, nil
|
||||||
|
@ -18,9 +18,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -257,45 +255,6 @@ func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerIn
|
|||||||
// Include only when the Runmode is dev will generate router file in the router/auto.go from the controller
|
// Include only when the Runmode is dev will generate router file in the router/auto.go from the controller
|
||||||
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
|
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
|
||||||
func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
||||||
if BConfig.RunMode == DEV {
|
|
||||||
skip := make(map[string]bool, 10)
|
|
||||||
wgopath := utils.GetGOPATHs()
|
|
||||||
go111module := os.Getenv(`GO111MODULE`)
|
|
||||||
for _, c := range cList {
|
|
||||||
reflectVal := reflect.ValueOf(c)
|
|
||||||
t := reflect.Indirect(reflectVal).Type()
|
|
||||||
// for go modules
|
|
||||||
if go111module == `on` {
|
|
||||||
pkgpath := filepath.Join(WorkPath, "..", t.PkgPath())
|
|
||||||
if utils.FileExists(pkgpath) {
|
|
||||||
if pkgpath != "" {
|
|
||||||
if _, ok := skip[pkgpath]; !ok {
|
|
||||||
skip[pkgpath] = true
|
|
||||||
parserPkg(pkgpath, t.PkgPath())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if len(wgopath) == 0 {
|
|
||||||
panic("you are in dev mode. So please set gopath")
|
|
||||||
}
|
|
||||||
pkgpath := ""
|
|
||||||
for _, wg := range wgopath {
|
|
||||||
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", t.PkgPath()))
|
|
||||||
if utils.FileExists(wg) {
|
|
||||||
pkgpath = wg
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if pkgpath != "" {
|
|
||||||
if _, ok := skip[pkgpath]; !ok {
|
|
||||||
skip[pkgpath] = true
|
|
||||||
parserPkg(pkgpath, t.PkgPath())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, c := range cList {
|
for _, c := range cList {
|
||||||
reflectVal := reflect.ValueOf(c)
|
reflectVal := reflect.ValueOf(c)
|
||||||
t := reflect.Indirect(reflectVal).Type()
|
t := reflect.Indirect(reflectVal).Type()
|
||||||
|
Loading…
Reference in New Issue
Block a user