Merge pull request #25 from pedronasser/api-fix

Added wrappers; API fixes and swagger
This commit is contained in:
Travis Reeder
2016-07-29 11:41:49 -07:00
committed by GitHub
31 changed files with 694 additions and 409 deletions

View File

@@ -17,8 +17,6 @@ CREATE TABLE IF NOT EXISTS routes (
path text NOT NULL,
app_name character varying(256) NOT NULL,
image character varying(256) NOT NULL,
type character varying(256) NOT NULL,
container_path text NOT NULL,
headers text NOT NULL
);`
@@ -26,7 +24,7 @@ const appsTableCreate = `CREATE TABLE IF NOT EXISTS apps (
name character varying(256) NOT NULL PRIMARY KEY
);`
const routeSelector = `SELECT name, path, app_name, image, type, container_path, headers FROM routes`
const routeSelector = `SELECT name, path, app_name, image, headers FROM routes`
type rowScanner interface {
Scan(dest ...interface{}) error
@@ -162,22 +160,18 @@ func (ds *PostgresDatastore) StoreRoute(route *models.Route) (*models.Route, err
_, err = ds.db.Exec(`
INSERT INTO routes (
name, app_name, path, image,
type, container_path, headers
headers
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (name) DO UPDATE SET
path = $3,
image = $4,
type = $5,
container_path = $6,
headers = $7;
headers = $5;
`,
route.Name,
route.AppName,
route.Path,
route.Image,
route.Type,
route.ContainerPath,
headers,
)
@@ -206,8 +200,6 @@ func scanRoute(scanner rowScanner, route *models.Route) error {
&route.Path,
&route.AppName,
&route.Image,
&route.Type,
&route.ContainerPath,
&headerStr,
)

View File

@@ -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))

View File

@@ -40,5 +40,5 @@ func handleAppGet(c *gin.Context) {
app.Routes = routes
c.JSON(http.StatusOK, app)
c.JSON(http.StatusOK, &models.AppWrapper{app})
}

View File

@@ -21,5 +21,5 @@ func handleAppList(c *gin.Context) {
return
}
c.JSON(http.StatusOK, apps)
c.JSON(http.StatusOK, &models.AppsWrapper{apps})
}

View File

@@ -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))
}

View File

@@ -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")

View File

@@ -16,11 +16,17 @@ func handleRouteCreate(c *gin.Context) {
err := c.BindJSON(route)
if err != nil {
log.WithError(err).Debug(models.ErrInvalidJSON)
log.WithError(err).Error(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
return
}
if route == nil {
log.WithError(err).Error(models.ErrInvalidJSON)
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesMissingNew))
return
}
route.AppName = c.Param("app")
if err := route.Validate(); err != nil {
@@ -46,7 +52,7 @@ func handleRouteCreate(c *gin.Context) {
route, err = store.StoreRoute(route)
if err != nil {
log.WithError(err).Debug(models.ErrRoutesCreate)
log.WithError(err).Error(models.ErrRoutesCreate)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
return
}

View File

@@ -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})
}

View File

@@ -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})
}

View File

@@ -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))