Fix API inconsistencies (#404)

* fix api inconsistencies

* handling empty format cases

* code style
This commit is contained in:
Pedro Nasser
2016-12-07 17:16:48 -02:00
committed by GitHub
parent b990cba1df
commit 5367a3ef99
8 changed files with 42 additions and 23 deletions

View File

@@ -345,8 +345,12 @@ func (ds *BoltDatastore) UpdateRoute(ctx context.Context, newroute *models.Route
if newroute.Timeout != 0 {
route.Timeout = newroute.Timeout
}
if newroute.Format != "" {
route.Format = newroute.Format
}
if newroute.MaxConcurrency != 0 {
route.MaxConcurrency = newroute.MaxConcurrency
}
if newroute.Headers != nil {
if route.Headers == nil {
route.Headers = map[string][]string{}
@@ -364,6 +368,10 @@ func (ds *BoltDatastore) UpdateRoute(ctx context.Context, newroute *models.Route
}
}
if err := route.Validate(); err != nil {
return err
}
buf, err := json.Marshal(route)
if err != nil {
return err

View File

@@ -30,15 +30,15 @@ var (
type Routes []*Route
type Route struct {
AppName string `json:"app_name,omitempty"`
Path string `json:"path,omitempty"`
Image string `json:"image,omitempty"`
Memory uint64 `json:"memory,omitempty"`
Headers http.Header `json:"headers,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
MaxConcurrency int `json:"max_concurrency,omitempty"`
Timeout int32 `json:"timeout,omitempty"`
AppName string `json:"app_name"`
Path string `json:"path"`
Image string `json:"image"`
Memory uint64 `json:"memory"`
Headers http.Header `json:"headers"`
Type string `json:"type"`
Format string `json:"format"`
MaxConcurrency int `json:"max_concurrency"`
Timeout int32 `json:"timeout"`
Config `json:"config"`
}
@@ -92,14 +92,26 @@ func (r *Route) Validate() error {
res = append(res, ErrRoutesValidationInvalidType)
}
if r.Format == "" {
r.Format = FormatDefault
}
if r.Format != FormatDefault && r.Format != FormatHTTP {
res = append(res, ErrRoutesValidationInvalidFormat)
}
if r.MaxConcurrency == 0 && r.Format == FormatHTTP {
if r.MaxConcurrency == 0 {
r.MaxConcurrency = 1
}
if r.Headers == nil {
r.Headers = http.Header{}
}
if r.Config == nil {
r.Config = map[string]string{}
}
if r.Timeout == 0 {
r.Timeout = defaultRouteTimeout
} else if r.Timeout < 0 {

View File

@@ -23,7 +23,7 @@ const (
const (
// FormatDefault ...
FormatDefault = ""
FormatDefault = "default"
// FormatHTTP ...
FormatHTTP = "http"
)

View File

@@ -26,6 +26,7 @@ type Protocol string
const (
Default Protocol = models.FormatDefault
HTTP Protocol = models.FormatHTTP
Empty Protocol = ""
)
// New creates a valid protocol handler from a I/O pipe representing containers
@@ -34,7 +35,7 @@ func New(p Protocol, in io.Writer, out io.Reader) (ContainerIO, error) {
switch p {
case HTTP:
return &HTTPProtocol{in, out}, nil
case Default:
case Default, Empty:
return &DefaultProtocol{}, nil
default:
return nil, errInvalidProtocol

View File

@@ -41,7 +41,7 @@ func (s *Server) handleAppCreate(c *gin.Context) {
return
}
_, err = Api.Datastore.InsertApp(ctx, wapp.App)
app, err := Api.Datastore.InsertApp(ctx, wapp.App)
if err != nil {
log.WithError(err).Errorln(models.ErrAppsCreate)
c.JSON(http.StatusInternalServerError, simpleError(err))
@@ -55,5 +55,5 @@ func (s *Server) handleAppCreate(c *gin.Context) {
return
}
c.JSON(http.StatusCreated, appResponse{"App successfully created", wapp.App})
c.JSON(http.StatusCreated, appResponse{"App successfully created", app})
}

View File

@@ -57,8 +57,6 @@ func handleAppUpdate(c *gin.Context) {
return
}
wapp.App = app
// Nothing to update right now in apps
c.JSON(http.StatusOK, appResponse{"App successfully updated", wapp.App})
c.JSON(http.StatusOK, appResponse{"App successfully updated", app})
}

View File

@@ -86,12 +86,12 @@ func (s *Server) handleRouteCreate(c *gin.Context) {
}
_, err = Api.Datastore.InsertRoute(ctx, wroute.Route)
route, err := Api.Datastore.InsertRoute(ctx, wroute.Route)
if err != nil {
log.WithError(err).Error(models.ErrRoutesCreate)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
return
}
c.JSON(http.StatusCreated, routeResponse{"Route successfully created", wroute.Route})
c.JSON(http.StatusCreated, routeResponse{"Route successfully created", route})
}

View File

@@ -49,12 +49,12 @@ func handleRouteUpdate(c *gin.Context) {
}
}
_, err = Api.Datastore.UpdateRoute(ctx, wroute.Route)
route, err := Api.Datastore.UpdateRoute(ctx, wroute.Route)
if err != nil {
log.WithError(err).Debug(models.ErrRoutesUpdate)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesUpdate))
return
}
c.JSON(http.StatusOK, routeResponse{"Route successfully updated", wroute.Route})
c.JSON(http.StatusOK, routeResponse{"Route successfully updated", route})
}