Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit fa5dc68

Browse files
authored
Merge pull request #15 from InVisionApp/update-readme
format go examples in README and simplify install instructions
2 parents c46c97f + 869e62b commit fa5dc68

1 file changed

Lines changed: 33 additions & 39 deletions

File tree

README.md

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ A simple library to support http services. Currently, **rye** provides a middlew
1414
In order to use **rye**, you should vendor it and the **statsd** client within your project.
1515

1616
```sh
17+
govendor fetch github.com/InVisionApp/rye
1718
govendor fetch github.com/cactus/go-statsd-client/statsd
18-
19-
# Rye is a private repo, so we should clone it first
20-
mkdir -p $GOPATH/github.com/InVisionApp
21-
cd $GOPATH/github.com/InVisionApp
22-
git clone git@github.com:InVisionApp/rye.git
23-
24-
govendor add github.com/InVisionApp/rye
2519
```
2620

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

5953
```go
6054
config := &rye.Config{
61-
Statter: statsdClient,
62-
StatRate: DEFAULT_STATSD_RATE,
55+
Statter: statsdClient,
56+
StatRate: DEFAULT_STATSD_RATE,
6357
}
6458
```
6559

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

9185
func errorHandler(rw http.ResponseWriter, r *http.Request) *rye.Response {
92-
return &rye.Response {
93-
StatusCode: http.StatusInternalServerError,
94-
Err: errors.New(message),
86+
return &rye.Response{
87+
StatusCode: http.StatusInternalServerError,
88+
Err: errors.New(message),
9589
}
9690
}
9791
```
@@ -108,8 +102,8 @@ routes.Handle("/", middlewareHandler.Handle([]rye.Handler{
108102
log.Infof("API server listening on %v", ListenAddress)
109103

110104
srv := &http.Server{
111-
Addr: ListenAddress,
112-
Handler: routes,
105+
Addr: ListenAddress,
106+
Handler: routes,
113107
}
114108

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

134128
```go
135129
func addContextVar(rw http.ResponseWriter, r *http.Request) *rye.Response {
136-
// Retrieve the request's context
137-
ctx := r.Context()
130+
// Retrieve the request's context
131+
ctx := r.Context()
138132

139-
// Create a NEW context
140-
ctx = context.WithValue(ctx,"CONTEXT_KEY","my context value")
133+
// Create a NEW context
134+
ctx = context.WithValue(ctx,"CONTEXT_KEY","my context value")
141135

142-
// Return that in the Rye response
143-
// Rye will add it to the Request to
144-
// pass to the next middleware
145-
return &rye.Response{Context:ctx}
136+
// Return that in the Rye response
137+
// Rye will add it to the Request to
138+
// pass to the next middleware
139+
return &rye.Response{Context:ctx}
146140
}
147141
```
148142
Now in a later middleware, you can easily retrieve the value you set!
149143
```go
150144
func getContextVar(rw http.ResponseWriter, r *http.Request) *rye.Response {
151-
// Retrieving the value is easy!
152-
myVal := r.Context().Value("CONTEXT_KEY")
145+
// Retrieving the value is easy!
146+
myVal := r.Context().Value("CONTEXT_KEY")
147+
148+
// Log it to the server log?
149+
log.Infof("Context Value: %v", myVal)
153150

154-
// Log it to the server log?
155-
log.Infof("Context Value: %v", myVal)
156-
157-
return nil
151+
return nil
158152
}
159153
```
160154
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`.
@@ -197,15 +191,15 @@ routes.Handle("/", middlewareHandler.Handle([]rye.Handler{
197191

198192
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:
199193
```go
200-
func getJWTfromContext(rw http.ResponseWriter, r *http.Request) *rye.Response {
201-
// Retrieving the value is easy!
202-
// Just reference the rye.CONTEXT_JWT const as a key
203-
myVal := r.Context().Value(rye.CONTEXT_JWT)
204-
205-
// Log it to the server log?
206-
log.Infof("Context Value: %v", myVal)
207-
208-
return nil
194+
func getJWTfromContext(rw http.ResponseWriter, r *http.Request) *rye.Response {
195+
// Retrieving the value is easy!
196+
// Just reference the rye.CONTEXT_JWT const as a key
197+
myVal := r.Context().Value(rye.CONTEXT_JWT)
198+
199+
// Log it to the server log?
200+
log.Infof("Context Value: %v", myVal)
201+
202+
return nil
209203
}
210204
```
211205

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

0 commit comments

Comments
 (0)