Skip to content

Commit 594cd5b

Browse files
authored
Feat/97/rest (#98)
* [#97] Refactored Context to ServerContext * [#97] Updated Readme Examples.
1 parent 598a6a3 commit 594cd5b

File tree

5 files changed

+54
-54
lines changed

5 files changed

+54
-54
lines changed

rest/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ func main() {
249249
// this is the path prefix for each endpoint. Default is empty and no path prefix is added
250250
srv.Opts().PathPrefix = "/api/v1"
251251
// Add a GET endpoint
252-
srv.Get("healthCheck", func(ctx rest.Context) {
252+
srv.Get("healthCheck", func(ctx rest.ServerContext) {
253253
// Set the status code. Remember to set the status code before writing the response
254254
ctx.SetStatusCode(http.StatusOK)
255255
ctx.WriteString("Health Check Get")
256256
})
257257
// Add a POST endpoint
258-
srv.Post("healthCheck", func(ctx rest.Context) {
258+
srv.Post("healthCheck", func(ctx rest.ServerContext) {
259259
input, _ := ctx.GetBody()
260260
// Set the status code. Remember to set the status code before writing the response
261261
ctx.SetStatusCode(http.StatusOK)
@@ -294,7 +294,7 @@ func main() {
294294
// this is the path prefix for each endpoint. Default is empty and no path prefix is added
295295
srv.Opts().PathPrefix = "/api/v1"
296296
// Add a POST endpoint
297-
srv.Post("healthCheck", func(ctx rest.Context) {
297+
srv.Post("healthCheck", func(ctx rest.ServerContext) {
298298
input, _ := ctx.GetBody()
299299
// Set the status code. Remember to set the status code before writing the response
300300
ctx.SetStatusCode(http.StatusOK)
@@ -333,7 +333,7 @@ func main() {
333333
// this is the path prefix for each endpoint. Default is empty and no path prefix is added
334334
srv.Opts().PathPrefix = "/api/v1"
335335
// Add a PUT endpoint
336-
srv.Put("healthCheck", func(ctx rest.Context) {
336+
srv.Put("healthCheck", func(ctx rest.ServerContext) {
337337
input, _ := ctx.GetBody()
338338
// Set the status code. Remember to set the status code before writing the response
339339
ctx.SetStatusCode(http.StatusOK)
@@ -372,7 +372,7 @@ func main() {
372372
// this is the path prefix for each endpoint. Default is empty and no path prefix is added
373373
srv.Opts().PathPrefix = "/api/v1"
374374
// Add a DELETE endpoint
375-
srv.Delete("healthCheck", func(ctx rest.Context) {
375+
srv.Delete("healthCheck", func(ctx rest.ServerContext) {
376376
input, _ := ctx.GetBody()
377377
// Set the status code. Remember to set the status code before writing the response
378378
ctx.SetStatusCode(http.StatusOK)
@@ -411,7 +411,7 @@ func main() {
411411
// this is the path prefix for each endpoint. Default is empty and no path prefix is added
412412
srv.Opts().PathPrefix = "/api/v1"
413413
// Add a GET endpoint
414-
srv.Get("endpoint/:pathparam", func(ctx rest.Context) {
414+
srv.Get("endpoint/:pathparam", func(ctx rest.ServerContext) {
415415
// Get the query parameters
416416
queryParams := ctx.GetParam("paramName",rest.QueryParam)
417417
// Get the path parameters

rest/server.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
PathParam
2323
)
2424

25-
type HandlerFunc func(context Context)
25+
type HandlerFunc func(context ServerContext)
2626

2727
type Paramtype int
2828

@@ -72,7 +72,7 @@ func (rs *restServer) AddRoute(path string, handler HandlerFunc, methods ...stri
7272
}
7373
p = rs.opts.PathPrefix + p
7474
route, err = rs.router.Add(p, func(w http.ResponseWriter, r *http.Request) {
75-
ctx := Context{
75+
ctx := ServerContext{
7676
request: r,
7777
response: w,
7878
}
@@ -104,7 +104,7 @@ func (rs *restServer) Delete(path string, handler HandlerFunc) (route *turbo.Rou
104104
// Unhandled adds a handler for unhandled routes
105105
func (rs *restServer) Unhandled(handler HandlerFunc) (err error) {
106106
rs.router.SetUnmanaged(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
107-
ctx := Context{
107+
ctx := ServerContext{
108108
request: r,
109109
response: w,
110110
}
@@ -116,7 +116,7 @@ func (rs *restServer) Unhandled(handler HandlerFunc) (err error) {
116116
// Unsupported adds a handler for unsupported methods
117117
func (rs *restServer) Unsupported(handler HandlerFunc) (err error) {
118118
rs.router.SetUnsupportedMethod(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119-
ctx := Context{
119+
ctx := ServerContext{
120120
request: r,
121121
response: w,
122122
}

rest/server_context.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ var xmlCodec = codec.XmlCodec()
2020
// yamlCodec is the default codec for yaml
2121
var yamlCodec = codec.YamlCodec()
2222

23-
// Context is the struct that holds the request and response of the server.
24-
type Context struct {
23+
// ServerContext is the struct that holds the request and response of the server.
24+
type ServerContext struct {
2525
request *http.Request
2626
response http.ResponseWriter
2727
}
2828

2929
// Options is the struct that holds the configuration for the Server.
30-
func (c *Context) GetParam(name string, typ Paramtype) (string, error) {
30+
func (c *ServerContext) GetParam(name string, typ Paramtype) (string, error) {
3131
switch typ {
3232
case QueryParam:
3333
return turbo.GetQueryParam(name, c.request)
@@ -39,17 +39,17 @@ func (c *Context) GetParam(name string, typ Paramtype) (string, error) {
3939
}
4040

4141
// GetBody returns the body of the request.
42-
func (c *Context) GetBody() (io.Reader, error) {
42+
func (c *ServerContext) GetBody() (io.Reader, error) {
4343
return c.request.Body, nil
4444
}
4545

4646
// GetHeader returns the header of the request.
47-
func (c *Context) GetHeader(name string) string {
47+
func (c *ServerContext) GetHeader(name string) string {
4848
return c.request.Header.Get(name)
4949
}
5050

5151
// InHeaders returns the headers of the request.
52-
func (c *Context) InHeaders() http.Header {
52+
func (c *ServerContext) InHeaders() http.Header {
5353
// clone the headers
5454
headers := make(http.Header)
5555
for k, v := range c.request.Header {
@@ -59,23 +59,23 @@ func (c *Context) InHeaders() http.Header {
5959
}
6060

6161
// GetMethod returns the method of the request.
62-
func (c *Context) GetMethod() string {
62+
func (c *ServerContext) GetMethod() string {
6363
return c.request.Method
6464
}
6565

6666
// GetURL returns the URL of the request.
67-
func (c *Context) GetURL() string {
67+
func (c *ServerContext) GetURL() string {
6868
return c.request.URL.String()
6969
}
7070

7171
// GetRequest returns the request.
7272
// for most Rest Use cases this would not be required
73-
func (c *Context) GetRequest() *http.Request {
73+
func (c *ServerContext) GetRequest() *http.Request {
7474
return c.request
7575
}
7676

7777
// Read reads the body of the request into the given object.
78-
func (c *Context) Read(obj interface{}) error {
78+
func (c *ServerContext) Read(obj interface{}) error {
7979
contentType := c.request.Header.Get(ContentTypeHeader)
8080
codec, err := codec.GetDefault(contentType)
8181
if err != nil {
@@ -86,25 +86,25 @@ func (c *Context) Read(obj interface{}) error {
8686
}
8787

8888
// WriteJSON writes the object to the response in JSON format.
89-
func (c *Context) WriteJSON(data interface{}) error {
89+
func (c *ServerContext) WriteJSON(data interface{}) error {
9090
c.SetHeader(ContentTypeHeader, ioutils.MimeApplicationJSON)
9191
return jsonCodec.Write(data, c.response)
9292
}
9393

9494
// WriteXML writes the object to the response in XML format.
95-
func (c *Context) WriteXML(data interface{}) error {
95+
func (c *ServerContext) WriteXML(data interface{}) error {
9696
c.SetHeader(ContentTypeHeader, ioutils.MimeApplicationXML)
9797
return xmlCodec.Write(data, c.response)
9898
}
9999

100100
// WriteYAML writes the object to the response in YAML format.
101-
func (c *Context) WriteYAML(data interface{}) error {
101+
func (c *ServerContext) WriteYAML(data interface{}) error {
102102
c.SetHeader(ContentTypeHeader, ioutils.MimeTextYAML)
103103
return yamlCodec.Write(data, c.response)
104104
}
105105

106106
// Write writes the object to the response with the given content type and status code.
107-
func (c *Context) Write(data interface{}, contentType string) error {
107+
func (c *ServerContext) Write(data interface{}, contentType string) error {
108108
codec, err := codec.GetDefault(contentType)
109109
if err != nil {
110110
return err
@@ -114,42 +114,42 @@ func (c *Context) Write(data interface{}, contentType string) error {
114114
}
115115

116116
// WriteData writes the data to the response.
117-
func (c *Context) WriteData(data []byte) (int, error) {
117+
func (c *ServerContext) WriteData(data []byte) (int, error) {
118118
return c.response.Write(data)
119119
}
120120

121121
// WriteString writes the string to the response.
122-
func (c *Context) WriteString(data string) {
122+
func (c *ServerContext) WriteString(data string) {
123123

124124
io.Copy(c.response, strings.NewReader(data))
125125
}
126126

127127
// SetHeader sets the header of the response.
128-
func (c *Context) SetHeader(name, value string) {
128+
func (c *ServerContext) SetHeader(name, value string) {
129129
c.response.Header().Set(name, value)
130130
}
131131

132132
// SetContentType sets the content type of the response.
133-
func (c *Context) SetContentType(contentType string) {
133+
func (c *ServerContext) SetContentType(contentType string) {
134134
c.response.Header().Set(ContentTypeHeader, contentType)
135135
}
136136

137137
// SetStatusCode sets the status code of the response.
138-
func (c *Context) SetStatusCode(statusCode int) {
138+
func (c *ServerContext) SetStatusCode(statusCode int) {
139139
c.response.WriteHeader(statusCode)
140140
}
141141

142142
// SetCookie sets the cookie of the response.
143-
func (c *Context) SetCookie(cookie *http.Cookie) {
143+
func (c *ServerContext) SetCookie(cookie *http.Cookie) {
144144
http.SetCookie(c.response, cookie)
145145
}
146146

147147
// WriteFrom writes the data from the reader to the response.
148-
func (c *Context) WriteFrom(data io.Reader) {
148+
func (c *ServerContext) WriteFrom(data io.Reader) {
149149
io.Copy(c.response, data)
150150
}
151151

152152
// HttpResWriter returns the http.ResponseWriter
153-
func (c *Context) HttpResWriter() http.ResponseWriter {
153+
func (c *ServerContext) HttpResWriter() http.ResponseWriter {
154154
return c.response
155155
}

rest/server_context_test.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// TestContext_GetParam tests the GetParam function
1212
func TestContext_GetParam(t *testing.T) {
1313
req := httptest.NewRequest(http.MethodGet, "/test?param=value", nil)
14-
ctx := &Context{request: req}
14+
ctx := &ServerContext{request: req}
1515

1616
value, err := ctx.GetParam("param", QueryParam)
1717
if err != nil {
@@ -26,7 +26,7 @@ func TestContext_GetParam(t *testing.T) {
2626
func TestContext_GetBody(t *testing.T) {
2727
body := "test body"
2828
req := httptest.NewRequest(http.MethodPost, "/test", strings.NewReader(body))
29-
ctx := &Context{request: req}
29+
ctx := &ServerContext{request: req}
3030

3131
r, err := ctx.GetBody()
3232
if err != nil {
@@ -43,7 +43,7 @@ func TestContext_GetBody(t *testing.T) {
4343
func TestContext_GetHeader(t *testing.T) {
4444
req := httptest.NewRequest(http.MethodGet, "/test", nil)
4545
req.Header.Set("X-Test-Header", "test-value")
46-
ctx := &Context{request: req}
46+
ctx := &ServerContext{request: req}
4747

4848
value := ctx.GetHeader("X-Test-Header")
4949
if value != "test-value" {
@@ -55,7 +55,7 @@ func TestContext_GetHeader(t *testing.T) {
5555
func TestContext_InHeaders(t *testing.T) {
5656
req := httptest.NewRequest(http.MethodGet, "/test", nil)
5757
req.Header.Set("X-Test-Header", "test-value")
58-
ctx := &Context{request: req}
58+
ctx := &ServerContext{request: req}
5959

6060
headers := ctx.InHeaders()
6161
if headers.Get("X-Test-Header") != "test-value" {
@@ -66,7 +66,7 @@ func TestContext_InHeaders(t *testing.T) {
6666
// TestContext_GetMethod tests the GetMethod function
6767
func TestContext_GetMethod(t *testing.T) {
6868
req := httptest.NewRequest(http.MethodPost, "/test", nil)
69-
ctx := &Context{request: req}
69+
ctx := &ServerContext{request: req}
7070

7171
method := ctx.GetMethod()
7272
if method != http.MethodPost {
@@ -77,7 +77,7 @@ func TestContext_GetMethod(t *testing.T) {
7777
// TestContext_GetURL tests the GetURL function
7878
func TestContext_GetURL(t *testing.T) {
7979
req := httptest.NewRequest(http.MethodGet, "/test", nil)
80-
ctx := &Context{request: req}
80+
ctx := &ServerContext{request: req}
8181

8282
url := ctx.GetURL()
8383
if url != "/test" {
@@ -88,7 +88,7 @@ func TestContext_GetURL(t *testing.T) {
8888
// TestContext_GetRequest tests the GetRequest function
8989
func TestContext_GetRequest(t *testing.T) {
9090
req := httptest.NewRequest(http.MethodGet, "/test", nil)
91-
ctx := &Context{request: req}
91+
ctx := &ServerContext{request: req}
9292

9393
request := ctx.GetRequest()
9494
if request != req {
@@ -101,7 +101,7 @@ func TestContext_Read(t *testing.T) {
101101
body := `{"key":"value"}`
102102
req := httptest.NewRequest(http.MethodPost, "/test", strings.NewReader(body))
103103
req.Header.Set(ContentTypeHeader, "application/json")
104-
ctx := &Context{request: req}
104+
ctx := &ServerContext{request: req}
105105

106106
var obj map[string]string
107107
err := ctx.Read(&obj)
@@ -116,7 +116,7 @@ func TestContext_Read(t *testing.T) {
116116
// TestContext_Write tests the Write function
117117
func TestContext_Write(t *testing.T) {
118118
rec := httptest.NewRecorder()
119-
ctx := &Context{response: rec}
119+
ctx := &ServerContext{response: rec}
120120

121121
data := map[string]string{"key": "value"}
122122
err := ctx.Write(data, "application/json")
@@ -131,7 +131,7 @@ func TestContext_Write(t *testing.T) {
131131
// TestContext_WriteData tests the WriteData function
132132
func TestContext_WriteData(t *testing.T) {
133133
rec := httptest.NewRecorder()
134-
ctx := &Context{response: rec}
134+
ctx := &ServerContext{response: rec}
135135

136136
data := []byte("test data")
137137
n, err := ctx.WriteData(data)
@@ -149,7 +149,7 @@ func TestContext_WriteData(t *testing.T) {
149149
// TestContext_WriteString tests the WriteString function
150150
func TestContext_WriteString(t *testing.T) {
151151
rec := httptest.NewRecorder()
152-
ctx := &Context{response: rec}
152+
ctx := &ServerContext{response: rec}
153153

154154
data := "test string"
155155
ctx.WriteString(data)
@@ -161,7 +161,7 @@ func TestContext_WriteString(t *testing.T) {
161161
// TestContext_SetHeader tests the SetHeader function
162162
func TestContext_SetHeader(t *testing.T) {
163163
rec := httptest.NewRecorder()
164-
ctx := &Context{response: rec}
164+
ctx := &ServerContext{response: rec}
165165

166166
ctx.SetHeader("X-Test-Header", "test-value")
167167
if rec.Header().Get("X-Test-Header") != "test-value" {
@@ -172,7 +172,7 @@ func TestContext_SetHeader(t *testing.T) {
172172
// TestContext_SetContentType tests the SetContentType function
173173
func TestContext_SetContentType(t *testing.T) {
174174
rec := httptest.NewRecorder()
175-
ctx := &Context{response: rec}
175+
ctx := &ServerContext{response: rec}
176176

177177
ctx.SetContentType("application/json")
178178
if rec.Header().Get(ContentTypeHeader) != "application/json" {
@@ -183,7 +183,7 @@ func TestContext_SetContentType(t *testing.T) {
183183
// TestContext_SetStatusCode tests the SetStatusCode function
184184
func TestContext_SetStatusCode(t *testing.T) {
185185
rec := httptest.NewRecorder()
186-
ctx := &Context{response: rec}
186+
ctx := &ServerContext{response: rec}
187187

188188
ctx.SetStatusCode(http.StatusCreated)
189189
if rec.Code != http.StatusCreated {
@@ -194,7 +194,7 @@ func TestContext_SetStatusCode(t *testing.T) {
194194
// TestContext_SetCookie tests the SetCookie function
195195
func TestContext_SetCookie(t *testing.T) {
196196
rec := httptest.NewRecorder()
197-
ctx := &Context{response: rec}
197+
ctx := &ServerContext{response: rec}
198198

199199
cookie := &http.Cookie{Name: "test-cookie", Value: "test-value"}
200200
ctx.SetCookie(cookie)
@@ -206,7 +206,7 @@ func TestContext_SetCookie(t *testing.T) {
206206
// TestContext_WriteFrom tests the WriteFrom function
207207
func TestContext_WriteFrom(t *testing.T) {
208208
rec := httptest.NewRecorder()
209-
ctx := &Context{response: rec}
209+
ctx := &ServerContext{response: rec}
210210

211211
data := "test data"
212212
ctx.WriteFrom(strings.NewReader(data))
@@ -218,7 +218,7 @@ func TestContext_WriteFrom(t *testing.T) {
218218
// TestContext_HttpResWriter tests the HttpResWriter function
219219
func TestContext_HttpResWriter(t *testing.T) {
220220
rec := httptest.NewRecorder()
221-
ctx := &Context{response: rec}
221+
ctx := &ServerContext{response: rec}
222222

223223
writer := ctx.HttpResWriter()
224224
if writer != rec {

0 commit comments

Comments
 (0)