-
-
Notifications
You must be signed in to change notification settings - Fork 984
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
Ability to set the base context #90
Comments
@cyx yea I've gone back and forth with this one myself. I've implemented it, then removed it, then thought perhaps we should bring it back. can you describe your use case for a base context? what would you use it for? |
I was thinking about custom context types: ctx := &MyCustomAppContext{} // implements context.Context interface
router := chi.NewRouter(ctx) func Handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context().(*MyCustomAppContext)
// I can use custom methods/helpers defined on the *MyCustomAppContext type now..
// this means... saving a lot of boiler plate and type conversions
sessionUser := ctx.SessionUser()
session := ctx.Session() // instead of session, ok := ctx.Value("session").(*Session)
db := ctx.DBSession() // DB connection assigned from a middleware?
w.Write([]byte("yay"))
} |
For our usecase, the most important part is cancellation. ctx, cancel := context.WithCancel(context.Background())
graceful.AddSignal(syscall.SIGTERM)
graceful.PostHook(cancel)
// more code here that adds on to context. FWIW we were originally using github.com/guregu/kami. We definitely love a working PR we have moving to chi, and this is the only piece that felt weird to me TBH. |
@cyx let me know if you get any success or some other ideas on how to approach it though |
@pkieltyka do you sit in any irc / slack channel? Would be interested to talk about this in detail. |
@cyx id love to get into it, but I'm under a pile of work onboarding a bunch of customers this week |
btw, here is a PR to add this functionality #92 I'll be adding a _examples/graceful example too to this PR that uses the new https://github.com/goware/valve package to manage graceful shutdowns, utilizing a base context |
done in a526d0c |
I'm not sure there's a facility for this already, but after searching through the code and docs, there doesn't seem to be a way to do it without making a middleware.
Basically the usecase is like:
In any case the underlying goal is to just easily set the base context. Let me know if this makes sense.
The text was updated successfully, but these errors were encountered: