forked from mirror/gin
Merge branch 'master' into develop
This commit is contained in:
commit
00413032bb
|
@ -0,0 +1,7 @@
|
||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.1
|
||||||
|
- 1.2
|
||||||
|
- 1.3
|
||||||
|
- tip
|
|
@ -1,6 +1,7 @@
|
||||||
#Gin Web Framework
|
#Gin Web Framework
|
||||||
|
|
||||||
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.png)](https://godoc.org/github.com/gin-gonic/gin)
|
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.png)](https://godoc.org/github.com/gin-gonic/gin)
|
||||||
|
[![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin)
|
||||||
|
|
||||||
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.
|
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.
|
||||||
[Check out the official web site](http://gin-gonic.github.io/gin/)
|
[Check out the official web site](http://gin-gonic.github.io/gin/)
|
||||||
|
@ -260,7 +261,7 @@ You can also use your own html template render
|
||||||
import "html/template"
|
import "html/template"
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
html := template.ParseFiles("file1", "file2")
|
html := template.Must(template.ParseFiles("file1", "file2"))
|
||||||
r.HTMLTemplates = html
|
r.HTMLTemplates = html
|
||||||
|
|
||||||
// Listen and server on 0.0.0.0:8080
|
// Listen and server on 0.0.0.0:8080
|
||||||
|
|
32
gin.go
32
gin.go
|
@ -23,7 +23,7 @@ type (
|
||||||
|
|
||||||
H map[string]interface{}
|
H map[string]interface{}
|
||||||
|
|
||||||
// Used internally to collect a error ocurred during a http request.
|
// Used internally to collect errors that occurred during an http request.
|
||||||
ErrorMsg struct {
|
ErrorMsg struct {
|
||||||
Err string `json:"error"`
|
Err string `json:"error"`
|
||||||
Meta interface{} `json:"meta"`
|
Meta interface{} `json:"meta"`
|
||||||
|
@ -58,7 +58,7 @@ type (
|
||||||
engine *Engine
|
engine *Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents the web framework, it wrappers the blazing fast httprouter multiplexer and a list of global middlewares.
|
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
|
||||||
Engine struct {
|
Engine struct {
|
||||||
*RouterGroup
|
*RouterGroup
|
||||||
HTMLTemplates *template.Template
|
HTMLTemplates *template.Template
|
||||||
|
@ -202,7 +202,7 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
|
||||||
group.Handlers = append(group.Handlers, middlewares...)
|
group.Handlers = append(group.Handlers, middlewares...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Greates a new router group. You should create add all the routes that share that have common middlwares or same path prefix.
|
// Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
||||||
// For example, all the routes that use a common middlware for authorization could be grouped.
|
// For example, all the routes that use a common middlware for authorization could be grouped.
|
||||||
func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup {
|
func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup {
|
||||||
prefix := path.Join(group.prefix, component)
|
prefix := path.Join(group.prefix, component)
|
||||||
|
@ -307,7 +307,7 @@ func (c *Context) Abort(code int) {
|
||||||
c.index = AbortIndex
|
c.index = AbortIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fail is the same than Abort plus an error message.
|
// Fail is the same as Abort plus an error message.
|
||||||
// Calling `context.Fail(500, err)` is equivalent to:
|
// Calling `context.Fail(500, err)` is equivalent to:
|
||||||
// ```
|
// ```
|
||||||
// context.Error("Operation aborted", err)
|
// context.Error("Operation aborted", err)
|
||||||
|
@ -318,8 +318,8 @@ func (c *Context) Fail(code int, err error) {
|
||||||
c.Abort(code)
|
c.Abort(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attachs an error to the current context. The error is pushed to a list of errors.
|
// Attaches an error to the current context. The error is pushed to a list of errors.
|
||||||
// It's a gooc idea to call Error for each error ocurred during the resolution of a request.
|
// It's a good idea to call Error for each error that occurred during the resolution of a request.
|
||||||
// A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.
|
// A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.
|
||||||
func (c *Context) Error(err error, meta interface{}) {
|
func (c *Context) Error(err error, meta interface{}) {
|
||||||
c.Errors = append(c.Errors, ErrorMsg{
|
c.Errors = append(c.Errors, ErrorMsg{
|
||||||
|
@ -341,8 +341,8 @@ func (c *Context) LastError() error {
|
||||||
/******** METADATA MANAGEMENT********/
|
/******** METADATA MANAGEMENT********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
||||||
// Sets a new pair key/value just for the specefied context.
|
// Sets a new pair key/value just for the specified context.
|
||||||
// It also lazy initializes the hashmap
|
// It also lazy initializes the hashmap.
|
||||||
func (c *Context) Set(key string, item interface{}) {
|
func (c *Context) Set(key string, item interface{}) {
|
||||||
if c.Keys == nil {
|
if c.Keys == nil {
|
||||||
c.Keys = make(map[string]interface{})
|
c.Keys = make(map[string]interface{})
|
||||||
|
@ -389,8 +389,8 @@ func (c *Context) ParseBody(item interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serializes the given struct as a JSON into the response body in a fast and efficient way.
|
// Serializes the given struct as JSON into the response body in a fast and efficient way.
|
||||||
// It also sets the Content-Type as "application/json"
|
// It also sets the Content-Type as "application/json".
|
||||||
func (c *Context) JSON(code int, obj interface{}) {
|
func (c *Context) JSON(code int, obj interface{}) {
|
||||||
c.Writer.Header().Set("Content-Type", "application/json")
|
c.Writer.Header().Set("Content-Type", "application/json")
|
||||||
if code >= 0 {
|
if code >= 0 {
|
||||||
|
@ -403,8 +403,8 @@ func (c *Context) JSON(code int, obj interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serializes the given struct as a XML into the response body in a fast and efficient way.
|
// Serializes the given struct as XML into the response body in a fast and efficient way.
|
||||||
// It also sets the Content-Type as "application/xml"
|
// It also sets the Content-Type as "application/xml".
|
||||||
func (c *Context) XML(code int, obj interface{}) {
|
func (c *Context) XML(code int, obj interface{}) {
|
||||||
c.Writer.Header().Set("Content-Type", "application/xml")
|
c.Writer.Header().Set("Content-Type", "application/xml")
|
||||||
if code >= 0 {
|
if code >= 0 {
|
||||||
|
@ -417,8 +417,8 @@ func (c *Context) XML(code int, obj interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renders the HTTP template specified by his file name.
|
// Renders the HTTP template specified by its file name.
|
||||||
// It also update the HTTP code and sets the Content-Type as "text/html".
|
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
||||||
// See http://golang.org/doc/articles/wiki/
|
// See http://golang.org/doc/articles/wiki/
|
||||||
func (c *Context) HTML(code int, name string, data interface{}) {
|
func (c *Context) HTML(code int, name string, data interface{}) {
|
||||||
c.Writer.Header().Set("Content-Type", "text/html")
|
c.Writer.Header().Set("Content-Type", "text/html")
|
||||||
|
@ -434,7 +434,7 @@ func (c *Context) HTML(code int, name string, data interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Writes the given string into the response body and sets the Content-Type to "text/plain"
|
// Writes the given string into the response body and sets the Content-Type to "text/plain".
|
||||||
func (c *Context) String(code int, msg string) {
|
func (c *Context) String(code int, msg string) {
|
||||||
if code >= 0 {
|
if code >= 0 {
|
||||||
c.Writer.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
|
@ -443,7 +443,7 @@ func (c *Context) String(code int, msg string) {
|
||||||
c.Writer.Write([]byte(msg))
|
c.Writer.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, data []byte) {
|
func (c *Context) Data(code int, data []byte) {
|
||||||
c.Writer.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
c.Writer.Write(data)
|
c.Writer.Write(data)
|
||||||
|
|
Loading…
Reference in New Issue