Skip to content

Commit

Permalink
refactor(manager): Refactor Send, SendOk and use http constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Robert committed Jun 5, 2016
1 parent 32fbe15 commit 034c0ae
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
18 changes: 9 additions & 9 deletions manager/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ type Context struct {
// ParseBody parses the body.
func (c *Context) ParseBody(bind interface{}) error {
if c.Request.Header.Get("Content-Type") != "application/json" {
c.SendError(415, "Invalid content type. Must be application/json")
c.SendError(http.StatusUnsupportedMediaType, "Invalid content type. Must be application/json")
return errors.New("Invalid type")
}

decoder := json.NewDecoder(c.Request.Body)
return decoder.Decode(&bind)
}

// Send is used to serialize and write the response as JSON.
func (c *Context) Send(data interface{}) {
c.SendStatus(200, data)
// SendOk is used to serialize and write the response as JSON.
func (c *Context) SendOk(data interface{}) {
c.Send(http.StatusOK, data)
}

// SendStatus is used to serialize and write the response as JSON with custom status code.
func (c *Context) SendStatus(status int, data interface{}) {
// Send is used to serialize and write the response as JSON with custom status code.
func (c *Context) Send(status int, data interface{}) {
buf, err := json.Marshal(data)
if err != nil {
c.SendError(500, err.Error())
c.SendError(http.StatusInternalServerError, err.Error())
return
}
c.Response.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -71,10 +71,10 @@ func (c *Context) SendError(status int, message string) {

// SendNoContent replies with 204 status code.
func (c *Context) SendNoContent() {
c.Response.WriteHeader(204)
c.Response.WriteHeader(http.StatusNoContent)
}

// SendNotFound replies with 404 status code and custom message.
func (c *Context) SendNotFound(message string) {
c.SendError(404, message)
c.SendError(http.StatusNotFound, message)
}
6 changes: 3 additions & 3 deletions manager/controller_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (indexController) Get(ctx *Context) {
"manager": "/manager",
}

ctx.Send(info{
ctx.SendOk(info{
Hostname: hostname,
Version: vinxi.Version,
Platform: runtime.GOOS,
Expand Down Expand Up @@ -62,7 +62,7 @@ func (indexController) Catalog(ctx *Context) {
Plugins: plugins,
}

ctx.Send(catalog)
ctx.SendOk(catalog)
}

func (indexController) Manager(ctx *Context) {
Expand All @@ -72,5 +72,5 @@ func (indexController) Manager(ctx *Context) {
Links: map[string]string{"plugins": "/manager/plugins"},
}

ctx.Send(info)
ctx.SendOk(info)
}
4 changes: 2 additions & 2 deletions manager/controller_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func createInstances(instances []*Instance) []JSONInstance {
type instancesController struct{}

func (instancesController) List(ctx *Context) {
ctx.Send(createInstances(ctx.Manager.Instances()))
ctx.SendOk(createInstances(ctx.Manager.Instances()))
}

func (instancesController) Get(ctx *Context) {
ctx.Send(createInstance(ctx.Instance))
ctx.SendOk(createInstance(ctx.Instance))
}

func (instancesController) Delete(ctx *Context) {
Expand Down
6 changes: 3 additions & 3 deletions manager/controller_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func (pluginsController) List(ctx *Context) {
} else {
layer = ctx.Manager.Plugins
}
ctx.Send(createPlugins(layer.All()))
ctx.SendOk(createPlugins(layer.All()))
}

func (pluginsController) Get(ctx *Context) {
ctx.Send(createPlugin(ctx.Plugin))
ctx.SendOk(createPlugin(ctx.Plugin))
}

func (pluginsController) Delete(ctx *Context) {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (p pluginsController) Create(ctx *Context) {
}

p.registerPlugin(ctx, instance)
ctx.Send(createPlugin(instance))
ctx.SendOk(createPlugin(instance))
}

func (pluginsController) registerPlugin(ctx *Context, instance plugin.Plugin) {
Expand Down
6 changes: 3 additions & 3 deletions manager/controller_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func createRule(rule rule.Rule) JSONRule {
type rulesController struct{}

func (rulesController) List(ctx *Context) {
ctx.Send(createRules(ctx.Scope))
ctx.SendOk(createRules(ctx.Scope))
}

func (rulesController) Get(ctx *Context) {
ctx.Send(createRule(ctx.Rule))
ctx.SendOk(createRule(ctx.Rule))
}

func (rulesController) Delete(ctx *Context) {
Expand Down Expand Up @@ -80,5 +80,5 @@ func (rulesController) Create(ctx *Context) {
}

ctx.Scope.UseRule(instance)
ctx.Send(createRule(instance))
ctx.SendOk(createRule(instance))
}
6 changes: 3 additions & 3 deletions manager/controller_scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func (scopesController) List(ctx *Context) {
} else {
scopes = ctx.Manager.Scopes()
}
ctx.Send(createScopes(scopes))
ctx.SendOk(createScopes(scopes))
}

func (scopesController) Get(ctx *Context) {
ctx.Send(createScope(ctx.Scope))
ctx.SendOk(createScope(ctx.Scope))
}

func (scopesController) Delete(ctx *Context) {
Expand Down Expand Up @@ -73,5 +73,5 @@ func (scopesController) Create(ctx *Context) {
ctx.Manager.UseScope(instance)
}

ctx.Send(createScope(instance))
ctx.SendOk(createScope(instance))
}

0 comments on commit 034c0ae

Please sign in to comment.