Middleware (#502)

* API endpoint extensions working.

extensions example.

extensions example.

* Added server.NewEnv and some docs for the API extensions example.

extensions example.

extensions example.

* Uncommented special handler stuff.

* First example of middleware.

easier to use.

* Added a special Middleware context to make middleware easier to use.

* Fix tests.

* Cleanup based on PR comments.
This commit is contained in:
Travis Reeder
2017-01-30 14:43:23 -08:00
committed by C Cirello
parent 37efa47bdf
commit ce26f665ea
10 changed files with 213 additions and 48 deletions

View File

@@ -2,6 +2,13 @@
IronFunctions is extensible so you can add custom functionality and extend the project without needing to modify the core.
There are 4 different ways to extend the functionality of IronFunctions.
1. Listeners - listen to API events such as a route getting updated and react accordingly.
1. Middleware - a chain of middleware is executed before an API handler is called.
1. Add API Endpoints - extend the default IronFunctions API.
1. Special Handlers - TODO: DO WE NEED THIS ANYMORE??
## Listeners
Listeners are the main way to extend IronFunctions.
@@ -34,10 +41,8 @@ func (c *myCustomListener) BeforeAppDelete(ctx context.Context, app *models.App)
function main () {
srv := server.New(/* Here all required parameters to initialize the server */)
srv.AddAppCreateListener(myCustomListener)
srv.AddAppUpdateListener(myCustomListener)
srv.AddAppDeleteListener(myCustomListener)
srv.AddAppListener(myCustomListener)
srv.Run()
}
```
@@ -48,45 +53,11 @@ These are all available listeners:
#### App Listeners
To be a valid listener your struct should respect interfaces combined or alone found [in this file](/iron-io/functions/blob/master/api/server/apps_listeners.go)
##### AppCreateListener
_Triggers before and after every app creation that happens in the API_
Triggered on requests to the following routes:
- POST /v1/apps
- POST /v1/apps/:app/routes
##### AppUpdateListener
_Triggers before and after every app updates that happens in the API_
Triggered during requests to the following routes:
- PUT /v1/apps
##### AppDeleteListener
_Triggers before and after every app deletion that happens in the API_
Triggered during requests to the following routes:
- DELETE /v1/apps/:app
See the godoc for AppListener [in this file](/iron-io/functions/blob/master/api/server/apps_listeners.go)
#### Runner Listeners
To be a valid listener your struct should respect interfaces combined or alone found [in this file](/iron-io/functions/blob/master/api/server/runner_listeners.go).
##### RunnerListener
_Triggers before and after every function run_
Triggered during requests to the following routes:
- GET /r/:app/:route
- POST /r/:app/:route
See the godoc for RunnerListner [in this file](/iron-io/functions/blob/master/api/server/runner_listeners.go).
## Adding API Endpoints
@@ -94,6 +65,16 @@ You can add API endpoints by using the `AddEndpoint` and `AddEndpointFunc` metho
See examples of this in [/examples/extensions/main.go](/examples/extensions/main.go).
## Middleware
Middleware enables you to add functionality to every API request. For every request, the chain of Middleware will be called
in order allowing you to modify or reject requests, as well as write output and cancel the chain.
NOTES:
* middleware is responsible for writing output if it's going to cancel the chain.
* cancel the chain by returning an error from your Middleware's Serve method.
## Special Handlers
To understand how **Special Handlers** works you need to understand what are **Special Routes**.