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

Optional twirp prefix #264

Merged
merged 19 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b5095f7
docs website cleanup .gitignore and add package-lock.json in case the…
marioizquierdo Aug 24, 2020
a646a49
Client option WithClientPathPrefix allows to specify a different path…
marioizquierdo Aug 24, 2020
85aca69
Update client option to twirp.SkipPathPrefix(), which fits the curren…
marioizquierdo Aug 26, 2020
cf5e1bd
Generate code and test cients with the new option (using the default …
marioizquierdo Aug 26, 2020
afc0c81
review comments
marioizquierdo Aug 26, 2020
5d9d1c9
Route requests by path suffix, which allows to mount services on arbi…
marioizquierdo Aug 27, 2020
f8cb685
Add tests for services and clients using different path prefixes
marioizquierdo Aug 27, 2020
fc1a41a
Client option twirp.WithClientPathPrefix(prefix), where /twirp is the…
marioizquierdo Sep 2, 2020
78fc2b1
Add server options when initializing with the constructor. Add twirp.…
marioizquierdo Sep 2, 2020
f35701c
server.PathPrefix() returns the service base path with the specified …
marioizquierdo Sep 3, 2020
a1b7fe5
Add a few more tests to verify the server.PathPrefix() keeps the same…
marioizquierdo Sep 3, 2020
0cbc965
Review instructions to use cURL
marioizquierdo Sep 3, 2020
8a90385
Review docs, update routing and spec to specify a flexible path prefix
marioizquierdo Sep 3, 2020
89eddb7
Make the documentation link in the README easier to find
marioizquierdo Sep 3, 2020
8c5b366
Generate docs
marioizquierdo Sep 3, 2020
6541a0c
Review and updates on docs/routing.md
marioizquierdo Sep 3, 2020
7484d9c
More edits to simplify routing docs
marioizquierdo Sep 3, 2020
dc6772d
Routing changes are part of protocol v7
marioizquierdo Sep 13, 2020
ec1e7bb
keep v5 as current and v7 as Draft until v7 is officially released
marioizquierdo Sep 13, 2020
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
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