-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
75 lines (63 loc) · 1.99 KB
/
Copy pathcontext.go
File metadata and controls
75 lines (63 loc) · 1.99 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
package rou
import (
"encoding/json"
"io"
"net/http"
"net/url"
)
type Context struct {
responseWriter http.ResponseWriter
request *http.Request
routeParams Storage
}
type Storage interface {
Delete(name string)
Has(name string) bool
Set(name, value string)
Get(name string) string
}
type ErrorObject struct {
Message string `json:"message"`
Code int `json:"code"`
}
type ResponseObject[T any] struct {
Error *ErrorObject `json:"error"`
Body T `json:"body"`
}
// Returns basic HTTP response writer
func (c Context) ResponseWriter() http.ResponseWriter {
return c.responseWriter
}
// Returns basic HTTP request object
func (c Context) Request() *http.Request {
return c.request
}
// Returns query params of request
func (c Context) Params() url.Values {
return c.request.URL.Query()
}
// Set response content type
func (ctx *Context) SetContentType(contentType string) {
ctx.responseWriter.Header().Add("Content-Type", contentType)
}
// Returns RouterParams structure with params of route as key value
//
// If you have route "/user/:id/posts/:postId" and request URL path "/user/1/posts/45" RouterParams will have map with keys
// took from route and values which it will take from request URL path `["id": "1", "postId": "45"]`
func (c Context) RouterParams() Storage {
return c.routeParams
}
func (c Context) ErrorJSONResponse(status int, message string) {
c.ResponseWriter().Header().Add("Content-Type", "application/json")
c.ResponseWriter().WriteHeader(status)
responseMsg := ResponseObject[any]{Error: &ErrorObject{Message: message, Code: status}}
jsonContent, _ := json.Marshal(responseMsg)
io.WriteString(c.ResponseWriter(), string(jsonContent))
}
func (c Context) SuccessJSONResponse(body any) {
c.ResponseWriter().Header().Add("Content-Type", "application/json")
c.ResponseWriter().WriteHeader(http.StatusOK)
responseMsg := ResponseObject[any]{Body: body}
jsonContent, _ := json.Marshal(responseMsg)
io.WriteString(c.ResponseWriter(), string(jsonContent))
}