-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCreateRouter.go
More file actions
61 lines (49 loc) · 1.01 KB
/
CreateRouter.go
File metadata and controls
61 lines (49 loc) · 1.01 KB
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
package create
import (
"os"
"path"
"path/filepath"
"text/template"
"github.com/muxi-Infra/muxi-micro/tool/gin/parse"
)
type Name struct {
Max string
Min string
}
// 存在不覆盖
func CreateRouter(addr string, apis []*parse.Api) error {
dir := path.Join(addr, "router.go")
if _, err := os.Stat(dir); err == nil {
return nil
}
tmplPath := filepath.Join("gin", "template", "router.tpl")
t, err := template.New("router").ParseFiles(tmplPath)
if err != nil {
return err
}
outputPath := path.Join(addr, "router.go")
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
data := struct {
Name []*Name
}{
Name: MaxFirstLetter(apis),
}
if err := t.ExecuteTemplate(file, "router", data); err != nil {
return err
}
return nil
}
func MaxFirstLetter(apis []*parse.Api) []*Name {
var names []*Name
for _, api := range apis {
var name Name
name.Max = maxFirstLetter(api.ServiceName)
name.Min = api.ServiceName
names = append(names, &name)
}
return names
}