This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
context.go
148 lines (120 loc) · 4.06 KB
/
context.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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package clevergo
import (
"context"
"io"
"net/http"
)
// Context contains incoming request, route, params and manages outgoing response.
type Context struct {
Params Params
Route *Route
Request *http.Request
Response http.ResponseWriter
}
func newContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{
Request: r,
Response: w,
}
}
func (ctx *Context) reset() {
ctx.Params = nil
ctx.Route = nil
ctx.Response = nil
ctx.Request = nil
}
// Error is a shortcut of http.Error.
func (ctx *Context) Error(msg string, code int) {
http.Error(ctx.Response, msg, code)
}
// NotFound is a shortcut of http.NotFound.
func (ctx *Context) NotFound() {
http.NotFound(ctx.Response, ctx.Request)
}
// Redirect is a shortcut of http.Redirect.
func (ctx *Context) Redirect(url string, code int) {
http.Redirect(ctx.Response, ctx.Request, url, code)
}
// SetContentType sets the content type header.
func (ctx *Context) SetContentType(v string) {
ctx.Response.Header().Set("Content-Type", v)
}
// SetContentTypeHTML sets the content type as HTML.
func (ctx *Context) SetContentTypeHTML() {
ctx.SetContentType("text/html; charset=utf-8")
}
// SetContentTypeText sets the content type as text.
func (ctx *Context) SetContentTypeText() {
ctx.SetContentType("text/plain; charset=utf-8")
}
// SetContentTypeJSON sets the content type as JSON.
func (ctx *Context) SetContentTypeJSON() {
ctx.SetContentType("application/json")
}
// SetContentTypeXML sets the content type as XML.
func (ctx *Context) SetContentTypeXML() {
ctx.SetContentType("application/xml")
}
// SetCookie is a shortcut of http.SetCookie.
func (ctx *Context) SetCookie(cookie *http.Cookie) {
http.SetCookie(ctx.Response, cookie)
}
// Write is a shortcut of http.ResponseWriter.Write.
func (ctx *Context) Write(data []byte) (int, error) {
return ctx.Response.Write(data)
}
// WriteString writes the string data to response.
func (ctx *Context) WriteString(data string) (int, error) {
return io.WriteString(ctx.Response, data)
}
// WriteHeader is a shortcut of http.ResponseWriter.WriteHeader.
func (ctx *Context) WriteHeader(code int) {
ctx.Response.WriteHeader(code)
}
// WithValue stores the given value under the given key.
func (ctx *Context) WithValue(key, val interface{}) {
ctx.Request = ctx.Request.WithContext(context.WithValue(ctx.Request.Context(), key, val))
}
// Value returns the value of the given key.
func (ctx *Context) Value(key interface{}) interface{} {
return ctx.Request.Context().Value(key)
}
// IsMethod returns a boolean value indicates whether the request method is the given method.
func (ctx *Context) IsMethod(method string) bool {
return ctx.Request.Method == method
}
// IsAJAX indicates whether it is an AJAX (XMLHttpRequest) request.
func (ctx *Context) IsAJAX() bool {
return ctx.Request.Header.Get("X-Requested-With") == "XMLHttpRequest"
}
// IsDelete returns a boolean value indicates whether the request method is DELETE.
func (ctx *Context) IsDelete() bool {
return ctx.IsMethod(http.MethodDelete)
}
// IsGet returns a boolean value indicates whether the request method is GET.
func (ctx *Context) IsGet() bool {
return ctx.IsMethod(http.MethodGet)
}
// IsOptions returns a boolean value indicates whether the request method is OPTIONS.
func (ctx *Context) IsOptions() bool {
return ctx.IsMethod(http.MethodOptions)
}
// IsPatch returns a boolean value indicates whether the request method is PATCH.
func (ctx *Context) IsPatch() bool {
return ctx.IsMethod(http.MethodPatch)
}
// IsPost returns a boolean value indicates whether the request method is POST.
func (ctx *Context) IsPost() bool {
return ctx.IsMethod(http.MethodPost)
}
// IsPut returns a boolean value indicates whether the request method is PUT.
func (ctx *Context) IsPut() bool {
return ctx.IsMethod(http.MethodPut)
}
// GetHeader is a shortcut of http.Request.Header.Get.
func (ctx *Context) GetHeader(name string) string {
return ctx.Request.Header.Get(name)
}