-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_conf.go
72 lines (61 loc) · 1.31 KB
/
router_conf.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
package main
import (
"encoding/json"
"fmt"
"os"
)
type routersConf struct {
Port uint32 `json:"port"`
Routers []router `json:"routers"`
}
type router struct {
Router *string `json:"router,omitempty"`
Disable bool `json:"disable"`
To *string `json:"to,omitempty"`
ForwardPort *uint32 `json:"forward,omitempty"`
DistFile *string `json:"distFile,omitempty"`
}
func getRouters(fp string) *routersConf {
c := &routersConf{
Port: 7788,
Routers: []router{},
}
b, err := os.ReadFile(fp)
if err != nil {
return c
}
err = json.Unmarshal(b, c)
if err != nil {
return c
}
// for i := 0; i < len(c.Routers); i++ {
// if c.Routers[i].Disable {
// c.Routers = append(c.Routers[:i], c.Routers[i+1:]...)
// }
// }
return c
}
func confToRouters(routers []router) *BaseRouter {
root := NewBaseRouter()
for _, v := range routers {
if v.Disable {
continue
}
if v.Router != nil {
if v.To != nil {
root.AddRouter(*v.Router, HttpHandler(*v.Router, *v.To))
} else {
if v.DistFile != nil {
root.AddRouter(*v.Router, FileHandler(*v.Router, *v.DistFile))
}
}
continue
}
if v.ForwardPort != nil {
root.AddRouter(*v.Router,
HttpHandler(fmt.Sprintf("/%d", *v.ForwardPort),
fmt.Sprintf("http://127.0.0.1:%d", *v.ForwardPort)))
}
}
return root
}