mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
added wrapper on models; changed handlers; fixes
This commit is contained in:
@@ -5,13 +5,14 @@ import "errors"
|
|||||||
type Apps []*App
|
type Apps []*App
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrAppsCreate = errors.New("Could not create app")
|
ErrAppsCreate = errors.New("Could not create app")
|
||||||
ErrAppsUpdate = errors.New("Could not update app")
|
ErrAppsUpdate = errors.New("Could not update app")
|
||||||
ErrAppsRemoving = errors.New("Could not remove app from datastore")
|
ErrAppsRemoving = errors.New("Could not remove app from datastore")
|
||||||
ErrAppsGet = errors.New("Could not get app from datastore")
|
ErrAppsGet = errors.New("Could not get app from datastore")
|
||||||
ErrAppsList = errors.New("Could not list apps from datastore")
|
ErrAppsList = errors.New("Could not list apps from datastore")
|
||||||
ErrAppsNotFound = errors.New("App not found")
|
ErrAppsNotFound = errors.New("App not found")
|
||||||
ErrAppNothingToUpdate = errors.New("Nothing to update")
|
ErrAppsNothingToUpdate = errors.New("Nothing to update")
|
||||||
|
ErrAppsMissingNew = errors.New("Missing new application")
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
|||||||
32
api/models/app_wrapper.go
Normal file
32
api/models/app_wrapper.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/go-openapi/errors"
|
||||||
|
|
||||||
|
type AppWrapper struct {
|
||||||
|
App *App `json:"app"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AppWrapper) Validate() error {
|
||||||
|
var res []error
|
||||||
|
|
||||||
|
if err := m.validateApp(); err != nil {
|
||||||
|
// prop
|
||||||
|
res = append(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(res) > 0 {
|
||||||
|
return errors.CompositeValidationError(res...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AppWrapper) validateApp() error {
|
||||||
|
|
||||||
|
if m.App != nil {
|
||||||
|
if err := m.App.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
34
api/models/apps_wrapper.go
Normal file
34
api/models/apps_wrapper.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
strfmt "github.com/go-openapi/strfmt"
|
||||||
|
"github.com/go-openapi/validate"
|
||||||
|
|
||||||
|
"github.com/go-openapi/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AppsWrapper struct {
|
||||||
|
Apps []*App `json:"apps"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AppsWrapper) Validate(formats strfmt.Registry) error {
|
||||||
|
var res []error
|
||||||
|
|
||||||
|
if err := m.validateApps(formats); err != nil {
|
||||||
|
res = append(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(res) > 0 {
|
||||||
|
return errors.CompositeValidationError(res...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AppsWrapper) validateApps(formats strfmt.Registry) error {
|
||||||
|
|
||||||
|
if err := validate.Required("apps", "body", m.Apps); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -17,6 +17,6 @@ func ApplyAppFilter(app *App, filter *AppFilter) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ApplyRouteFilter(route *Route, filter *RouteFilter) bool {
|
func ApplyRouteFilter(route *Route, filter *RouteFilter) bool {
|
||||||
return (filter.Path != "" && route.Path == filter.Path) &&
|
return (filter.Path == "" || route.Path == filter.Path) &&
|
||||||
(filter.AppName != "" && route.AppName == filter.AppName)
|
(filter.AppName == "" || route.AppName == filter.AppName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,5 +11,5 @@ func (m *Error) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrInvalidJSON = errors.New("Could not create app")
|
ErrInvalidJSON = errors.New("Invalid JSON")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,27 +3,28 @@ package models
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
apiErrors "github.com/go-openapi/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrRoutesCreate = errors.New("Could not create route")
|
ErrRoutesCreate = errors.New("Could not create route")
|
||||||
ErrRoutesUpdate = errors.New("Could not update route")
|
ErrRoutesUpdate = errors.New("Could not update route")
|
||||||
ErrRoutesRemoving = errors.New("Could not remove route from datastore")
|
ErrRoutesRemoving = errors.New("Could not remove route from datastore")
|
||||||
ErrRoutesGet = errors.New("Could not get route from datastore")
|
ErrRoutesGet = errors.New("Could not get route from datastore")
|
||||||
ErrRoutesList = errors.New("Could not list routes from datastore")
|
ErrRoutesList = errors.New("Could not list routes from datastore")
|
||||||
ErrRoutesNotFound = errors.New("Route not found")
|
ErrRoutesNotFound = errors.New("Route not found")
|
||||||
|
ErrRoutesMissingNew = errors.New("Missing new route")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Routes []*Route
|
type Routes []*Route
|
||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
AppName string `json:"appname"`
|
AppName string `json:"appname"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
Type string `json:"type,omitempty"`
|
Headers http.Header `json:"headers,omitempty"`
|
||||||
ContainerPath string `json:"container_path,omitempty"`
|
|
||||||
Headers http.Header `json:"headers,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -34,20 +35,26 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (r *Route) Validate() error {
|
func (r *Route) Validate() error {
|
||||||
|
var res []error
|
||||||
|
|
||||||
if r.Name == "" {
|
if r.Name == "" {
|
||||||
return ErrRoutesValidationName
|
res = append(res, ErrRoutesValidationAppName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Image == "" {
|
if r.Image == "" {
|
||||||
return ErrRoutesValidationImage
|
res = append(res, ErrRoutesValidationImage)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.AppName == "" {
|
if r.AppName == "" {
|
||||||
return ErrRoutesValidationAppName
|
res = append(res, ErrRoutesValidationAppName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Path == "" {
|
if r.Path == "" {
|
||||||
return ErrRoutesValidationPath
|
res = append(res, ErrRoutesValidationPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(res) > 0 {
|
||||||
|
return apiErrors.CompositeValidationError(res...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
31
api/models/route_wrapper.go
Normal file
31
api/models/route_wrapper.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/go-openapi/errors"
|
||||||
|
|
||||||
|
type RouteWrapper struct {
|
||||||
|
Route *Route `json:"route"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *RouteWrapper) Validate() error {
|
||||||
|
var res []error
|
||||||
|
|
||||||
|
if err := m.validateRoute(); err != nil {
|
||||||
|
res = append(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(res) > 0 {
|
||||||
|
return errors.CompositeValidationError(res...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *RouteWrapper) validateRoute() error {
|
||||||
|
|
||||||
|
if m.Route != nil {
|
||||||
|
if err := m.Route.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
36
api/models/routes_wrapper.go
Normal file
36
api/models/routes_wrapper.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
strfmt "github.com/go-openapi/strfmt"
|
||||||
|
"github.com/go-openapi/validate"
|
||||||
|
|
||||||
|
"github.com/go-openapi/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RoutesWrapper struct {
|
||||||
|
Cursor string `json:"cursor,omitempty"`
|
||||||
|
Error *ErrorBody `json:"error,omitempty"`
|
||||||
|
Routes []*Route `json:"routes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *RoutesWrapper) Validate(formats strfmt.Registry) error {
|
||||||
|
var res []error
|
||||||
|
|
||||||
|
if err := m.validateRoutes(formats); err != nil {
|
||||||
|
res = append(res, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(res) > 0 {
|
||||||
|
return errors.CompositeValidationError(res...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *RoutesWrapper) validateRoutes(formats strfmt.Registry) error {
|
||||||
|
|
||||||
|
if err := validate.Required("routes", "body", m.Routes); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -12,22 +12,28 @@ func handleAppCreate(c *gin.Context) {
|
|||||||
store := c.MustGet("store").(models.Datastore)
|
store := c.MustGet("store").(models.Datastore)
|
||||||
log := c.MustGet("log").(logrus.FieldLogger)
|
log := c.MustGet("log").(logrus.FieldLogger)
|
||||||
|
|
||||||
app := &models.App{}
|
wapp := &models.AppWrapper{}
|
||||||
|
|
||||||
err := c.BindJSON(app)
|
err := c.BindJSON(wapp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.Validate(); err != nil {
|
if wapp.App == nil {
|
||||||
|
log.Debug(models.ErrAppsMissingNew)
|
||||||
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsMissingNew))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := wapp.Validate(); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
app, err = store.StoreApp(app)
|
app, err := store.StoreApp(wapp.App)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrAppsCreate)
|
log.WithError(err).Debug(models.ErrAppsCreate)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||||
|
|||||||
@@ -40,5 +40,5 @@ func handleAppGet(c *gin.Context) {
|
|||||||
|
|
||||||
app.Routes = routes
|
app.Routes = routes
|
||||||
|
|
||||||
c.JSON(http.StatusOK, app)
|
c.JSON(http.StatusOK, &models.AppWrapper{app})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ func handleAppList(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, apps)
|
c.JSON(http.StatusOK, &models.AppsWrapper{apps})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,14 +21,5 @@ func handleAppUpdate(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// app.Name = c.Param("app")
|
c.JSON(http.StatusOK, simpleError(models.ErrAppsNothingToUpdate))
|
||||||
|
|
||||||
// app, err = store.StoreApp(app)
|
|
||||||
// if err != nil {
|
|
||||||
// log.WithError(err).Debug(models.ErrAppsUpdate)
|
|
||||||
// c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsUpdate))
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, simpleError(models.ErrAppNothingToUpdate))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func Start(engine *gin.Engine) {
|
|||||||
v1.POST("/apps", handleAppCreate)
|
v1.POST("/apps", handleAppCreate)
|
||||||
|
|
||||||
v1.GET("/apps/:app", handleAppGet)
|
v1.GET("/apps/:app", handleAppGet)
|
||||||
v1.POST("/apps/:app", handleAppUpdate)
|
v1.PUT("/apps/:app", handleAppUpdate)
|
||||||
v1.DELETE("/apps/:app", handleAppDestroy)
|
v1.DELETE("/apps/:app", handleAppDestroy)
|
||||||
|
|
||||||
apps := v1.Group("/apps/:app")
|
apps := v1.Group("/apps/:app")
|
||||||
|
|||||||
@@ -12,31 +12,37 @@ func handleRouteCreate(c *gin.Context) {
|
|||||||
store := c.MustGet("store").(models.Datastore)
|
store := c.MustGet("store").(models.Datastore)
|
||||||
log := c.MustGet("log").(logrus.FieldLogger)
|
log := c.MustGet("log").(logrus.FieldLogger)
|
||||||
|
|
||||||
route := &models.Route{}
|
wroute := &models.RouteWrapper{}
|
||||||
|
|
||||||
err := c.BindJSON(route)
|
err := c.BindJSON(wroute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
log.WithError(err).Error(models.ErrInvalidJSON)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
route.AppName = c.Param("app")
|
if wroute.Route == nil {
|
||||||
|
log.WithError(err).Error(models.ErrInvalidJSON)
|
||||||
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesMissingNew))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err := route.Validate(); err != nil {
|
wroute.Route.AppName = c.Param("app")
|
||||||
|
|
||||||
|
if err := wroute.Validate(); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
app, err := store.GetApp(route.AppName)
|
app, err := store.GetApp(wroute.Route.AppName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error(models.ErrAppsGet)
|
log.WithError(err).Error(models.ErrAppsGet)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if app == nil {
|
if app == nil {
|
||||||
app, err = store.StoreApp(&models.App{Name: route.AppName})
|
app, err = store.StoreApp(&models.App{Name: wroute.Route.AppName})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error(models.ErrAppsCreate)
|
log.WithError(err).Error(models.ErrAppsCreate)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||||
@@ -44,9 +50,9 @@ func handleRouteCreate(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
route, err = store.StoreRoute(route)
|
route, err := store.StoreRoute(wroute.Route)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrRoutesCreate)
|
log.WithError(err).Error(models.ErrRoutesCreate)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,5 +24,5 @@ func handleRouteGet(c *gin.Context) {
|
|||||||
|
|
||||||
log.WithFields(logrus.Fields{"route": route}).Debug("Got route")
|
log.WithFields(logrus.Fields{"route": route}).Debug("Got route")
|
||||||
|
|
||||||
c.JSON(http.StatusOK, route)
|
c.JSON(http.StatusOK, &models.RouteWrapper{route})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,5 +27,5 @@ func handleRouteList(c *gin.Context) {
|
|||||||
|
|
||||||
log.WithFields(logrus.Fields{"routes": routes}).Debug("Got routes")
|
log.WithFields(logrus.Fields{"routes": routes}).Debug("Got routes")
|
||||||
|
|
||||||
c.JSON(http.StatusOK, routes)
|
c.JSON(http.StatusOK, &models.RoutesWrapper{Routes: routes})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,19 +12,19 @@ func handleRouteUpdate(c *gin.Context) {
|
|||||||
store := c.MustGet("store").(models.Datastore)
|
store := c.MustGet("store").(models.Datastore)
|
||||||
log := c.MustGet("log").(logrus.FieldLogger)
|
log := c.MustGet("log").(logrus.FieldLogger)
|
||||||
|
|
||||||
route := &models.Route{}
|
wroute := &models.RouteWrapper{}
|
||||||
appName := c.Param("app")
|
appName := c.Param("app")
|
||||||
|
|
||||||
err := c.BindJSON(route)
|
err := c.BindJSON(wroute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
route.AppName = appName
|
wroute.Route.AppName = appName
|
||||||
|
|
||||||
route, err = store.StoreRoute(route)
|
route, err := store.StoreRoute(wroute.Route)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrAppsCreate)
|
log.WithError(err).Debug(models.ErrAppsCreate)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||||
|
|||||||
Reference in New Issue
Block a user