mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
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:
@@ -59,6 +59,54 @@ func (s *Server) apiAppHandlerWrapperFunc(apiHandler ApiAppHandler) gin.HandlerF
|
||||
}
|
||||
}
|
||||
|
||||
// per Route
|
||||
|
||||
type ApiRouteHandler interface {
|
||||
// Handle(ctx context.Context)
|
||||
ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route)
|
||||
}
|
||||
|
||||
type ApiRouteHandlerFunc func(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route)
|
||||
|
||||
// ServeHTTP calls f(w, r).
|
||||
func (f ApiRouteHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route) {
|
||||
f(w, r, app, route)
|
||||
}
|
||||
|
||||
func (s *Server) apiRouteHandlerWrapperFunc(apiHandler ApiRouteHandler) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
context := c.Request.Context()
|
||||
// get the app
|
||||
appName := c.Param(api.CApp)
|
||||
app, err := s.Datastore.GetApp(context, appName)
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if app == nil {
|
||||
handleErrorResponse(c, models.ErrAppsNotFound)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
// get the route TODO
|
||||
routePath := "/" + c.Param(api.CRoute)
|
||||
route, err := s.Datastore.GetRoute(context, appName, routePath)
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if route == nil {
|
||||
handleErrorResponse(c, models.ErrRoutesNotFound)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
apiHandler.ServeHTTP(c.Writer, c.Request, app, route)
|
||||
}
|
||||
}
|
||||
|
||||
// AddEndpoint adds an endpoint to /v1/x
|
||||
func (s *Server) AddEndpoint(method, path string, handler ApiHandler) {
|
||||
v1 := s.Router.Group("/v1")
|
||||
@@ -81,3 +129,14 @@ func (s *Server) AddAppEndpoint(method, path string, handler ApiAppHandler) {
|
||||
func (s *Server) AddAppEndpointFunc(method, path string, handler func(w http.ResponseWriter, r *http.Request, app *models.App)) {
|
||||
s.AddAppEndpoint(method, path, ApiAppHandlerFunc(handler))
|
||||
}
|
||||
|
||||
// AddRouteEndpoint adds an endpoints to /v1/apps/:app/routes/:route/x
|
||||
func (s *Server) AddRouteEndpoint(method, path string, handler ApiRouteHandler) {
|
||||
v1 := s.Router.Group("/v1")
|
||||
v1.Handle(method, "/apps/:app/routes/:route"+path, s.apiRouteHandlerWrapperFunc(handler)) // conflicts with existing wildcard
|
||||
}
|
||||
|
||||
// AddRouteEndpoint adds an endpoints to /v1/apps/:app/routes/:route/x
|
||||
func (s *Server) AddRouteEndpointFunc(method, path string, handler func(w http.ResponseWriter, r *http.Request, app *models.App, route *models.Route)) {
|
||||
s.AddRouteEndpoint(method, path, ApiRouteHandlerFunc(handler))
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@ func (s *Server) handleRouteGet(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
appName := c.MustGet(api.AppName).(string)
|
||||
routePath := path.Clean(c.MustGet(api.Path).(string))
|
||||
|
||||
routePath := path.Clean("/" + c.MustGet(api.Path).(string))
|
||||
route, err := s.Datastore.GetRoute(ctx, appName, routePath)
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
|
||||
@@ -363,7 +363,7 @@ func (s *Server) bindHandlers(ctx context.Context) {
|
||||
|
||||
apps.GET("/routes", s.handleRouteList)
|
||||
apps.POST("/routes", s.handleRoutesPostPutPatch)
|
||||
apps.GET("/routes/*route", s.handleRouteGet)
|
||||
apps.GET("/routes/:route", s.handleRouteGet)
|
||||
apps.PATCH("/routes/*route", s.handleRoutesPostPutPatch)
|
||||
apps.PUT("/routes/*route", s.handleRoutesPostPutPatch)
|
||||
apps.DELETE("/routes/*route", s.handleRouteDelete)
|
||||
|
||||
Reference in New Issue
Block a user