/
simple_web_echo.go
executable file
·134 lines (103 loc) · 2.45 KB
/
simple_web_echo.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package manager
import (
"sync"
"github.com/joaosoft/logger"
"github.com/labstack/echo"
)
// SimpleWebEcho ...
type SimpleWebEcho struct {
server *echo.Echo
host string
logger logger.ILogger
started bool
}
// NewSimpleWebEcho...
func (manager *Manager) NewSimpleWebEcho(host string) IWeb {
e := echo.New()
e.HideBanner = true
return &SimpleWebEcho{
server: e,
host: host,
logger: manager.logger,
}
}
// AddRoutes ...
func (w *SimpleWebEcho) AddRoutes(routes ...*Route) error {
for _, route := range routes {
err := w.AddRoute(route.Method, route.Path, route.Handler, route.Middlewares...)
if err != nil {
return err
}
}
return nil
}
// AddRoute ...
func (w *SimpleWebEcho) AddRoute(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) error {
w.server.Add(method, path, handler.(func(echo.Context) error))
for _, item := range middleware {
w.server.Group(path, item.(echo.MiddlewareFunc))
}
return nil
}
// AddNamespace ...
func (w *SimpleWebEcho) AddNamespace(path string, middleware []MiddlewareFunc, routes ...*Route) error {
middlewares := make([]echo.MiddlewareFunc, 0)
for _, m := range middleware {
middlewares = append(middlewares, m.(echo.MiddlewareFunc))
}
echoGroup := w.server.Group(path, middlewares...)
for _, route := range routes {
echoGroup.Add(route.Method, route.Path, route.Handler.(func(echo.Context) error))
for _, item := range route.Middlewares {
w.server.Group(path, item.(echo.MiddlewareFunc))
}
}
return nil
}
func (w *SimpleWebEcho) AddFilter(pattern string, position string, middleware MiddlewareFunc, method string, methods ...string) {
// TODO: implement
}
// Start ...
func (w *SimpleWebEcho) Start(waitGroup ...*sync.WaitGroup) error {
var wg *sync.WaitGroup
if len(waitGroup) == 0 {
wg = &sync.WaitGroup{}
wg.Add(1)
} else {
wg = waitGroup[0]
}
defer wg.Done()
if w.started {
return nil
}
go w.server.Start(w.host)
w.started = true
return nil
}
// Stop ...
func (w *SimpleWebEcho) Stop(waitGroup ...*sync.WaitGroup) error {
var wg *sync.WaitGroup
if len(waitGroup) == 0 {
wg = &sync.WaitGroup{}
wg.Add(1)
} else {
wg = waitGroup[0]
}
defer wg.Done()
if !w.started {
return nil
}
if err := w.server.Close(); err != nil {
return err
}
w.started = false
return nil
}
// Started ...
func (w *SimpleWebEcho) Started() bool {
return w.started
}
// GetClient ...
func (w *SimpleWebEcho) GetClient() interface{} {
return w.server
}