Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #78: Support sharing data between handlers within request scope #80

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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