Skip to content

Commit a621e71

Browse files
author
alexadrake
committed
update: devex
1 parent 7e36cf9 commit a621e71

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111

1212
# Dependency directories (remove the comment below to include it)
1313
# vendor/
14+
.idea

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ func main() {
5555
}
5656
```
5757

58+
**Note:** Additional middlewares are provided for different routers.
59+
5860
Build and deploy your app and that's it. Your API is being tracked in the Speakeasy workspace you just created
5961
and will be visible on the dashboard next time you log in. Visit our [docs site](https://docs.speakeasyapi.dev/) to
6062
learn more.
6163

64+
#### Mux-based routers
65+
66+
For middlewares based on the net/http `ServeMux` interface, use `speakeasy.MiddlewareWithMux`.
67+
68+
#### Gin
69+
70+
For the [gin](https://github.com/gin-gonic/gin) framework, use `speakeasy.GinMiddlware`.
71+
6272
### Advanced configuration
6373

6474
The Speakeasy SDK provides both a global and per Api configuration option. If you want to use the SDK to track multiple Apis or Versions from the same service you can configure individual instances of the SDK, like so:
@@ -118,9 +128,10 @@ To help the SDK in these situations you can provide path hints per request handl
118128
```go
119129
func MyHandler(w http.ResponseWriter, r *http.Request) {
120130
// Provide a path hint for the request using the OpenAPI Path Templating format: https://swagger.io/specification/#path-templating-matching
121-
ctrl := speakeasy.MiddlewareController(req)
122-
ctrl.PathHint("/v1/users/{id}")
123-
131+
ctrl, ok := speakeasy.MiddlewareController(req)
132+
if ok {
133+
ctrl.PathHint("/v1/users/{id}")
134+
}
124135
// the rest of your handlers code
125136
}
126137
```
@@ -138,9 +149,10 @@ To help associate requests with customers/users of your APIs you can provide a c
138149

139150
```go
140151
func MyHandler(w http.ResponseWriter, r *http.Request) {
141-
ctrl := speakeasy.MiddlewareController(req)
142-
ctrl.CustomerID("a-customers-id") // This customer ID will be used to associate this instance of a request with your customers/users
143-
152+
ctrl, ok := speakeasy.MiddlewareController(req)
153+
if ok {
154+
ctrl.CustomerID("a-customers-id") // This customer ID will be used to associate this instance of a request with your customers/users
155+
}
144156
// the rest of your handlers code
145157
}
146158
```
@@ -157,7 +169,7 @@ But if you would like to be more selective you can mask certain sensitive data u
157169

158170
```go
159171
func MyHandler(w http.ResponseWriter, r *http.Request) {
160-
ctrl := speakeasy.MiddlewareController(req)
172+
ctrl, _ := speakeasy.MiddlewareController(req)
161173
ctrl.Masking(speakeasy.WithRequestHeaderMask("Authorization")) // Mask the Authorization header in the request
162174

163175
// the rest of your handlers code
@@ -190,7 +202,7 @@ r.Use(speakeasy.Middleware)
190202
r.Use(func (next http.Handler) http.Handler {
191203
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
192204
// Mask the Authorization header in the request for all requests served by this middleware
193-
ctrl := speakeasy.MiddlewareController(req)
205+
ctrl, _ := speakeasy.MiddlewareController(req)
194206
ctrl.Masking(speakeasy.WithRequestHeaderMask("Authorization"))
195207
})
196208
})
@@ -233,7 +245,7 @@ accessToken, err := storeSDKInstance.GetEmbedAccessToken(ctx, &embedaccesstoken.
233245

234246
// Or finally if you have a handler that you would like to generate an access token from, you can get the SDK instance for that handler from the middleware controller and use the `GetEmbedAccessToken` function it.
235247
func MyHandler(w http.ResponseWriter, r *http.Request) {
236-
ctrl := speakeasy.MiddlewareController(req)
248+
ctrl, _ := speakeasy.MiddlewareController(req)
237249
accessToken, err := ctrl.GetSDKInstance().GetEmbedAccessToken(ctx, &embedaccesstoken.EmbedAccessTokenRequest{
238250
Filters: []*embedaccesstoken.EmbedAccessTokenRequest_Filter{
239251
{

controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ type controller struct {
224224

225225
// MiddlewareController will return the speakeasy middleware controller from the current request,
226226
// if the current request is monitored by the speakeasy middleware.
227-
func MiddlewareController(r *http.Request) *controller {
228-
c, _ := r.Context().Value(controllerKey).(*controller)
229-
return c
227+
func MiddlewareController(r *http.Request) (*controller, bool) {
228+
c, ok := r.Context().Value(controllerKey).(*controller)
229+
return c, ok
230230
}
231231

232232
// PathHint will allow you to provide a path hint for the current request.

0 commit comments

Comments
 (0)