Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Context enhancement #27

Merged
merged 38 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8554735
Add Context.JSON
razonyang Apr 2, 2020
c632a4d
Add Context.String
razonyang Apr 2, 2020
f88af6f
Fix typo
razonyang Apr 2, 2020
ad625b1
Improve Context.JSON unit test
razonyang Apr 2, 2020
d8ea2f4
Add Context.XML
razonyang Apr 2, 2020
01ad987
Update Context.JSON unit test
razonyang Apr 2, 2020
eebb001
Content.SetContentTypeJSON and Content.SetContentTypeXML append chars…
razonyang Apr 2, 2020
1826de2
Add Context.HTML
razonyang Apr 2, 2020
a514ae7
Add Context.Cookie and Context.Cookies
razonyang Apr 2, 2020
6ea73f9
Add Context.FormValue
razonyang Apr 2, 2020
ac7b896
Add Context.PostFormValue
razonyang Apr 2, 2020
4cfb0b5
Add Context.QueryString
razonyang Apr 3, 2020
92a4ace
Add Context.QueryParams and Context.QueryParam
razonyang Apr 3, 2020
200bbc6
Add Context.JSONP and Context.JSONPCallback
razonyang Apr 3, 2020
82d0c78
Update README
razonyang Apr 3, 2020
87e6e98
Add Context.Render
razonyang Apr 3, 2020
f273951
Add Context.RouteURL to generate URL of the naming route
razonyang Apr 3, 2020
2312e70
Update README
razonyang Apr 3, 2020
f3164b3
Fix typo
razonyang Apr 3, 2020
8a90ec8
Use sync.Pool for retrieving buffer
razonyang Apr 3, 2020
f4fae86
Update README
razonyang Apr 3, 2020
aec1c54
Fix unit test of Context.RouteURL
razonyang Apr 3, 2020
30fd5e7
Minor improvement
razonyang Apr 3, 2020
662c236
Remove init()
razonyang Apr 3, 2020
1bea186
Add Content-Type constants
razonyang Apr 3, 2020
3da7518
Add Context.Emit and Context.Blob
razonyang Apr 3, 2020
7a4cf62
Add Context.HtmlBlob
razonyang Apr 3, 2020
e4c07bd
Update CHANGELOG.md
razonyang Apr 3, 2020
fc9e18a
Add Context.XMLBlob
razonyang Apr 3, 2020
5daba16
Add Context.JSONBlob
razonyang Apr 3, 2020
8ea7fb3
Clean up unit test
razonyang Apr 3, 2020
f3ee58c
Add Context.JSONPBlob and Context.JSONPCallbackBlob
razonyang Apr 3, 2020
cd5a20a
JSONP get rid of fmt
razonyang Apr 3, 2020
5d6bd4d
Move params unit test to params_test.go
razonyang Apr 4, 2020
4aa2e7f
Update unit test
razonyang Apr 4, 2020
0f2dd6e
Add RecoveryLogger
razonyang Apr 4, 2020
b02c65b
Update README
razonyang Apr 4, 2020
fb396b1
Release v1.9.0
razonyang Apr 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ Change Log

under development
-----------------
- Add `Context.JSON` to send JSON response.
- Add `Context.String` to send string response.
- Add `Context.XML` to send XML response.
- `Content.SetContentTypeJSON` and `Content.SetContentTypeXML` append `charset=utf-8` to content type header.
- Add `Context.HTML` to send HTML response.
- Add `Context.Cookie` and `Context.Cookies`.
- Add `Context.FormValue`.
- Add `Context.PostFormValue`.
- Add `Context.QueryString`.
- Add `Context.QueryParams` and `Context.QueryParam`.
- Add `Context.JSONP` and `Context.JSONPCallback`.

v1.8.1 April 2, 2020
--------------------
Expand Down
119 changes: 117 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ package clevergo

import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
)

// Context contains incoming request, route, params and manages outgoing response.
Expand All @@ -16,6 +20,7 @@ type Context struct {
Route *Route
Request *http.Request
Response http.ResponseWriter
query url.Values
}

