Skip to content

Commit

Permalink
Support sharing data between handlers within request scope (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
abahmed authored Feb 16, 2021
1 parent ab37804 commit 35adf9f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
13 changes: 13 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Context interface {
Status(status int) Context
Set(key string, value string)
Get(key string) string
SetLocal(key string, value interface{})
GetLocal(key string) interface{}
Body() string
ParseBody(out interface{}) error
}
Expand Down Expand Up @@ -117,6 +119,17 @@ func (ctx *context) Body() string {
return GetString(ctx.requestCtx.Request.Body())
}

// SetLocal stores value with key within request scope and it is accessible through
// handlers of that request
func (ctx *context) SetLocal(key string, value interface{}) {
ctx.requestCtx.SetUserValue(key, value)
}

// GetLocal gets value by key which are stored by SetLocal within request scope
func (ctx *context) GetLocal(key string) interface{} {
return ctx.requestCtx.UserValue(key)
}

// ParseBody parses request body into provided struct
// Supports decoding theses types: application/json
func (ctx *context) ParseBody(out interface{}) error {
Expand Down
7 changes: 4 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ func TestNext(t *testing.T) {
routes := []struct {
path string
middleware handlerFunc
handler handlerFunc
}{
{path: "/ok", middleware: emptyMiddleware},
{path: "/unauthorized", middleware: unAuthorizedHandler},
{path: "/ok", middleware: emptyMiddleware, handler: emptyMiddlewareHandler},
{path: "/unauthorized", middleware: unAuthorizedHandler, handler: emptyHandler},
}

// get instance of gearbox
gb := setupGearbox()

// register routes according to method
for _, r := range routes {
gb.Get(r.path, r.middleware, emptyHandler)
gb.Get(r.path, r.middleware, r.handler)
}

// start serving
Expand Down
2 changes: 1 addition & 1 deletion gearbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Exported constants
const (
Version = "1.1.1" // Version of gearbox
Version = "1.2.0" // Version of gearbox
Name = "Gearbox" // Name of gearbox
// http://patorjk.com/software/taag/#p=display&f=Big%20Money-ne&t=Gearbox
banner = `
Expand Down
11 changes: 11 additions & 0 deletions gearbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,20 @@ var fallbackHandler = func(ctx Context) {

// emptyMiddleware does not stop the request and passes it to the next middleware/handler
var emptyMiddleware = func(ctx Context) {
// Try to set user data
ctx.SetLocal("test-key", "value")

ctx.Next()
}

// emptyMiddlewareHandler just an empty handler
var emptyMiddlewareHandler = func(ctx Context) {
data, ok := ctx.GetLocal("test-key").(string)
if !ok || data != "value" {
panic("test-key value is wrong")
}
}

// registerRoute matches with register route request with available methods and calls it
func registerRoute(gb Gearbox, method, path string, handler func(ctx Context)) {
switch method {
Expand Down

0 comments on commit 35adf9f

Please sign in to comment.