-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
271 lines (232 loc) · 7.29 KB
/
wrapper.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package router
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"runtime/debug"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/rs/zerolog/log"
context2 "github.com/kodekoding/phastos/v2/go/context"
"github.com/kodekoding/phastos/v2/go/helper"
"github.com/kodekoding/phastos/v2/go/response"
)
type WrapperFunc func(http.ResponseWriter, *http.Request) *response.JSON
type RouteInterface interface {
chi.Routes
GetHandler() *chi.Mux
InitRoute(corsConfig ...cors.Options)
Handle(pattern string, handler http.Handler)
Group(prefix string, fn func(r RouteInterface)) RouteInterface
Get(pattern string, handler WrapperFunc)
Post(pattern string, handler WrapperFunc)
Patch(pattern string, handler WrapperFunc)
Delete(pattern string, handler WrapperFunc)
Put(pattern string, handler WrapperFunc)
Use(middlewares ...func(http.Handler) http.Handler)
With(middlewares ...func(http.Handler) http.Handler) RouteInterface
Mount(pattern string, handler http.Handler)
StaticFS(pattern, imgDir string)
}
type ChiRouter struct {
handle *chi.Mux
timeout int
}
func NewChiRouter(timeout ...int) *ChiRouter {
// default timeout if timeout param is nil (3 seconds)
ctxTimeout := 30 * 6
if timeout != nil && len(timeout) == 1 {
ctxTimeout = timeout[0]
}
return &ChiRouter{handle: chi.NewRouter(), timeout: ctxTimeout}
}
func (cr *ChiRouter) GetHandler() *chi.Mux {
return cr.handle
}
func (cr *ChiRouter) InitRoute(corsConfig ...cors.Options) {
cr.Use(
middleware.Logger,
middleware.Recoverer,
)
if corsConfig != nil && len(corsConfig) > 0 {
cr.Use(cors.Handler(corsConfig[0]))
}
cr.Get("/ping", func(_ http.ResponseWriter, _ *http.Request) *response.JSON {
return response.NewJSON().Success("PONG")
})
}
func (cr *ChiRouter) Group(prefix string, fn func(r RouteInterface)) RouteInterface {
subRouter := NewChiRouter()
if fn != nil {
fn(subRouter)
}
cr.Mount(prefix, subRouter)
return subRouter
}
func (cr *ChiRouter) Mount(pattern string, handler http.Handler) {
cr.handle.Mount(pattern, handler)
}
func (cr *ChiRouter) Handle(pattern string, handler http.Handler) {
cr.handle.Handle(pattern, handler)
}
func (cr *ChiRouter) Use(middlewares ...func(http.Handler) http.Handler) {
cr.handle.Use(middlewares...)
}
func (cr *ChiRouter) With(middlewares ...func(http.Handler) http.Handler) RouteInterface {
cr.handle.With(middlewares...)
return cr
}
func (cr *ChiRouter) StaticFS(pattern, imgDir string) {
handler := cr.createStaticHandler(pattern, imgDir)
pattern = path.Join(pattern, "/:name")
cr.handle.Get(pattern, handler)
cr.handle.Head(pattern, handler)
}
func (cr *ChiRouter) createStaticHandler(pattern, imgStaticDir string) http.HandlerFunc {
fileSystem := http.Dir(imgStaticDir)
fileServer := http.StripPrefix(pattern, http.FileServer(fileSystem))
return func(w http.ResponseWriter, r *http.Request) {
fileName := chi.URLParam(r, "name")
file, err := fileSystem.Open(fileName)
if err != nil {
log.Info().Err(err).Msg("error when Open File")
return
}
_ = file.Close()
log.Info().Msg("done")
fileServer.ServeHTTP(w, r)
return
}
}
// FileServer is serving static files
func FileServer(r chi.Router, public string, static string) {
if strings.ContainsAny(public, "{}*") {
panic("FileServer does not permit URL parameters.")
}
root, _ := filepath.Abs(static)
if _, err := os.Stat(root); os.IsNotExist(err) {
panic("Static Documents Directory Not Found")
}
fs := http.StripPrefix(public, http.FileServer(http.Dir(root)))
log.Info().Msgf("%s -> %s", public, root)
if public != "/" && public[len(public)-1] != '/' {
r.Get(public, http.RedirectHandler(public+"/", 301).ServeHTTP)
public += "/"
}
r.Get(public+"*", func(w http.ResponseWriter, r *http.Request) {
file := strings.Replace(r.RequestURI, public, "/", 1)
if _, err := os.Stat(root + file); os.IsNotExist(err) {
http.ServeFile(w, r, path.Join(root, "index.html"))
return
}
fs.ServeHTTP(w, r)
})
}
func (cr *ChiRouter) Get(pattern string, handler WrapperFunc) {
cr.handle.Get(pattern, cr.wrapper(pattern, handler))
}
func (cr *ChiRouter) Post(pattern string, handler WrapperFunc) {
cr.handle.Post(pattern, cr.wrapper(pattern, handler))
}
func (cr *ChiRouter) Patch(pattern string, handler WrapperFunc) {
cr.handle.Patch(pattern, cr.wrapper(pattern, handler))
}
func (cr *ChiRouter) Delete(pattern string, handler WrapperFunc) {
cr.handle.Delete(pattern, cr.wrapper(pattern, handler))
}
func (cr *ChiRouter) Put(pattern string, handler WrapperFunc) {
cr.handle.Put(pattern, cr.wrapper(pattern, handler))
}
func (cr *ChiRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cr.handle.ServeHTTP(w, r)
}
func (cr *ChiRouter) Routes() []chi.Route {
return cr.handle.Routes()
}
func (cr *ChiRouter) Middlewares() chi.Middlewares {
return cr.handle.Middlewares()
}
// Match(rctx *Context, method, path string) bool
func (cr *ChiRouter) Match(rctx *chi.Context, method, path string) bool {
return cr.handle.Match(rctx, method, path)
}
func (cr *ChiRouter) wrapper(pattern string, handler WrapperFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
t := time.Now()
ctx, cancel := context.WithTimeout(request.Context(), time.Second*time.Duration(cr.timeout))
defer cancel()
respChan := make(chan *response.JSON)
go func() {
defer panicRecover(request, pattern)
//handler
resp := handler(writer, request)
respChan <- resp
}()
select {
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
writer.WriteHeader(http.StatusGatewayTimeout)
_, err := writer.Write([]byte("timeout"))
if err != nil {
log.Error().Err(err).Msg("context deadline exceed")
}
}
case responseFunc := <-respChan:
if responseFunc != nil {
_ = responseFunc.ErrorChecking(request)
responseFunc.Latency = time.Since(t).Seconds() * 1000
responseFunc.Send(writer)
} else {
log.Info().Msg(request.URL.Path + ": handler send nil response")
}
}
}
}
func panicRecover(r *http.Request, path string) {
if err := recover(); err != nil {
stackTrace := string(debug.Stack())
b, _ := io.ReadAll(r.Body)
fields := map[string]interface{}{
"Request.Body": string(b),
"Host": r.Host,
"RequestMethod": r.Method,
"RequestURI": r.RequestURI,
"Error": err,
"Path": path,
}
// slack.PostToSlack(fmt.Errorf("panic in api handler, [Path] %s, [err] %v", path, err), stackTrace, "", fields)
panicData := fields
panicData["StackTrace"] = stackTrace
panicData["IP"] = r.RemoteAddr
// route path
//routePath := r.URL.Path
//panicData["ServiceName"] = constants.ServiceName[routePath]
panicData["StackError"] = err
ctx := r.Context()
traceId := helper.GenerateUUIDV4()
byteData, _ := json.Marshal(panicData)
notifMsg := fmt.Sprintf(`your API is panic:
%s
traceID: %s`, string(byteData), traceId)
notif := context2.GetNotif(r.Context())
allNotifPlatform := notif.GetAllPlatform()
go func() {
for _, service := range allNotifPlatform {
if service.IsActive() {
if err := service.Send(ctx, notifMsg, nil); err != nil {
log.Error().Err(err).Msgf("error when send %s notifications", service.Type())
}
}
}
}()
log.Error().Fields(fields).Msg("got panic at handler")
}
}