func newContext(w http.ResponseWriter, r *http.Request) *Context {
Expand All @@ -30,6 +35,7 @@ func (ctx *Context) reset() {
ctx.Route = nil
ctx.Response = nil
ctx.Request = nil
ctx.query = nil
}

// Error is a shortcut of http.Error.
Expand Down Expand Up @@ -64,12 +70,22 @@ func (ctx *Context) SetContentTypeText() {

// SetContentTypeJSON sets the content type as JSON.
func (ctx *Context) SetContentTypeJSON() {
ctx.SetContentType("application/json")
ctx.SetContentType("application/json; charset=utf-8")
}

// SetContentTypeXML sets the content type as XML.
func (ctx *Context) SetContentTypeXML() {
ctx.SetContentType("application/xml")
ctx.SetContentType("application/xml; charset=utf-8")
}

// Cookie is a shortcut of http.Request.Cookie.
func (ctx *Context) Cookie(name string) (*http.Cookie, error) {
return ctx.Request.Cookie(name)
}

// Cookies is a shortcut of http.Request.Cookies.
func (ctx *Context) Cookies() []*http.Cookie {
return ctx.Request.Cookies()
}

// SetCookie is a shortcut of http.SetCookie.
Expand Down Expand Up @@ -146,3 +162,102 @@ func (ctx *Context) IsPut() bool {
func (ctx *Context) GetHeader(name string) string {
return ctx.Request.Header.Get(name)
}

// JSON sends JSON response with status code, it also sets
// Content-Type as "application/json".
func (ctx *Context) JSON(code int, data interface{}) error {
bs, err := json.Marshal(data)
if err != nil {
return err
}

ctx.SetContentTypeJSON()
ctx.Response.WriteHeader(code)
_, err = ctx.Response.Write(bs)
return err
}

// JSONP is a shortcut of JSONPCallback with specified callback param name.
func (ctx *Context) JSONP(code int, data interface{}) error {
return ctx.JSONPCallback(code, "callback", data)
}

// JSONPCallback sends JSONP response with status code, it also sets
// Content-Type as "application/javascript".
// If the callback is not present, returns JSON response instead.
func (ctx *Context) JSONPCallback(code int, callback string, data interface{}) error {
fn := ctx.QueryParam(callback)
if fn == "" {
return ctx.JSON(code, data)
}

bs, err := json.Marshal(data)
if err != nil {
return err
}

ctx.SetContentType("application/javascript; charset=utf-8")
ctx.Response.WriteHeader(code)
_, err = ctx.WriteString(fmt.Sprintf("%s(%s)", fn, bs))
razonyang marked this conversation as resolved.
Show resolved Hide resolved
return err
}

// String send string response with status code, it also sets
// Content-Type as "text/plain; charset=utf-8".
func (ctx *Context) String(code int, s string) error {
ctx.SetContentTypeText()
ctx.Response.WriteHeader(code)
_, err := ctx.WriteString(s)
return err
}

// XML sends XML response with status code, it also sets
// Content-Type as "application/xml".
func (ctx *Context) XML(code int, data interface{}) error {
bs, err := xml.Marshal(data)
if err != nil {
return err
}

ctx.SetContentTypeXML()
ctx.Response.WriteHeader(code)
_, err = ctx.Response.Write(bs)
return err
}

// HTML sends HTML response with status code, it also sets
// Content-Type as "text/html".
func (ctx *Context) HTML(code int, html string) error {
ctx.SetContentTypeHTML()
ctx.Response.WriteHeader(code)
_, err := ctx.WriteString(html)
return err
}

// FormValue is a shortcut of http.Request.FormValue.
func (ctx *Context) FormValue(key string) string {
return ctx.Request.FormValue(key)
}

// PostFormValue is a shortcut of http.Request.PostFormValue.
func (ctx *Context) PostFormValue(key string) string {
return ctx.Request.PostFormValue(key)
}

// QueryString returns the raw query of request URL.
func (ctx *Context) QueryString() string {
return ctx.Request.URL.RawQuery
}

// QueryParam returns the param for the given key.
func (ctx *Context) QueryParam(key string) string {
return ctx.QueryParams().Get(key)
}

// QueryParams returns request URL values.
func (ctx *Context) QueryParams() url.Values {
if ctx.query == nil {
ctx.query = ctx.Request.URL.Query()
}
return ctx.query
}