mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Adds before/after app get/list. And some bug fixes/cleanup. (#610)
* Adds before/after app get/list. And some bug fixes/cleanup. * Fix test
This commit is contained in:
17
fnext/context.go
Normal file
17
fnext/context.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package fnext
|
||||
|
||||
// good reading on this: https://twitter.com/sajma/status/757217773852487680
|
||||
type contextKey string
|
||||
|
||||
// func (c contextKey) String() string {
|
||||
// return "fnext context key " + string(c)
|
||||
// }
|
||||
|
||||
// Keys for extensions to get things out of the context
|
||||
var (
|
||||
// MiddlewareControllerKey is a context key. It can be used in handlers with context.WithValue to
|
||||
// access the MiddlewareContext.
|
||||
MiddlewareControllerKey = contextKey("middleware_controller")
|
||||
// AppNameKey
|
||||
AppNameKey = contextKey("app_name")
|
||||
)
|
||||
@@ -20,6 +20,25 @@ type AppListener interface {
|
||||
BeforeAppDelete(ctx context.Context, app *models.App) error
|
||||
// AfterAppDelete called after deleting App in the database
|
||||
AfterAppDelete(ctx context.Context, app *models.App) error
|
||||
// BeforeAppGet called right before getting an app
|
||||
BeforeAppGet(ctx context.Context, appName string) error
|
||||
// AfterAppGet called after getting app from database
|
||||
AfterAppGet(ctx context.Context, app *models.App) error
|
||||
// BeforeAppsList called right before getting a list of all user's apps. Modify the filter to adjust what gets returned.
|
||||
BeforeAppsList(ctx context.Context, filter *models.AppFilter) error
|
||||
// AfterAppsList called after deleting getting a list of user's apps. apps is the result after applying AppFilter.
|
||||
AfterAppsList(ctx context.Context, apps []*models.App) error
|
||||
|
||||
// TODO: WHAT IF THESE WERE CHANGE TO WRAPPERS INSTEAD OF LISTENERS, SORT OF LIKE MIDDLEWARE, EG
|
||||
// AppCreate(ctx, app, next func(ctx, app) or next.AppCreate(ctx, app)) <- where func is the InsertApp function (ie: the corresponding Datastore function)
|
||||
// Then instead of two two functions and modifying objects in the params, they get modified and then passed on. Eg:
|
||||
// AppCreate(ctx, app, next) (app *models.App, err error) {
|
||||
// // do stuff before
|
||||
// app.Name = app.Name + "-12345"
|
||||
// app, err = next.AppCreate(ctx, app)
|
||||
// // do stuff after if you want
|
||||
// return app, err
|
||||
// }
|
||||
}
|
||||
|
||||
// CallListener enables callbacks around Call events
|
||||
|
||||
@@ -5,12 +5,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
// MiddlewareControllerKey is a context key. It can be used in handlers with context.WithValue to
|
||||
// access the MiddlewareContext.
|
||||
MiddlewareControllerKey = contextKey("middleware-controller")
|
||||
)
|
||||
|
||||
// MiddlewareController allows a bit more flow control to the middleware, since we multiple paths a request can go down.
|
||||
// 1) Could be routed towards the API
|
||||
// 2) Could be routed towards a function
|
||||
@@ -43,10 +37,3 @@ type MiddlewareFunc func(next http.Handler) http.Handler
|
||||
func (m MiddlewareFunc) Handle(next http.Handler) http.Handler {
|
||||
return m(next)
|
||||
}
|
||||
|
||||
// good reading on this: https://twitter.com/sajma/status/757217773852487680
|
||||
type contextKey string
|
||||
|
||||
// func (c contextKey) String() string {
|
||||
// return "fnext context key " + string(c)
|
||||
// }
|
||||
|
||||
@@ -37,4 +37,7 @@ type ExtServer interface {
|
||||
AddRouteEndpoint(method, path string, handler ApiRouteHandler)
|
||||
// AddRouteEndpoint adds an endpoints to /v1/apps/:app/routes/:route/x
|
||||
AddRouteEndpointFunc(method, path string, handler func(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route))
|
||||
|
||||
// Datastore returns the Datastore Fn is using
|
||||
Datastore() models.Datastore
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user