Merge branch 'master' of github.com:gin-gonic/gin

This commit is contained in:
Manu Mtz-Almeida 2014-07-04 00:05:18 +02:00
commit edc3872dd0
3 changed files with 25 additions and 17 deletions

7
.travis.yml Normal file
View File

@ -0,0 +1,7 @@
language: go
go:
- 1.1
- 1.2
- 1.3
- tip

View File

@ -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/)
@ -259,7 +260,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
View File

@ -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"`
@ -53,7 +53,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
handlers404 []HandlerFunc handlers404 []HandlerFunc
@ -154,7 +154,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)
@ -240,7 +240,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)
@ -251,8 +251,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{
@ -274,8 +274,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{})
@ -322,8 +322,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 {
@ -336,8 +336,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 {
@ -350,8 +350,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")
@ -367,7 +367,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)
@ -376,7 +376,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)