mirror of https://github.com/gin-gonic/gin.git
Merge branch 'redirect' of https://github.com/alexandernyquist/gin into develop
This commit is contained in:
commit
ceec1b6443
12
README.md
12
README.md
|
@ -251,7 +251,7 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### XML, and JSON rendering
|
#### XML and JSON rendering
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -320,6 +320,16 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Redirects
|
||||||
|
|
||||||
|
Issuing a HTTP redirect is easy:
|
||||||
|
|
||||||
|
```r.GET("/test", func(c *gin.Context) {
|
||||||
|
c.Redirect(301, "http://www.google.com/")
|
||||||
|
})
|
||||||
|
|
||||||
|
Both internal and external locations are supported.
|
||||||
|
```
|
||||||
|
|
||||||
#### Custom Middlewares
|
#### Custom Middlewares
|
||||||
|
|
||||||
|
|
|
@ -247,6 +247,15 @@ func (c *Context) String(code int, format string, values ...interface{}) {
|
||||||
c.Render(code, render.Plain, format, values)
|
c.Render(code, render.Plain, format, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a HTTP redirect to the specific location.
|
||||||
|
func (c *Context) Redirect(code int, location string) {
|
||||||
|
if code >= 300 && code <= 308 {
|
||||||
|
c.Render(code, render.Redirect, location)
|
||||||
|
} else {
|
||||||
|
panic(fmt.Sprintf("Cannot send a redirect with status code %d", code))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Writes some data into the body stream and updates the HTTP code.
|
// Writes some data into the body stream and updates the HTTP code.
|
||||||
func (c *Context) Data(code int, contentType string, data []byte) {
|
func (c *Context) Data(code int, contentType string, data []byte) {
|
||||||
if len(contentType) > 0 {
|
if len(contentType) > 0 {
|
||||||
|
|
|
@ -22,6 +22,9 @@ type (
|
||||||
// Plain text
|
// Plain text
|
||||||
plainRender struct{}
|
plainRender struct{}
|
||||||
|
|
||||||
|
// Redirects
|
||||||
|
redirectRender struct{}
|
||||||
|
|
||||||
// form binding
|
// form binding
|
||||||
HTMLRender struct {
|
HTMLRender struct {
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
|
@ -32,6 +35,7 @@ var (
|
||||||
JSON = jsonRender{}
|
JSON = jsonRender{}
|
||||||
XML = xmlRender{}
|
XML = xmlRender{}
|
||||||
Plain = plainRender{}
|
Plain = plainRender{}
|
||||||
|
Redirect = redirectRender{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
||||||
|
@ -45,6 +49,12 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{})
|
||||||
return encoder.Encode(data[0])
|
return encoder.Encode(data[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||||
|
w.Header().Set("Location", data[0].(string))
|
||||||
|
w.WriteHeader(code)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||||
writeHeader(w, code, "application/xml")
|
writeHeader(w, code, "application/xml")
|
||||||
encoder := xml.NewEncoder(w)
|
encoder := xml.NewEncoder(w)
|
||||||
|
|
Loading…
Reference in New Issue