Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 1.11 KB

hooks.md

File metadata and controls

29 lines (24 loc) · 1.11 KB
id title sidebar_label
hooks
Server Hooks
Server Hooks

The service constructor can use hooks *twirp.ServerHooks to plug in additional functionality:

func NewHaberdasherServer(svc Haberdasher, hooks *twirp.ServerHooks) http.Handler

These hooks provide a framework for side-effects at important points while a request gets handled. You can do things like log requests, record response times in statsd, authenticate requests, and so on.

To enable hooks for your service, you pass a *twirp.ServerHooks in to the server constructor. The ServerHooks struct holds a pile of callbacks, each of which receives the current context.Context, and can provide a new context.Context (possibly including a new value through the context.WithValue function).

Check out the godoc for ServerHooks for information on the specific callbacks. For an example hooks implementation, github.com/twitchtv/twirp/hooks/statsd is a good tutorial.