mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/iron-io/functions/api"
|
|
"github.com/iron-io/functions/api/models"
|
|
"github.com/iron-io/runner/common"
|
|
)
|
|
|
|
func (s *Server) handleAppDelete(c *gin.Context) {
|
|
ctx := c.MustGet("ctx").(context.Context)
|
|
log := common.Logger(ctx)
|
|
|
|
app := &models.App{Name: c.MustGet(api.AppName).(string)}
|
|
|
|
routes, err := s.Datastore.GetRoutesByApp(ctx, app.Name, &models.RouteFilter{})
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrAppsRemoving)
|
|
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
return
|
|
}
|
|
|
|
if len(routes) > 0 {
|
|
log.WithError(err).Debug(models.ErrDeleteAppsWithRoutes)
|
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrDeleteAppsWithRoutes))
|
|
return
|
|
}
|
|
|
|
err = s.FireBeforeAppDelete(ctx, app)
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrAppsRemoving)
|
|
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
return
|
|
}
|
|
|
|
err = s.Datastore.RemoveApp(ctx, app.Name)
|
|
if err == models.ErrAppsNotFound {
|
|
log.WithError(err).Debug(models.ErrAppsRemoving)
|
|
c.JSON(http.StatusNotFound, simpleError(err))
|
|
return
|
|
} else if err != nil {
|
|
log.WithError(err).Error(models.ErrAppsRemoving)
|
|
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
return
|
|
}
|
|
|
|
err = s.FireAfterAppDelete(ctx, app)
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrAppsRemoving)
|
|
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "App deleted"})
|
|
}
|