Skip to content

Commit

Permalink
start to add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bughou committed Jul 10, 2020
1 parent a0dd81a commit 54e071b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 48 deletions.
File renamed without changes.
19 changes: 19 additions & 0 deletions converters/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package docs

import (
"fmt"
"path/filepath"
)

func Group(parentGroupDir, path string, descs []string) string {
groupDir := filepath.Join(parentGroupDir, filepath.FromSlash(path))

return groupDir
}

func Route(groupDir, path, fullPath string, handler interface{}) {
fmt.Println("vim-go")
}

func addLink(parentGroupDir, title, href string) {
}
112 changes: 64 additions & 48 deletions routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,71 @@ package goa
import (
"bytes"
"log"
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/lovego/goa/converters/docs"
"github.com/lovego/regex_tree"
)

type RouterGroup struct {
basePath string
handlers HandlerFuncs
routes map[string]*regex_tree.Node
docDir string
}

func (g *RouterGroup) Group(path string, handlers ...HandlerFunc) *RouterGroup {
return &RouterGroup{
func (g *RouterGroup) DocDir(dir string) {
g.docDir = filepath.Clean(strings.TrimSpace(dir))
}

func (g *RouterGroup) Group(path string, descs ...string) *RouterGroup {
newGroup := &RouterGroup{
basePath: g.concatPath(quotePath(path)),
handlers: g.concatHandlers(handlers...),
handlers: g.concatHandlers(),
routes: g.routes,
}
if g.docDir != "" {
newGroup.docDir = docs.Group(g.docDir, path, descs)
}
return newGroup
}

func (g *RouterGroup) Use(handlers ...HandlerFunc) {
func (g *RouterGroup) Use(handlers ...HandlerFunc) *RouterGroup {
g.handlers = append(g.handlers, handlers...)
return g
}

func (g *RouterGroup) Add(method, path string, handler interface{}) *RouterGroup {
method = strings.ToUpper(method)
fullPath := g.concatPath(quotePath(path))
// remove trailing slash
if len(fullPath) > 1 && fullPath[len(fullPath)-1] == '/' {
fullPath = fullPath[:len(fullPath)-1]
}
if handler == nil {
return g
}
handlerFunc := convertHandler(handler, fullPath)
handlers := g.concatHandlers(handlerFunc)

rootNode := g.routes[method]
if rootNode == nil {
rootNode, err := regex_tree.New(fullPath, handlers)
if err != nil {
log.Panic(err)
}
g.routes[method] = rootNode
} else if err := rootNode.Add(fullPath, handlers); err != nil {
log.Panic(err)
}

if g.docDir != "" {
docs.Route(g.docDir, path, fullPath, handler)
}
return g
}

func (g *RouterGroup) Get(path string, handler interface{}) *RouterGroup {
Expand Down Expand Up @@ -53,50 +95,6 @@ func (g *RouterGroup) Delete(path string, handler interface{}) *RouterGroup {
return g.Add("DELETE", path, handler)
}

func (g *RouterGroup) Add(method, path string, handler interface{}) *RouterGroup {
method = strings.ToUpper(method)
path = g.concatPath(quotePath(path))
// remove trailing slash
if len(path) > 1 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
if handler == nil {
return g
}
handlerFunc := convertHandler(handler, path)
handlers := g.concatHandlers(handlerFunc)

rootNode := g.routes[method]
if rootNode == nil {
rootNode, err := regex_tree.New(path, handlers)
if err != nil {
log.Panic(err)
}
g.routes[method] = rootNode
} else if err := rootNode.Add(path, handlers); err != nil {
log.Panic(err)
}
return g
}

func (g RouterGroup) concatPath(path string) string {
path = g.basePath + path
if len(path) == 0 {
log.Panic(`router path must not be empty.`)
}
if path[0] != '/' {
log.Panic(`router path must begin with "/".`)
}
return path
}

func (g RouterGroup) concatHandlers(handlers ...HandlerFunc) HandlerFuncs {
result := make(HandlerFuncs, len(g.handlers)+len(handlers))
copy(result, g.handlers)
copy(result[len(g.handlers):], handlers)
return result
}

func (g *RouterGroup) Lookup(method, path string) (HandlerFuncs, []string) {
if method == `HEAD` {
method = `GET`
Expand Down Expand Up @@ -146,6 +144,24 @@ func (g *RouterGroup) RoutesString() string {
return buf.String()
}

func (g RouterGroup) concatPath(path string) string {
path = g.basePath + path
if len(path) == 0 {
log.Panic(`router path must not be empty.`)
}
if path[0] != '/' {
log.Panic(`router path must begin with "/".`)
}
return path
}

func (g RouterGroup) concatHandlers(handlers ...HandlerFunc) HandlerFuncs {
result := make(HandlerFuncs, len(g.handlers)+len(handlers))
copy(result, g.handlers)
copy(result[len(g.handlers):], handlers)
return result
}

func quotePath(path string) string {
// if path contains "(" or ")" it should be a regular expression
if strings.ContainsAny(path, "()") {
Expand Down

0 comments on commit 54e071b

Please sign in to comment.