/
main.go
119 lines (101 loc) · 3.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"log"
"net/http"
"os"
"sync/atomic"
"github.com/livefir/fir"
ory "github.com/ory/client-go"
)
type App struct {
ory *ory.APIClient
// save the cookies for any upstream calls to the Ory apis
cookies string
// save the session to display it on the dashboard
session *ory.Session
}
func (app *App) sessionMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
log.Printf("handling middleware request\n")
// set the cookies on the ory client
// this example passes all request.Cookies
// to `ToSession` function
//
// However, you can pass only the value of
// ory_session_projectid cookie to the endpoint
cookies := request.Header.Get("Cookie")
// check if we have a session
session, _, err := app.ory.FrontendApi.ToSession(request.Context()).Cookie(cookies).Execute()
if (err != nil && session == nil) || (err == nil && !*session.Active) {
// this will redirect the user to the managed Ory Login UI
http.Redirect(writer, request, "/.ory/self-service/login/browser", http.StatusSeeOther)
return
}
app.cookies = cookies
app.session = session
// continue to the requested page (in our case the Dashboard)
next.ServeHTTP(writer, request)
}
}
var content = `<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css" />
<script defer src="https://unpkg.com/@livefir/fir@latest/dist/fir.min.js"></script>
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
<div class="my-6" style="height: 500px">
<div class="columns is-mobile is-centered is-vcentered">
<div x-data class="column is-one-third-desktop has-text-centered is-narrow">
{{block "count" .}}
<div @inc.window="$fir.replaceEl()" @dec.window="$fir.replaceEl()" id="count">
{{.count}}
</div>
{{end}}
<button class="button has-background-primary" @click="$dispatch('inc')">+
</button>
<button class="button has-background-primary" @click="$dispatch('dec')">-
</button>
</div>
</div>
</div>
</body>
</html>`
type index struct {
value int32
}
func (i *index) load(ctx fir.RouteContext) error {
return ctx.Data(map[string]any{"count": atomic.LoadInt32(&i.value)})
}
func (i *index) inc(ctx fir.RouteContext) error {
return ctx.Data(map[string]any{"count": atomic.AddInt32(&i.value, 1)})
}
func (i *index) dec(ctx fir.RouteContext) error {
return ctx.Data(map[string]any{"count": atomic.AddInt32(&i.value, -1)})
}
func (i *index) Options() fir.RouteOptions {
return fir.RouteOptions{
fir.ID("counter"),
fir.Content("count.html"),
fir.OnLoad(i.load),
fir.OnEvent("inc", i.inc),
fir.OnEvent("dec", i.dec),
}
}
func main() {
proxyPort := os.Getenv("PROXY_PORT")
if proxyPort == "" {
proxyPort = "4000"
}
c := ory.NewConfiguration()
c.Servers = ory.ServerConfigurations{{URL: fmt.Sprintf("http://localhost:%s/.ory", proxyPort)}}
app := &App{
ory: ory.NewAPIClient(c),
}
controller := fir.NewController("fir-ory-counter", fir.DevelopmentMode(true))
http.Handle("/", app.sessionMiddleware(controller.Route(&index{})))
log.Println("listening on http://localhost:9867")
http.ListenAndServe(":9867", nil)
}