Skip to content

Commit

Permalink
Merge pull request #15 from InVisionApp/update-readme
Browse files Browse the repository at this point in the history
format go examples in README and simplify install instructions
  • Loading branch information
caledhwa authored Dec 8, 2016
2 parents c46c97f + 869e62b commit fa5dc68
Showing 1 changed file with 33 additions and 39 deletions.
72 changes: 33 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ A simple library to support http services. Currently, **rye** provides a middlew
In order to use **rye**, you should vendor it and the **statsd** client within your project.

```sh
govendor fetch github.com/InVisionApp/rye
govendor fetch github.com/cactus/go-statsd-client/statsd

# Rye is a private repo, so we should clone it first
mkdir -p $GOPATH/github.com/InVisionApp
cd $GOPATH/github.com/InVisionApp
git clone [email protected]:InVisionApp/rye.git

govendor add github.com/InVisionApp/rye
```

## Why another middleware lib?
Expand Down Expand Up @@ -58,8 +52,8 @@ Create a statsd client (if desired) and create a rye Config in order to pass in

```go
config := &rye.Config{
Statter: statsdClient,
StatRate: DEFAULT_STATSD_RATE,
Statter: statsdClient,
StatRate: DEFAULT_STATSD_RATE,
}
```

Expand Down Expand Up @@ -89,9 +83,9 @@ func middlewareFirstHandler(rw http.ResponseWriter, r *http.Request) *rye.Respon
}

func errorHandler(rw http.ResponseWriter, r *http.Request) *rye.Response {
return &rye.Response {
StatusCode: http.StatusInternalServerError,
Err: errors.New(message),
return &rye.Response{
StatusCode: http.StatusInternalServerError,
Err: errors.New(message),
}
}
```
Expand All @@ -108,8 +102,8 @@ routes.Handle("/", middlewareHandler.Handle([]rye.Handler{
log.Infof("API server listening on %v", ListenAddress)

srv := &http.Server{
Addr: ListenAddress,
Handler: routes,
Addr: ListenAddress,
Handler: routes,
}

srv.ListenAndServe()
Expand All @@ -133,28 +127,28 @@ Here's the details of creating a middleware with a proper `Context`. You must fi

```go
func addContextVar(rw http.ResponseWriter, r *http.Request) *rye.Response {
// Retrieve the request's context
ctx := r.Context()
// Retrieve the request's context
ctx := r.Context()

// Create a NEW context
ctx = context.WithValue(ctx,"CONTEXT_KEY","my context value")
// Create a NEW context
ctx = context.WithValue(ctx,"CONTEXT_KEY","my context value")

// Return that in the Rye response
// Rye will add it to the Request to
// pass to the next middleware
return &rye.Response{Context:ctx}
// Return that in the Rye response
// Rye will add it to the Request to
// pass to the next middleware
return &rye.Response{Context:ctx}
}
```
Now in a later middleware, you can easily retrieve the value you set!
```go
func getContextVar(rw http.ResponseWriter, r *http.Request) *rye.Response {
// Retrieving the value is easy!
myVal := r.Context().Value("CONTEXT_KEY")
// Retrieving the value is easy!
myVal := r.Context().Value("CONTEXT_KEY")

// Log it to the server log?
log.Infof("Context Value: %v", myVal)

// Log it to the server log?
log.Infof("Context Value: %v", myVal)

return nil
return nil
}
```
For another simple example, look in the [JWT middleware](middleware_jwt.go) - it adds the JWT into the context for use by other middlewares. It uses the `CONTEXT_JWT` key to push the JWT token into the `Context`.
Expand Down Expand Up @@ -197,15 +191,15 @@ routes.Handle("/", middlewareHandler.Handle([]rye.Handler{

The [JWT Middleware](middleware_jwt.go) pushes the JWT token onto the Context for use by other middlewares in the chain. This is a convenience that allows any part of your middleware chain quick access to the JWT. Example usage might include a middleware that needs access to your user id or email address stored in the JWT. To access this `Context` variable, the code is very simple:
```go
func getJWTfromContext(rw http.ResponseWriter, r *http.Request) *rye.Response {
// Retrieving the value is easy!
// Just reference the rye.CONTEXT_JWT const as a key
myVal := r.Context().Value(rye.CONTEXT_JWT)

// Log it to the server log?
log.Infof("Context Value: %v", myVal)
return nil
func getJWTfromContext(rw http.ResponseWriter, r *http.Request) *rye.Response {
// Retrieving the value is easy!
// Just reference the rye.CONTEXT_JWT const as a key
myVal := r.Context().Value(rye.CONTEXT_JWT)

// Log it to the server log?
log.Infof("Context Value: %v", myVal)

return nil
}
```

Expand All @@ -215,8 +209,8 @@ The [JWT Middleware](middleware_jwt.go) pushes the JWT token onto the Context fo
This struct is configuration for the MWHandler. It holds references and config to dependencies such as the statsdClient.
```go
type Config struct {
Statter statsd.Statter
StatRate float32
Statter statsd.Statter
StatRate float32
}
```

Expand Down

0 comments on commit fa5dc68

Please sign in to comment.