-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
73 lines (58 loc) · 1.38 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gim
import (
"os"
"path/filepath"
"strings"
)
type routesTree map[string]routesTree
type routeDescription struct {
path string
parameter string
}
func (g *Gim) extractRouteTree() (routesTree, error) {
routesRoot := routesTree{}
deep := 0
if err := filepath.Walk(g.Workspace.pagesFolder(), func(p string, info os.FileInfo, err error) error {
relativePath := g.cutPath(g.Workspace.pagesFolder(), p)
parts := strings.Split(relativePath, "/")
if len(parts) == deep+1 {
deep++
}
if !info.IsDir() {
var current *routesTree
current = &routesRoot
for _, part := range parts {
if (*current)[part] == nil {
(*current)[part] = routesTree{}
}
var temp = (*current)[part]
current = &temp
}
}
return nil
}); err != nil {
return nil, err
}
return routesRoot, nil
}
func (g *Gim) describeRoute(filename string) routeDescription {
chunks := strings.Split(filename, ".")
if len(chunks) == 0 {
return routeDescription{}
}
name := chunks[0]
// TODO: You can validate if the extension is valid
description := routeDescription{
path: name,
parameter: "",
}
if name == "index" {
description.path = ""
}
if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") {
parameterName := strings.Trim(name, "[]")
description.path = ":" + parameterName
description.parameter = parameterName
}
return description
}