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

add custom context support #1355

Closed
wants to merge 2 commits into from
Closed

add custom context support #1355

wants to merge 2 commits into from

Conversation

mei-rune
Copy link

@mei-rune mei-rune commented Jun 24, 2019

add custom context support

Originally posted by @runner-mei in #1302 (comment)

@StevenACoffman
Copy link

Not sure this is the same, but my use case is that I would like to combine echo and gqlgen. There is a gqlgen gin recipe where you create a gin middleware to add the gin.Context to the context.Context.
Ref: https://gqlgen.com/recipes/gin/#accessing-gin-context

@mei-rune
Copy link
Author

mei-rune commented Jul 3, 2019

@StevenACoffman yes, similar.

@StevenACoffman
Copy link

Original comment quoted below:

I like it Context as interface, but need improve now code.

add methods:

(e *Echo) SetNewContext(newCtx  func() Context) {
 e.pool.New = func() interface{} {
		return newCtx()
	}
}

func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := e.pool.Get().(Context)
	c.Reset(r, w)
	......
	c.Free()
	e.pool.Put(c)
}

my client code:

type MyContext strcut {
	echo.Context
	
	MyField1 xxx
	MyField2 xxx
	MyField3 xxx
	.... xxx
}

func (ctx *MyContext) Free() {
   ctx.MyField1 = nil
   ctx.MyField2 = nil
   ctx.MyField3 = nil
   ....
}

func toEchoFunc(h func(ctx *MyContext) error) echo.HandlerFunc {
	return func(ctx echo.Context) error {
		return h(ctx.(*MyContext))
	}
}

echo.SetNew(func() echo.Context {
	return &MyContext{Context: echo.NewContext()}
})

echo.GET("/xxx", toEchoFunc(func(ctx *MyContext) error {
   xxxxxxx
}))

@stale
Copy link

stale bot commented Oct 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Oct 4, 2019
@stale stale bot closed this Oct 14, 2019
@StevenACoffman
Copy link

By the way, I got things working here: https://github.com/StevenACoffman/echogqlgen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants