Skip to content

Commit

Permalink
Add example of middleware usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyp committed Oct 21, 2024
1 parent ec9bcee commit 6e29506
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
33 changes: 33 additions & 0 deletions dynoid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,39 @@ func GenerateToken(audience string) (context.Context, string)
import "github.com/heroku/x/dynoid/middleware"
```

<details><summary>Example</summary>
<p>



```go
package main

import (
"io"
"log"
"net/http"

"github.com/heroku/x/dynoid/middleware"
)

const AUDIENCE = "testing"

func main() {
authorized := middleware.AuthorizeSameSpace(AUDIENCE)
secureHandler := authorized(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello from a secure endpoint!\n")
}))

http.Handle("/secure", secureHandler)

log.Fatal(http.ListenAndServe(":8080", nil))
}
```

</p>
</details>

## Index

- [Variables](<#variables>)
Expand Down
22 changes: 22 additions & 0 deletions dynoid/middleware/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package middleware_test

import (
"io"
"log"
"net/http"

"github.com/heroku/x/dynoid/middleware"
)

const AUDIENCE = "testing"

func Example() {
authorized := middleware.AuthorizeSameSpace(AUDIENCE)
secureHandler := authorized(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello from a secure endpoint!\n")

Check failure on line 16 in dynoid/middleware/example_test.go

View workflow job for this annotation

GitHub Actions / ci (lint)

Error return value of `io.WriteString` is not checked (errcheck)
}))

http.Handle("/secure", secureHandler)

log.Fatal(http.ListenAndServe(":8080", nil))

Check failure on line 21 in dynoid/middleware/example_test.go

View workflow job for this annotation

GitHub Actions / ci (lint)

G114: Use of net/http serve function that has no support for setting timeouts (gosec)
}

0 comments on commit 6e29506

Please sign in to comment.