Skip to content

Commit

Permalink
General refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Oct 8, 2014
1 parent 9441533 commit 07a3961
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 180 deletions.
62 changes: 37 additions & 25 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ type Context struct {
}

/************************************/
/********** ROUTES GROUPING *********/
/********** CONTEXT CREATION ********/
/************************************/

func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
c := engine.cache.Get().(*Context)
c := engine.pool.Get().(*Context)
c.writermem.reset(w)
c.Request = req
c.Params = params
Expand All @@ -87,9 +87,9 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
return c
}

/************************************/
/****** FLOW AND ERROR MANAGEMENT****/
/************************************/
func (engine *Engine) reuseContext(c *Context) {
engine.pool.Put(c)
}

func (c *Context) Copy() *Context {
var cp Context = *c
Expand All @@ -98,6 +98,10 @@ func (c *Context) Copy() *Context {
return &cp
}

/************************************/
/*************** FLOW ***************/
/************************************/

// Next should be used only in the middlewares.
// It executes the pending handlers in the chain inside the calling handler.
// See example in github.
Expand All @@ -109,25 +113,31 @@ func (c *Context) Next() {
}
}

// Forces the system to do not continue calling the pending handlers.
// For example, the first handler checks if the request is authorized. If it's not, context.Abort(401) should be called.
// The rest of pending handlers would never be called for that request.
func (c *Context) Abort(code int) {
if code >= 0 {
c.Writer.WriteHeader(code)
}
// Forces the system to do not continue calling the pending handlers in the chain.
func (c *Context) Abort() {
c.index = AbortIndex
}

// Same than AbortWithStatus() but also writes the specified response status code.
// For example, the first handler checks if the request is authorized. If it's not, context.AbortWithStatus(401) should be called.
func (c *Context) AbortWithStatus(code int) {
c.Writer.WriteHeader(code)
c.Abort()
}

/************************************/
/********* ERROR MANAGEMENT *********/
/************************************/

// Fail is the same as Abort plus an error message.
// Calling `context.Fail(500, err)` is equivalent to:
// ```
// context.Error("Operation aborted", err)
// context.Abort(500)
// context.AbortWithStatus(500)
// ```
func (c *Context) Fail(code int, err error) {
c.Error(err, "Operation aborted")
c.Abort(code)
c.AbortWithStatus(code)
}

func (c *Context) ErrorTyped(err error, typ uint32, meta interface{}) {
Expand All @@ -146,9 +156,9 @@ func (c *Context) Error(err error, meta interface{}) {
}

func (c *Context) LastError() error {
s := len(c.Errors)
if s > 0 {
return errors.New(c.Errors[s-1].Err)
nuErrors := len(c.Errors)
if nuErrors > 0 {
return errors.New(c.Errors[nuErrors-1].Err)
} else {
return nil
}
Expand All @@ -170,9 +180,9 @@ func (c *Context) Set(key string, item interface{}) {
// Get returns the value for the given key or an error if the key does not exist.
func (c *Context) Get(key string) (interface{}, error) {
if c.Keys != nil {
item, ok := c.Keys[key]
value, ok := c.Keys[key]
if ok {
return item, nil
return value, nil
}
}
return nil, errors.New("Key does not exist.")
Expand All @@ -182,13 +192,13 @@ func (c *Context) Get(key string) (interface{}, error) {
func (c *Context) MustGet(key string) interface{} {
value, err := c.Get(key)
if err != nil || value == nil {
log.Panicf("Key %s doesn't exist", key)
log.Panicf("Key %s doesn't exist", value)
}
return value
}

/************************************/
/******** ENCOGING MANAGEMENT********/
/********* PARSING REQUEST **********/
/************************************/

// This function checks the Content-Type to select a binding engine automatically,
Expand Down Expand Up @@ -222,10 +232,14 @@ func (c *Context) BindWith(obj interface{}, b binding.Binding) bool {
return true
}

/************************************/
/******** RESPONSE RENDERING ********/
/************************************/

func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
if err := render.Render(c.Writer, code, obj...); err != nil {
c.ErrorTyped(err, ErrorTypeInternal, obj)
c.Abort(500)
c.AbortWithStatus(500)
}
}

Expand Down Expand Up @@ -267,9 +281,7 @@ func (c *Context) Data(code int, contentType string, data []byte) {
if len(contentType) > 0 {
c.Writer.Header().Set("Content-Type", contentType)
}
if code >= 0 {
c.Writer.WriteHeader(code)
}
c.Writer.WriteHeader(code)
c.Writer.Write(data)
}

Expand Down
6 changes: 3 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ func TestBadAbortHandlersChain(t *testing.T) {
c.Next()
stepsPassed += 1
// after check and abort
c.Abort(409)
c.AbortWithStatus(409)
})
r.Use(func(c *Context) {
stepsPassed += 1
c.Next()
stepsPassed += 1
c.Abort(403)
c.AbortWithStatus(403)
})

// RUN
Expand All @@ -260,7 +260,7 @@ func TestAbortHandlersChain(t *testing.T) {
r := New()
r.Use(func(context *Context) {
stepsPassed += 1
context.Abort(409)
context.AbortWithStatus(409)
})
r.Use(func(context *Context) {
stepsPassed += 1
Expand Down
Loading

0 comments on commit 07a3961

Please sign in to comment.