Skip to content

Commit

Permalink
Merge pull request #264 from twitchtv/optional_twirp_prefix
Browse files Browse the repository at this point in the history
Optional twirp prefix
  • Loading branch information
marioizquierdo authored Sep 13, 2020
2 parents 987a500 + ec1e7bb commit 551952a
Show file tree
Hide file tree
Showing 37 changed files with 2,520 additions and 836 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies we know about, build the core, and run all of the tests that we
have against Go and Python code. A few notes:

* Make sure to clone the repo on `$GOPATH/src/github.com/twitchtv/twirp`
* Run Go unit tests with `make test_core`, or just the tests with `go test -race ./...`.
* Run Go unit tests with `make test_core`, or just the tests with `go test -race ./...` (make sure to re-generate code with `make generate` before running unit tests).
* Most tests of the Go server are in `internal/twirptest/service_test.go`.
* Integration tests running the full stack in both Go and Python auto-generated clients are in the [clientcompat](./clientcompat) directory.

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ You will also need:

### Documentation

Thorough documentation is
[on the website](https://twitchtv.github.io/twirp/docs/intro.html).
On the website: https://twitchtv.github.io/twirp/docs/intro.html

Source for this documentation is in the [docs](./docs) subdirectory. The website
is generated from that folder using [Docusaurus](https://docusaurus.io/).
Expand Down
42 changes: 31 additions & 11 deletions client_hooks.go → client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ type ClientOption func(*ClientOptions)

// ClientOptions encapsulate the configurable parameters on a Twirp client.
type ClientOptions struct {
Hooks *ClientHooks
Hooks *ClientHooks
pathPrefix *string
}

func (opts *ClientOptions) PathPrefix() string {
if opts.pathPrefix == nil {
return "/twirp" // default prefix
}
return *opts.pathPrefix
}

// WithClientHooks defines the hooks for a Twirp client.
func WithClientHooks(hooks *ClientHooks) ClientOption {
return func(o *ClientOptions) {
o.Hooks = hooks
}
}

// ClientHooks is a container for callbacks that can instrument a
Expand All @@ -34,13 +49,13 @@ type ClientOptions struct {
// returns non-nil error, handling for that request will be stopped at that
// point. The Error hook will then be triggered.
//
// The RequestPrepaared hook will always be called first and will be called for
// The RequestPrepared hook will always be called first and will be called for
// each outgoing request from the Twirp client. The last hook to be called
// will either be Error or ResponseReceived, so be sure to handle both cases in
// your hooks.
type ClientHooks struct {
// RequestPrepared is called as soon as a request has been created and before it has been sent
// to the Twirp server.
// RequestPrepared is called as soon as a request has been created and before
// it has been sent to the Twirp server.
RequestPrepared func(context.Context, *http.Request) (context.Context, error)

// ResponseReceived is called after a request has finished sending. Since this
Expand All @@ -53,13 +68,6 @@ type ClientHooks struct {
Error func(context.Context, Error)
}

// WithClientHooks defines the hooks for a Twirp client.
func WithClientHooks(hooks *ClientHooks) ClientOption {
return func(o *ClientOptions) {
o.Hooks = hooks
}
}

// ChainClientHooks creates a new *ClientHooks which chains the callbacks in
// each of the constituent hooks passed in. Each hook function will be
// called in the order of the ClientHooks values passed in.
Expand Down Expand Up @@ -102,3 +110,15 @@ func ChainClientHooks(hooks ...*ClientHooks) *ClientHooks {
},
}
}

// WithClientPathPrefix specifies a different prefix to use for routing.
// If not specified, the "/twirp" prefix is used by default.
// The service must be configured to serve on the same prefix.
// An empty value "" can be speficied to use no prefix.
// URL format: "<baseURL>[<prefix>]/<package>.<Service>/<Method>"
// More info on Twirp docs: https://twitchtv.github.io/twirp/docs/routing.html
func WithClientPathPrefix(prefix string) ClientOption {
return func(o *ClientOptions) {
o.pathPrefix = &prefix
}
}
23 changes: 22 additions & 1 deletion client_hooks_test.go → client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,28 @@ func TestChainClientHooks(t *testing.T) {
errorCalled = nil
hook2.Error = nil
chain.Error(ctx, InternalError("whoops"))
if have, want := 0, len(errorCalled); have != want {
if have, want := len(errorCalled), 0; have != want {
t.Errorf("unexpected number of calls, have: %d, want: %d", have, want)
}
}

func TestWithClientPathPrefix(t *testing.T) {
opts := &ClientOptions{}

// Default value
if have, want := opts.PathPrefix(), "/twirp"; have != want {
t.Errorf("unexpected default PathPrefix() on ClientOptions, have: %q, want: %q", have, want)
}

// Set a different prefix
WithClientPathPrefix("/newprfx/foobar")(opts)
if have, want := opts.PathPrefix(), "/newprfx/foobar"; have != want {
t.Errorf("unexpected value after WithClientPathPrefix, have: %q, want: %q", have, want)
}

// Use empty value for no-prefix
WithClientPathPrefix("")(opts)
if have, want := opts.PathPrefix(), ""; have != want {
t.Errorf("unexpected value after WithClientPathPrefix, have: %q, want: %q", have, want)
}
}
136 changes: 102 additions & 34 deletions clientcompat/internal/clientcompat/clientcompat.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 551952a

Please sign in to comment.