Skip to content

Commit acf6747

Browse files
authored
Merge pull request #26 from speakeasy-api/ad/devex
update: devex
2 parents 7e36cf9 + ca658d8 commit acf6747

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
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: 2 additions & 2 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 {
227+
func MiddlewareController(r *http.Request) (*controller, bool) {
228228
c, _ := r.Context().Value(controllerKey).(*controller)
229-
return c
229+
return c, c != nil
230230
}
231231

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

middleware_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type test struct {
7272
Name string `json:"name"`
7373
Fields fields `json:"fields"`
7474
Args args `json:"args"`
75-
WantHAR string
75+
WantHAR string `json:"want_har"`
7676
}
7777

7878
const (
@@ -159,7 +159,7 @@ func TestSpeakeasy_Middleware_Capture_Success(t *testing.T) {
159159
})
160160

161161
h := sdkInstance.Middleware(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
162-
ctrl := speakeasy.MiddlewareController(req)
162+
ctrl, _ := speakeasy.MiddlewareController(req)
163163

164164
if tt.Args.QueryStringMasks != nil {
165165
for k, v := range tt.Args.QueryStringMasks {
@@ -457,7 +457,7 @@ func TestSpeakeasy_Middleware_GorillaMux_PathHint_Success(t *testing.T) {
457457

458458
r.Methods(http.MethodGet).Path(tt.args.path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
459459
if tt.args.devHint != "" {
460-
ctrl := speakeasy.MiddlewareController(req)
460+
ctrl, _ := speakeasy.MiddlewareController(req)
461461
require.NotNil(t, ctrl)
462462
ctrl.PathHint(tt.args.devHint)
463463
}
@@ -657,7 +657,7 @@ func TestSpeakeasy_GinMiddleware_Success(t *testing.T) {
657657
r.Use(sdkInstance.GinMiddleware)
658658

659659
r.Any("/*path", func(ctx *gin.Context) {
660-
ctrl := speakeasy.MiddlewareController(ctx.Request)
660+
ctrl, _ := speakeasy.MiddlewareController(ctx.Request)
661661

662662
if tt.Args.QueryStringMasks != nil {
663663
for k, v := range tt.Args.QueryStringMasks {
@@ -834,7 +834,7 @@ func TestSpeakeasy_GinMiddleware_PathHint_Success(t *testing.T) {
834834

835835
r.Handle(http.MethodGet, tt.args.path, func(ctx *gin.Context) {
836836
if tt.args.devHint != "" {
837-
ctrl := speakeasy.MiddlewareController(ctx.Request)
837+
ctrl, _ := speakeasy.MiddlewareController(ctx.Request)
838838
require.NotNil(t, ctrl)
839839
ctrl.PathHint(tt.args.devHint)
840840
}
@@ -897,7 +897,7 @@ func TestSpeakeasy_EchoMiddleware_Success(t *testing.T) {
897897
r.Use(sdkInstance.EchoMiddleware)
898898

899899
r.Any("/*", func(c echo.Context) error {
900-
ctrl := speakeasy.MiddlewareController(c.Request())
900+
ctrl, _ := speakeasy.MiddlewareController(c.Request())
901901

902902
if tt.Args.QueryStringMasks != nil {
903903
for k, v := range tt.Args.QueryStringMasks {
@@ -1075,7 +1075,7 @@ func TestSpeakeasy_EchoMiddleware_PathHint_Success(t *testing.T) {
10751075
r.Use(sdkInstance.EchoMiddleware)
10761076
r.Match([]string{http.MethodGet}, tt.args.path, func(c echo.Context) error {
10771077
if tt.args.devHint != "" {
1078-
ctrl := speakeasy.MiddlewareController(c.Request())
1078+
ctrl, _ := speakeasy.MiddlewareController(c.Request())
10791079
require.NotNil(t, ctrl)
10801080
ctrl.PathHint(tt.args.devHint)
10811081
}
@@ -1149,7 +1149,7 @@ func TestSpeakeasy_Middleware_Capture_CustomerID_Success(t *testing.T) {
11491149
assert.NoError(t, err)
11501150

11511151
sdkInstance.Middleware(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1152-
ctrl := speakeasy.MiddlewareController(req)
1152+
ctrl, _ := speakeasy.MiddlewareController(req)
11531153
require.NotNil(t, ctrl)
11541154
ctrl.CustomerID(tt.args.customerID)
11551155

0 commit comments

Comments
 (0)