Per route api extensions (#542)

* Extend extension mechanism to support per-route API extensions

* Tidy up comment

* Remove print statement

* Minor improvement to README

* Avoid calling c.Request.Context() twice
This commit is contained in:
Nigel Deakin
2017-11-29 12:03:23 +00:00
committed by GitHub
parent 4f72a1201f
commit 9a75785cbf
5 changed files with 80 additions and 7 deletions

View File

@@ -9,14 +9,13 @@ go build
./extensions
```
Then test with:
First create an app `myapp` and a function `myroute`. Then test with:
```sh
# First, create an app
fn apps create myapp
# And test
curl http://localhost:8080/v1/custom1
curl http://localhost:8080/v1/custom2
curl http://localhost:8080/v1/apps/myapp/custom3
curl http://localhost:8080/v1/apps/myapp/custom4
curl http://localhost:8080/v1/apps/myapp/routes/myroute/custom5
curl http://localhost:8080/v1/apps/myapp/routes/myroute/custom5
```

View File

@@ -29,6 +29,14 @@ func main() {
fmt.Println("Custom4Handler called")
fmt.Fprintf(w, "Hello app %v func, %q", app.Name, html.EscapeString(r.URL.Path))
})
// the following will be at /v1/apps/:app_name/routes/:route_name/custom5
// and /v1/apps/:app_name/routes/:route_name/custom6
funcServer.AddRouteEndpoint("GET", "/custom5", &Custom5Handler{})
funcServer.AddRouteEndpointFunc("GET", "/custom6", func(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route) {
// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
fmt.Println("Custom6Handler called")
fmt.Fprintf(w, "Hello app %v, route %v, request %q", app.Name, route.Path, html.EscapeString(r.URL.Path))
})
funcServer.Start(ctx)
}
@@ -47,3 +55,11 @@ func (h *Custom3Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, app *
fmt.Println("Custom3Handler called")
fmt.Fprintf(w, "Hello app %v, %q", app.Name, html.EscapeString(r.URL.Path))
}
type Custom5Handler struct {
}
func (h *Custom5Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route) {
fmt.Println("Custom5Handler called")
fmt.Fprintf(w, "Hello! app %v, route %v, request %q", app.Name, route.Path, html.EscapeString(r.URL.Path))
}