Skip to content

Commit f2fc583

Browse files
authored
feat: use context in Go example middleware (#865)
1 parent 318eb29 commit f2fc583

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

code-examples/protect-page-login/go/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (app *App) dashboardHandler() http.HandlerFunc {
1313
http.Error(writer, err.Error(), http.StatusInternalServerError)
1414
return
1515
}
16-
session, err := json.Marshal(app.session)
16+
session, err := json.Marshal(getSession(request.Context()))
1717
if err != nil {
1818
http.Error(writer, err.Error(), http.StatusInternalServerError)
1919
return
Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
package main
22

33
import (
4-
"fmt"
5-
ory "github.com/ory/client-go"
6-
"net/http"
7-
"os"
4+
"fmt"
5+
"net/http"
6+
"os"
7+
8+
ory "github.com/ory/client-go"
89
)
910

1011
type App struct {
11-
ory *ory.APIClient
12-
// save the cookies for any upstream calls to the Ory apis
13-
cookies string
14-
// save the session to display it on the dashboard
15-
session *ory.Session
12+
ory *ory.APIClient
1613
}
1714

1815
func main() {
19-
proxyPort := os.Getenv("PROXY_PORT")
20-
if proxyPort == "" {
21-
proxyPort = "4000"
22-
}
23-
24-
// register a new Ory client with the URL set to the Ory CLI Proxy
25-
// we can also read the URL from the env or a config file
26-
c := ory.NewConfiguration()
27-
c.Servers = ory.ServerConfigurations{{URL: fmt.Sprintf("http://localhost:%s/.ory", proxyPort)}}
28-
29-
app := &App{
30-
ory: ory.NewAPIClient(c),
31-
}
32-
mux := http.NewServeMux()
33-
34-
// dashboard
35-
mux.Handle("/", app.sessionMiddleware(app.dashboardHandler()))
36-
37-
port := os.Getenv("PORT")
38-
if port == "" {
39-
port = "3000"
40-
}
41-
42-
fmt.Printf("Application launched and running on http://127.0.0.1:%s\n", port)
43-
// start the server
44-
http.ListenAndServe(":"+port, mux)
16+
proxyPort := os.Getenv("PROXY_PORT")
17+
if proxyPort == "" {
18+
proxyPort = "4000"
19+
}
20+
21+
// register a new Ory client with the URL set to the Ory CLI Proxy
22+
// we can also read the URL from the env or a config file
23+
c := ory.NewConfiguration()
24+
c.Servers = ory.ServerConfigurations{{URL: fmt.Sprintf("http://localhost:%s/.ory", proxyPort)}}
25+
26+
app := &App{
27+
ory: ory.NewAPIClient(c),
28+
}
29+
mux := http.NewServeMux()
30+
31+
// dashboard
32+
mux.Handle("/", app.sessionMiddleware(app.dashboardHandler()))
33+
34+
port := os.Getenv("PORT")
35+
if port == "" {
36+
port = "3000"
37+
}
38+
39+
fmt.Printf("Application launched and running on http://127.0.0.1:%s\n", port)
40+
// start the server
41+
http.ListenAndServe(":"+port, mux)
4542
}

code-examples/protect-page-login/go/middleware.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"net/http"
7+
8+
ory "github.com/ory/client-go"
69
)
710

11+
// save the cookies for any upstream calls to the Ory apis
12+
func withCookies(ctx context.Context, v string) context.Context {
13+
return context.WithValue(ctx, "req.cookies", v)
14+
}
15+
16+
func getCookies(ctx context.Context) string {
17+
return ctx.Value("req.cookies").(string)
18+
}
19+
20+
// save the session to display it on the dashboard
21+
func withSession(ctx context.Context, v *ory.Session) context.Context {
22+
return context.WithValue(ctx, "req.session", v)
23+
}
24+
25+
func getSession(ctx context.Context) *ory.Session {
26+
return ctx.Value("req.session").(*ory.Session)
27+
}
28+
829
func (app *App) sessionMiddleware(next http.HandlerFunc) http.HandlerFunc {
930
return func(writer http.ResponseWriter, request *http.Request) {
1031
log.Printf("handling middleware request\n")
@@ -26,10 +47,12 @@ func (app *App) sessionMiddleware(next http.HandlerFunc) http.HandlerFunc {
2647
http.Redirect(writer, request, "/.ory/self-service/login/browser", http.StatusSeeOther)
2748
return
2849
}
29-
app.cookies = cookies
30-
app.session = session
50+
51+
ctx := withCookies(request.Context(), cookies)
52+
ctx = withSession(ctx, session)
53+
3154
// continue to the requested page (in our case the Dashboard)
32-
next.ServeHTTP(writer, request)
55+
next.ServeHTTP(writer, request.WithContext(ctx))
3356
return
3457
}
3558
}

0 commit comments

Comments
 (0)