Skip to content

Commit

Permalink
Merge pull request #42 from vinxi/fix/errors
Browse files Browse the repository at this point in the history
Fix multiple errors
  • Loading branch information
h2non committed Jun 5, 2016
2 parents 728994f + 5293daf commit a1ba7f3
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 62 deletions.
34 changes: 6 additions & 28 deletions docs/Admin_HTTP_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can define multiple user/password credentials to authenticate users.
GET /
```

#### Response
##### Response

```
HTTP 200 OK
Expand Down Expand Up @@ -81,32 +81,10 @@ GET /plugins
]
```

#### List plugins

```
GET /plugins
```

##### Response

```json
[
{
"id": "UGEZGfua5S3U0nDS",
"name": "auth",
"description": "Authorization and authentication protection",
"config": {
"scheme": "Bearer",
"token": "s3cr3t"
}
}
]
```

#### Register plugin

```
GET /plugins/{id}
POST /plugins
```

##### Request Body
Expand Down Expand Up @@ -160,7 +138,7 @@ GET /plugins/{id}
DELETE /plugins/{id}
```

#### Response
##### Response

```
HTTP 204 No Content
Expand All @@ -174,7 +152,7 @@ HTTP 204 No Content
GET /instances
```

#### Response
##### Response

```
HTTP 200 OK
Expand Down Expand Up @@ -232,7 +210,7 @@ HTTP 200 OK
GET /instances/{id}
```

#### Response
##### Response

```
HTTP 200 OK
Expand Down Expand Up @@ -288,7 +266,7 @@ HTTP 200 OK
DELETE /instances/{id}
```

#### Response
##### Response

```
HTTP 204 No Content
Expand Down
2 changes: 1 addition & 1 deletion manager/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# manager [![Build Status](https://travis-ci.org/vinxi/vinxi.png)](https://travis-ci.org/vinxi/vinxi) [![GoDoc](https://godoc.org/github.com/vinxi/vinxi?status.svg)](https://godoc.org/github.com/vinxi/manager) [![Coverage Status](https://coveralls.io/repos/github/vinxi/vinxi/badge.svg?branch=master)](https://coveralls.io/github/vinxi/vinxi?branch=master)

Sandbox is a vinxi based full-featured, high-level, remotely configurable proxy solution.
`manager` package implements a programmatic and HTTP API mangement layer for `vinxi` proxies.

## Installation

Expand Down
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)
}
8 changes: 4 additions & 4 deletions manager/controller_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type info struct {
Runtime string `json:"runtime"`
Platform string `json:"platform"`
NumCPU int `json:"cpus"`
NumGoroutines int `json:"gorutines"`
NumGoroutines int `json:"goroutines"`
Links map[string]string `json:"links"`
}

Expand All @@ -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))
}
14 changes: 7 additions & 7 deletions utils/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// worldwide. Distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/

// Package uniuri generates random strings good for use in URIs to identify
// Package id generates random strings good for use in URIs to identify
// unique objects.
//
// Example usage:
//
// s := uniuri.New() // s is now "apHCJBl7L1OmC57n"
// s := utils.New() // s is now "apHCJBl7L1OmC57n"
//
// A standard string created by New() is 16 bytes in length and consists of
// Latin upper and lowercase letters, and numbers (from the set of 62 allowed
Expand All @@ -26,14 +26,14 @@ package utils
import "crypto/rand"

const (
// StdLen is a standard length of uniuri string to achive ~95 bits of entropy.
// StdLen is a standard length of id string to achive ~95 bits of entropy.
StdLen = 16
// UUIDLen is a length of uniuri string to achive ~119 bits of entropy, closest
// UUIDLen is a length of id string to achive ~119 bits of entropy, closest
// to what can be losslessly converted to UUIDv4 (122 bits).
UUIDLen = 20
)

// StdChars is a set of standard characters allowed in uniuri string.
// StdChars is a set of standard characters allowed in id string.
var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

// NewID returns a new random string of the standard length, consisting of
Expand All @@ -56,15 +56,15 @@ func NewLenChars(length int, chars []byte) string {
}
clen := len(chars)
if clen < 2 || clen > 256 {
panic("uniuri: wrong charset length for NewLenChars")
panic("id: wrong charset length for NewLenChars")
}
maxrb := 255 - (256 % clen)
b := make([]byte, length)
r := make([]byte, length+(length/4)) // storage for random bytes.
i := 0
for {
if _, err := rand.Read(r); err != nil {
panic("uniuri: error reading random bytes: " + err.Error())
panic("id: error reading random bytes: " + err.Error())
}
for _, rb := range r {
c := int(rb)
Expand Down
2 changes: 1 addition & 1 deletion utils/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestNewLenCharsMaxLength(t *testing.T) {
NewLenChars(32, chars)
}

func TestBias(t *testing.T) {
func testBias(t *testing.T) {
chars := []byte("abcdefghijklmnopqrstuvwxyz")
slen := 100000
s := NewLenChars(slen, chars)
Expand Down
2 changes: 1 addition & 1 deletion vinxictl/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vinxictl [![Build Status](https://travis-ci.org/vinxi/vinxi.png)](https://travis-ci.org/vinxi/vinxi) [![GoDoc](https://godoc.org/github.com/vinxi/vinxi?status.svg)](https://godoc.org/github.com/vinxi/vinxi) [![Coverage Status](https://coveralls.io/repos/github/vinxi/vinxi/badge.svg?branch=feat%2Finheritance)](https://coveralls.io/github/vinxi/vinxi?branch=feat%2Finheritance) [![Go Report Card](https://goreportcard.com/badge/github.com/vinxi/vinxi)](https://goreportcard.com/report/github.com/vinxi/vinxi) [![API](https://img.shields.io/badge/vinxi-core-green.svg?style=flat)](https://godoc.org/github.com/vinxi/vinxi)
# vinxictl [![Build Status](https://travis-ci.org/vinxi/vinxi.png)](https://travis-ci.org/vinxi/vinxi) [![GoDoc](https://godoc.org/github.com/vinxi/vinxi?status.svg)](https://godoc.org/github.com/vinxi/vinxi) [![Coverage Status](https://coveralls.io/repos/github/vinxi/vinxi/badge.svg?branch=master)](https://coveralls.io/github/vinxi/vinxi?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/vinxi/vinxi)](https://goreportcard.com/report/github.com/vinxi/vinxi) [![API](https://img.shields.io/badge/vinxi-core-green.svg?style=flat)](https://godoc.org/github.com/vinxi/vinxi)

Command-line interface for vinxi with full-featured declarative configuration file.

Expand Down

0 comments on commit a1ba7f3

Please sign in to comment.