Skip to content

Commit f65871d

Browse files
committed
Readme section for url resolver
1 parent f38f164 commit f65871d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ go get github.com/manyminds/api2go/jsonapi
3838
- [Fetching related IDs](#fetching-related-ids)
3939
- [Fetching related resources](#fetching-related-resources)
4040
- [Using middleware](#using-middleware)
41+
- [Dynamic URL Handling](#dynamic-url-handling)
4142
- [Tests](#tests)
4243

4344
## Examples
@@ -567,6 +568,32 @@ registered with `func (api *API) UseMiddleware(middleware ...HandlerFunc)`. You
567568
that will be executed in order before any other api2go routes. Use this to set up database connections, user authentication
568569
and so on.
569570

571+
### Dynamic URL handling
572+
If you have different TLDs for one api, or want to use different domains in development and production, you can implement a custom
573+
URLResolver in api2go.
574+
575+
There is a simple interface, which can be used if you get TLD information from the database, the server environment, or anything else
576+
that's not request dependant:
577+
```go
578+
type URLResolver interface {
579+
GetBaseURL() string
580+
}
581+
```
582+
And a more complex one that also gets request information:
583+
```go
584+
type RequestAwareURLResolver interface {
585+
URLResolver
586+
SetRequest(http.Request)
587+
}
588+
```
589+
590+
For most use cases we provide a CallbackResolver which works on a per request basis and may fill
591+
your basic needs. This is particulary useful if you are using an nginx proxy which sets `X-Forwarded-For` headers.
592+
593+
```go
594+
resolver := NewCallbackResolver(func(r http.Request) string{})
595+
api := NewApiWithMarshalling("v1", resolver, marshalers)
596+
```
570597
## Tests
571598

572599
```sh

0 commit comments

Comments
 (0)