-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.go
166 lines (134 loc) · 3.6 KB
/
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
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
package aefire
import (
"encoding/json"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"net/http"
"strings"
)
func NewAEFireEcho(aef *AEFire) *echo.Echo {
e := NewEcho()
e.Use(aef.EchoContextMiddleware)
return e
}
func NewEcho() *echo.Echo {
eco := echo.New()
loggerConfig := middleware.DefaultLoggerConfig
loggerConfig.Format = `{
"time":"${time_rfc3339}",
"method":"${method}",
"uri":"${uri}",
"status":"${status}",
"error":"${error}",
}`
eco.HTTPErrorHandler = func(err error, c echo.Context) {
a := CastEchoContext(c)
he, ok := err.(*echo.HTTPError)
if ok {
if he.Internal != nil {
if herr, ok := he.Internal.(*echo.HTTPError); ok {
he = herr
}
}
} else {
he = &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: err.Error(),
}
}
if eco.Debug {
he.Message = err.Error()
} else if m, ok := he.Message.(string); ok {
he.Message = echo.Map{"message": m}
}
// Send response
if !c.Response().Committed {
if c.Request().Method == http.MethodHead { // Issue #608
err = c.NoContent(he.Code)
} else {
body := MapOf("message", he.Message)
if a.UID() != nil {
body["uid"] = *a.UID()
} else {
body["uid"] = "anonymous"
}
err = c.JSON(he.Code, body)
}
if err != nil {
eco.Logger.Error(err)
}
}
}
eco.Use(
middleware.Recover(), // Recover from all panics to always have your server up
//middleware.LoggerWithConfig(loggerConfig),
middleware.RequestID(), // Generate a request id on the HTTP response headers for identification
)
eco.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
a := CastEchoContext(c)
req := MapOf()
res := MapOf()
if len(reqBody) > 0 {
json.Unmarshal(reqBody, &req)
}
if len(resBody) > 0 && strings.Contains(c.Response().Header().Get("content-type"), "json") {
json.Unmarshal(resBody, &res)
}
log := []interface{}{}
if a.UID() != nil {
log = append(log, *a.UID())
} else {
log = append(log, "anonymous")
}
log = append(log, c.Response().Status,
c.Request().Method,
c.Request().URL.String(),
req,
res,
c.Request().Header.Get("User-Agent"))
println(ToJson(log))
}))
if l, ok := eco.Logger.(*log.Logger); ok {
//"${time_rfc3339_nano}","level":"${level}","prefix":"${prefix}","file":"${short_file}","line":"${line}"
l.SetHeader(" ")
}
eco.Debug = true
return eco
}
func JsonMap(c echo.Context, pairs ...interface{}) error {
return c.JSON(200, MapOf(pairs...))
}
func JsonOk(c echo.Context) error {
return c.JSON(200, MapOf("message", "ok"))
}
func JsonMsg(c echo.Context, msg string) error {
return c.JSON(200, MapOf("message", msg))
}
func (aef *AEFire) IdTokenAuth(key string, c echo.Context) (result bool, err error) {
a := aef.WithEcho(c)
a.UserToken, err = a.Auth.VerifyIDToken(c.Request().Context(), key)
if err == nil {
return true, nil
} else {
return false, echo.NewHTTPError(401, "인증이 실패했습니다.")
}
}
func (aef *AEFire) ValidateIdToken(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
a := aef.WithEcho(c)
idToken := strings.TrimPrefix(c.Request().Header.Get("Authorization"), "Bearer ")
if idToken != "" {
a.UserToken, _ = a.Auth.VerifyIDToken(c.Request().Context(), idToken)
}
return next(c)
}
}
func AuthCronMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if !strings.Contains(c.Request().Header.Get("X-Forwarded-For"), "0.1.0.1") {
return echo.NewHTTPError(http.StatusUnauthorized)
}
return next(c)
}
}