-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
163 lines (136 loc) · 3.6 KB
/
context.go
File metadata and controls
163 lines (136 loc) · 3.6 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
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
package cycapi
import (
"context"
"encoding/json"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"net/http"
"net/url"
"sync"
)
type Context struct {
context.Context
Response http.ResponseWriter
Request *http.Request
Params url.Values
Data *sync.Map
SpanRoot *sentry.Span
RouteInfo struct {
VersionName string
ResourceName string
ResourceID string
SubresourceName string
SubresourceID string
Method string
CustomMethod string
}
sentryHub *sentry.Hub
Logger *StandardLogger
}
func NewContext(res http.ResponseWriter, req *http.Request) *Context {
// Parse URL Params
params := url.Values{}
// Parse URL Query String Params
// For POST, PUT, and PATCH requests, it also parse the request body as a form.
// Request body parameters take precedence over URL query string values in params
if err := req.ParseForm(); err == nil {
for k, v := range req.Form {
for _, vv := range v {
params.Add(k, vv)
}
}
}
data := &sync.Map{}
// Initialize Sentry Hub and attach it to the context if sentry is initialized
var hub *sentry.Hub
if SentryEnabled {
hub = sentry.CurrentHub().Clone()
}
// Determine Trace ID
traceId := req.Header.Get("TRACE-ID")
if traceId == "" {
traceId = "unknown_" + uuid.New().String()
}
spanRoot := sentry.StartSpan(req.Context(), "New Context",
sentry.TransactionName(traceId))
copy(spanRoot.TraceID[:], traceId)
return &Context{
Context: req.Context(),
Response: res,
Request: req,
Params: params,
Data: data,
SpanRoot: spanRoot,
sentryHub: hub,
Logger: NewLogger(),
}
}
func (ctx *Context) SendError(error error, status int) {
ctx.Response.Header().Set("Content-Type", "application/json")
ctx.Response.WriteHeader(status)
bytesRep, _ := json.Marshal(error.Error())
_, _ = ctx.Response.Write(bytesRep)
}
//
func (ctx *Context) SendSuccess(body interface{}) {
ctx.Response.Header().Set("Content-Type", "application/json")
ctx.Response.WriteHeader(http.StatusOK)
bytesRep, _ := json.Marshal(body)
_, _ = ctx.Response.Write(bytesRep)
}
func (ctx *Context) NotFound() {
ctx.Response.WriteHeader(http.StatusNotFound)
}
func (ctx *Context) MethodNotAllowed() {
ctx.Response.WriteHeader(http.StatusMethodNotAllowed)
}
func (ctx *Context) NoContent() {
ctx.Response.WriteHeader(http.StatusNoContent)
}
func (ctx *Context) UnprocessableEntity() {
ctx.Response.WriteHeader(http.StatusUnprocessableEntity)
}
func (ctx *Context) Unauthorized() {
ctx.Response.WriteHeader(http.StatusUnauthorized)
}
func (ctx *Context) BadRequest() {
ctx.Response.WriteHeader(http.StatusBadRequest)
}
func (ctx *Context) InternalServerError(err error) {
sentry.CaptureException(err)
fmt.Println(err.Error())
ctx.Response.WriteHeader(http.StatusInternalServerError)
}
func (ctx *Context) Ok() {
ctx.Response.WriteHeader(http.StatusOK)
}
func (ctx *Context) SetTag(key, value string) {
if ctx.sentryHub != nil && SentryEnabled {
ctx.sentryHub.Scope().SetTag(key, value)
}
}
func (ctx *Context) HubError(err error) {
if ctx.sentryHub != nil && SentryEnabled {
ctx.sentryHub.CaptureException(err)
}
}
func (ctx *Context) HubMessage(msg string) {
if ctx.sentryHub != nil && SentryEnabled {
ctx.sentryHub.CaptureMessage(msg)
}
}
func (ctx *Context) HubBreadcrumb(category, msg string, data map[string]interface{}) {
if ctx.sentryHub != nil && SentryEnabled {
ctx.sentryHub.AddBreadcrumb(&sentry.Breadcrumb{
Category: category,
Message: msg,
Data: data,
Level: sentry.LevelInfo,
},
&sentry.BreadcrumbHint{})
}
}
func (ctx *Context) Close() {
ctx.SpanRoot.Finish()
}