mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Improve listeners and handlers (#350)
* improve listeners and handlers * add to route handlers * separate create/delete/update events * removed useless interface
This commit is contained in:
@@ -3,6 +3,7 @@ package ifaces
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"context"
|
||||||
"github.com/iron-io/functions/api/models"
|
"github.com/iron-io/functions/api/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +14,9 @@ type SpecialHandler interface {
|
|||||||
// Each handler can modify the context here so when it gets passed along, it will use the new info.
|
// Each handler can modify the context here so when it gets passed along, it will use the new info.
|
||||||
// Not using Gin's Context so we don't lock ourselves into Gin, this is a subset of the Gin context.
|
// Not using Gin's Context so we don't lock ourselves into Gin, this is a subset of the Gin context.
|
||||||
type HandlerContext interface {
|
type HandlerContext interface {
|
||||||
|
// Context return the context object
|
||||||
|
Context() context.Context
|
||||||
|
|
||||||
// Request returns the underlying http.Request object
|
// Request returns the underlying http.Request object
|
||||||
Request() *http.Request
|
Request() *http.Request
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,23 @@ import (
|
|||||||
"github.com/iron-io/functions/api/models"
|
"github.com/iron-io/functions/api/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AppListener interface {
|
type AppCreateListener interface {
|
||||||
// BeforeAppUpdate called right before storing App in the database
|
// BeforeAppCreate called right before creating App in the database
|
||||||
|
BeforeAppCreate(ctx context.Context, app *models.App) error
|
||||||
|
// AfterAppCreate called after creating App in the database
|
||||||
|
AfterAppCreate(ctx context.Context, app *models.App) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type AppUpdateListener interface {
|
||||||
|
// BeforeAppUpdate called right before updating App in the database
|
||||||
BeforeAppUpdate(ctx context.Context, app *models.App) error
|
BeforeAppUpdate(ctx context.Context, app *models.App) error
|
||||||
// AfterAppUpdate called after storing App in the database
|
// AfterAppUpdate called after updating App in the database
|
||||||
AfterAppUpdate(ctx context.Context, app *models.App) error
|
AfterAppUpdate(ctx context.Context, app *models.App) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AppDeleteListener interface {
|
||||||
|
// BeforeAppDelete called right before deleting App in the database
|
||||||
|
BeforeAppDelete(ctx context.Context, appName string) error
|
||||||
|
// AfterAppDelete called after deleting App in the database
|
||||||
|
AfterAppDelete(ctx context.Context, appName string) error
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func (s *Server) handleAppCreate(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Api.FireBeforeAppUpdate(ctx, wapp.App)
|
err = Api.FireBeforeAppCreate(ctx, wapp.App)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorln(models.ErrAppsCreate)
|
log.WithError(err).Errorln(models.ErrAppsCreate)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
@@ -48,7 +48,7 @@ func (s *Server) handleAppCreate(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Api.FireAfterAppUpdate(ctx, wapp.App)
|
err = Api.FireAfterAppCreate(ctx, wapp.App)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorln(models.ErrAppsCreate)
|
log.WithError(err).Errorln(models.ErrAppsCreate)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
|
|||||||
@@ -28,11 +28,25 @@ func handleAppDelete(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Api.Datastore.RemoveApp(ctx, appName); err != nil {
|
err = Api.FireAfterAppDelete(ctx, appName)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Errorln(models.ErrAppsRemoving)
|
||||||
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = Api.Datastore.RemoveApp(ctx, appName); err != nil {
|
||||||
log.WithError(err).Debug(models.ErrAppsRemoving)
|
log.WithError(err).Debug(models.ErrAppsRemoving)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsRemoving))
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsRemoving))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = Api.FireAfterAppDelete(ctx, appName)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Errorln(models.ErrAppsRemoving)
|
||||||
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "App deleted"})
|
c.JSON(http.StatusOK, gin.H{"message": "App deleted"})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,14 @@ func handleAppUpdate(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wapp.App.Name = c.Param("app")
|
wapp.App.Name = c.Param("app")
|
||||||
|
|
||||||
|
err = Api.FireAfterAppUpdate(ctx, wapp.App)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Errorln(models.ErrAppsUpdate)
|
||||||
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
app, err := Api.Datastore.UpdateApp(ctx, wapp.App)
|
app, err := Api.Datastore.UpdateApp(ctx, wapp.App)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrAppsUpdate)
|
log.WithError(err).Debug(models.ErrAppsUpdate)
|
||||||
@@ -36,6 +44,13 @@ func handleAppUpdate(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = Api.FireAfterAppUpdate(ctx, wapp.App)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Errorln(models.ErrAppsUpdate)
|
||||||
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
wapp.App = app
|
wapp.App = app
|
||||||
|
|
||||||
// Nothing to update right now in apps
|
// Nothing to update right now in apps
|
||||||
|
|||||||
82
api/server/listeners.go
Normal file
82
api/server/listeners.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/iron-io/functions/api/ifaces"
|
||||||
|
"github.com/iron-io/functions/api/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddAppCreateListener adds a listener that will be notified on App created.
|
||||||
|
func (s *Server) AddAppCreateListener(listener ifaces.AppCreateListener) {
|
||||||
|
s.AppCreateListeners = append(s.AppCreateListeners, listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddAppUpdateListener adds a listener that will be notified on App updated.
|
||||||
|
func (s *Server) AddAppUpdateListener(listener ifaces.AppUpdateListener) {
|
||||||
|
s.AppUpdateListeners = append(s.AppUpdateListeners, listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddAppDeleteListener adds a listener that will be notified on App deleted.
|
||||||
|
func (s *Server) AddAppDeleteListener(listener ifaces.AppDeleteListener) {
|
||||||
|
s.AppDeleteListeners = append(s.AppDeleteListeners, listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FireBeforeAppCreate(ctx context.Context, app *models.App) error {
|
||||||
|
for _, l := range s.AppCreateListeners {
|
||||||
|
err := l.BeforeAppCreate(ctx, app)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FireAfterAppCreate(ctx context.Context, app *models.App) error {
|
||||||
|
for _, l := range s.AppCreateListeners {
|
||||||
|
err := l.AfterAppCreate(ctx, app)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FireBeforeAppUpdate(ctx context.Context, app *models.App) error {
|
||||||
|
for _, l := range s.AppUpdateListeners {
|
||||||
|
err := l.BeforeAppUpdate(ctx, app)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FireAfterAppUpdate(ctx context.Context, app *models.App) error {
|
||||||
|
for _, l := range s.AppUpdateListeners {
|
||||||
|
err := l.AfterAppUpdate(ctx, app)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FireBeforeAppDelete(ctx context.Context, appName string) error {
|
||||||
|
for _, l := range s.AppDeleteListeners {
|
||||||
|
err := l.BeforeAppDelete(ctx, appName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) FireAfterAppDelete(ctx context.Context, appName string) error {
|
||||||
|
for _, l := range s.AppDeleteListeners {
|
||||||
|
err := l.AfterAppDelete(ctx, appName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -25,12 +25,14 @@ import (
|
|||||||
var Api *Server
|
var Api *Server
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Runner *runner.Runner
|
Runner *runner.Runner
|
||||||
Router *gin.Engine
|
Router *gin.Engine
|
||||||
MQ models.MessageQueue
|
MQ models.MessageQueue
|
||||||
AppListeners []ifaces.AppListener
|
AppCreateListeners []ifaces.AppCreateListener
|
||||||
SpecialHandlers []ifaces.SpecialHandler
|
AppUpdateListeners []ifaces.AppUpdateListener
|
||||||
Enqueue models.Enqueue
|
AppDeleteListeners []ifaces.AppDeleteListener
|
||||||
|
SpecialHandlers []ifaces.SpecialHandler
|
||||||
|
Enqueue models.Enqueue
|
||||||
|
|
||||||
tasks chan task.Request
|
tasks chan task.Request
|
||||||
|
|
||||||
@@ -94,31 +96,6 @@ func (s *Server) primeCache(ctx context.Context) {
|
|||||||
logrus.Info("cached prime")
|
logrus.Info("cached prime")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAppListener adds a listener that will be notified on App changes.
|
|
||||||
func (s *Server) AddAppListener(listener ifaces.AppListener) {
|
|
||||||
s.AppListeners = append(s.AppListeners, listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) FireBeforeAppUpdate(ctx context.Context, app *models.App) error {
|
|
||||||
for _, l := range s.AppListeners {
|
|
||||||
err := l.BeforeAppUpdate(ctx, app)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) FireAfterAppUpdate(ctx context.Context, app *models.App) error {
|
|
||||||
for _, l := range s.AppListeners {
|
|
||||||
err := l.AfterAppUpdate(ctx, app)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) AddSpecialHandler(handler ifaces.SpecialHandler) {
|
func (s *Server) AddSpecialHandler(handler ifaces.SpecialHandler) {
|
||||||
s.SpecialHandlers = append(s.SpecialHandlers, handler)
|
s.SpecialHandlers = append(s.SpecialHandlers, handler)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package server
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"context"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/iron-io/functions/api/models"
|
"github.com/iron-io/functions/api/models"
|
||||||
)
|
)
|
||||||
@@ -12,6 +13,11 @@ type SpecialHandlerContext struct {
|
|||||||
ginContext *gin.Context
|
ginContext *gin.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *SpecialHandlerContext) Context() context.Context {
|
||||||
|
ctx, _ := c.ginContext.Get("ctx")
|
||||||
|
return ctx.(context.Context)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *SpecialHandlerContext) Request() *http.Request {
|
func (c *SpecialHandlerContext) Request() *http.Request {
|
||||||
return c.ginContext.Request
|
return c.ginContext.Request
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user