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 { if newroute.Timeout != 0 {
route.Timeout = newroute.Timeout route.Timeout = newroute.Timeout
} }
route.Format = newroute.Format if newroute.Format != "" {
route.MaxConcurrency = newroute.MaxConcurrency route.Format = newroute.Format
}
if newroute.MaxConcurrency != 0 {
route.MaxConcurrency = newroute.MaxConcurrency
}
if newroute.Headers != nil { if newroute.Headers != nil {
if route.Headers == nil { if route.Headers == nil {
route.Headers = map[string][]string{} 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) buf, err := json.Marshal(route)
if err != nil { if err != nil {
return err return err

View File

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

View File

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

View File

@@ -26,6 +26,7 @@ type Protocol string
const ( const (
Default Protocol = models.FormatDefault Default Protocol = models.FormatDefault
HTTP Protocol = models.FormatHTTP HTTP Protocol = models.FormatHTTP
Empty Protocol = ""
) )
// New creates a valid protocol handler from a I/O pipe representing containers // 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 { switch p {
case HTTP: case HTTP:
return &HTTPProtocol{in, out}, nil return &HTTPProtocol{in, out}, nil
case Default: case Default, Empty:
return &DefaultProtocol{}, nil return &DefaultProtocol{}, nil
default: default:
return nil, errInvalidProtocol return nil, errInvalidProtocol

View File

@@ -41,7 +41,7 @@ func (s *Server) handleAppCreate(c *gin.Context) {
return return
} }
_, err = Api.Datastore.InsertApp(ctx, wapp.App) app, err := Api.Datastore.InsertApp(ctx, wapp.App)
if err != nil { if err != nil {
log.WithError(err).Errorln(models.ErrAppsCreate) log.WithError(err).Errorln(models.ErrAppsCreate)
c.JSON(http.StatusInternalServerError, simpleError(err)) c.JSON(http.StatusInternalServerError, simpleError(err))
@@ -55,5 +55,5 @@ func (s *Server) handleAppCreate(c *gin.Context) {
return 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 return
} }
wapp.App = app
// Nothing to update right now in apps // 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 { if err != nil {
log.WithError(err).Error(models.ErrRoutesCreate) log.WithError(err).Error(models.ErrRoutesCreate)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate)) c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
return 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 { if err != nil {
log.WithError(err).Debug(models.ErrRoutesUpdate) log.WithError(err).Debug(models.ErrRoutesUpdate)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesUpdate)) c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesUpdate))
return return
} }
c.JSON(http.StatusOK, routeResponse{"Route successfully updated", wroute.Route}) c.JSON(http.StatusOK, routeResponse{"Route successfully updated", route})
} }