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:
@@ -11,7 +11,8 @@ var (
|
||||
ErrAppsGet = errors.New("Could not get app from datastore")
|
||||
ErrAppsList = errors.New("Could not list apps from datastore")
|
||||
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 {
|
||||
|
||||
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 {
|
||||
return (filter.Path != "" && route.Path == filter.Path) &&
|
||||
(filter.AppName != "" && route.AppName == filter.AppName)
|
||||
return (filter.Path == "" || route.Path == filter.Path) &&
|
||||
(filter.AppName == "" || route.AppName == filter.AppName)
|
||||
}
|
||||
|
||||
@@ -11,5 +11,5 @@ func (m *Error) Validate() error {
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidJSON = errors.New("Could not create app")
|
||||
ErrInvalidJSON = errors.New("Invalid JSON")
|
||||
)
|
||||
|
||||
@@ -3,6 +3,8 @@ package models
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
apiErrors "github.com/go-openapi/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -12,6 +14,7 @@ var (
|
||||
ErrRoutesGet = errors.New("Could not get route from datastore")
|
||||
ErrRoutesList = errors.New("Could not list routes from datastore")
|
||||
ErrRoutesNotFound = errors.New("Route not found")
|
||||
ErrRoutesMissingNew = errors.New("Missing new route")
|
||||
)
|
||||
|
||||
type Routes []*Route
|
||||
@@ -21,8 +24,6 @@ type Route struct {
|
||||
AppName string `json:"appname"`
|
||||
Path string `json:"path"`
|
||||
Image string `json:"image"`
|
||||
Type string `json:"type,omitempty"`
|
||||
ContainerPath string `json:"container_path,omitempty"`
|
||||
Headers http.Header `json:"headers,omitempty"`
|
||||
}
|
||||
|
||||
@@ -34,20 +35,26 @@ var (
|
||||
)
|
||||
|
||||
func (r *Route) Validate() error {
|
||||
var res []error
|
||||
|
||||
if r.Name == "" {
|
||||
return ErrRoutesValidationName
|
||||
res = append(res, ErrRoutesValidationAppName)
|
||||
}
|
||||
|
||||
if r.Image == "" {
|
||||
return ErrRoutesValidationImage
|
||||
res = append(res, ErrRoutesValidationImage)
|
||||
}
|
||||
|
||||
if r.AppName == "" {
|
||||
return ErrRoutesValidationAppName
|
||||
res = append(res, ErrRoutesValidationAppName)
|
||||
}
|
||||
|
||||
if r.Path == "" {
|
||||
return ErrRoutesValidationPath
|
||||
res = append(res, ErrRoutesValidationPath)
|
||||
}
|
||||
|
||||
if len(res) > 0 {
|
||||
return apiErrors.CompositeValidationError(res...)
|
||||
}
|
||||
|
||||
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)
|
||||
log := c.MustGet("log").(logrus.FieldLogger)
|
||||
|
||||
app := &models.App{}
|
||||
wapp := &models.AppWrapper{}
|
||||
|
||||
err := c.BindJSON(app)
|
||||
err := c.BindJSON(wapp)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||
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)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||
return
|
||||
}
|
||||
|
||||
app, err = store.StoreApp(app)
|
||||
app, err := store.StoreApp(wapp.App)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrAppsCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||
|
||||
@@ -40,5 +40,5 @@ func handleAppGet(c *gin.Context) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, apps)
|
||||
c.JSON(http.StatusOK, &models.AppsWrapper{apps})
|
||||
}
|
||||
|
||||
@@ -21,14 +21,5 @@ func handleAppUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// app.Name = c.Param("app")
|
||||
|
||||
// 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))
|
||||
c.JSON(http.StatusOK, simpleError(models.ErrAppsNothingToUpdate))
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ func Start(engine *gin.Engine) {
|
||||
v1.POST("/apps", handleAppCreate)
|
||||
|
||||
v1.GET("/apps/:app", handleAppGet)
|
||||
v1.POST("/apps/:app", handleAppUpdate)
|
||||
v1.PUT("/apps/:app", handleAppUpdate)
|
||||
v1.DELETE("/apps/:app", handleAppDestroy)
|
||||
|
||||
apps := v1.Group("/apps/:app")
|
||||
|
||||
@@ -12,31 +12,37 @@ func handleRouteCreate(c *gin.Context) {
|
||||
store := c.MustGet("store").(models.Datastore)
|
||||
log := c.MustGet("log").(logrus.FieldLogger)
|
||||
|
||||
route := &models.Route{}
|
||||
wroute := &models.RouteWrapper{}
|
||||
|
||||
err := c.BindJSON(route)
|
||||
err := c.BindJSON(wroute)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||
log.WithError(err).Error(models.ErrInvalidJSON)
|
||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||
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)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||
return
|
||||
}
|
||||
|
||||
app, err := store.GetApp(route.AppName)
|
||||
app, err := store.GetApp(wroute.Route.AppName)
|
||||
if err != nil {
|
||||
log.WithError(err).Error(models.ErrAppsGet)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
|
||||
return
|
||||
}
|
||||
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 {
|
||||
log.WithError(err).Error(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 {
|
||||
log.WithError(err).Debug(models.ErrRoutesCreate)
|
||||
log.WithError(err).Error(models.ErrRoutesCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -24,5 +24,5 @@ func handleRouteGet(c *gin.Context) {
|
||||
|
||||
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")
|
||||
|
||||
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)
|
||||
log := c.MustGet("log").(logrus.FieldLogger)
|
||||
|
||||
route := &models.Route{}
|
||||
wroute := &models.RouteWrapper{}
|
||||
appName := c.Param("app")
|
||||
|
||||
err := c.BindJSON(route)
|
||||
err := c.BindJSON(wroute)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
||||
return
|
||||
}
|
||||
|
||||
route.AppName = appName
|
||||
wroute.Route.AppName = appName
|
||||
|
||||
route, err = store.StoreRoute(route)
|
||||
route, err := store.StoreRoute(wroute.Route)
|
||||
if err != nil {
|
||||
log.WithError(err).Debug(models.ErrAppsCreate)
|
||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
||||
|
||||
Reference in New Issue
Block a user