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

Commit 29da386

Browse files
committed
format go examples in readme
1 parent 9bc58f8 commit 29da386

1 file changed

Lines changed: 32 additions & 32 deletions

File tree

README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Create a statsd client (if desired) and create a rye Config in order to pass in
5252

5353
```go
5454
config := &rye.Config{
55-
Statter: statsdClient,
56-
StatRate: DEFAULT_STATSD_RATE,
55+
Statter: statsdClient,
56+
StatRate: DEFAULT_STATSD_RATE,
5757
}
5858
```
5959

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

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

104104
srv := &http.Server{
105-
Addr: ListenAddress,
106-
Handler: routes,
105+
Addr: ListenAddress,
106+
Handler: routes,
107107
}
108108

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

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

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

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}
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}
140140
}
141141
```
142142
Now in a later middleware, you can easily retrieve the value you set!
143143
```go
144144
func getContextVar(rw http.ResponseWriter, r *http.Request) *rye.Response {
145-
// Retrieving the value is easy!
146-
myVal := r.Context().Value("CONTEXT_KEY")
145+
// Retrieving the value is easy!
146+
myVal := r.Context().Value("CONTEXT_KEY")
147147

148-
// Log it to the server log?
149-
log.Infof("Context Value: %v", myVal)
150-
151-
return nil
148+
// Log it to the server log?
149+
log.Infof("Context Value: %v", myVal)
150+
151+
return nil
152152
}
153153
```
154154
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`.
@@ -191,15 +191,15 @@ routes.Handle("/", middlewareHandler.Handle([]rye.Handler{
191191

192192
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:
193193
```go
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
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
203203
}
204204
```
205205

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

0 commit comments

Comments
 (0)