Skip to content

Commit 42f8a17

Browse files
committed
feat: add example/demo
1 parent 38a93f7 commit 42f8a17

7 files changed

Lines changed: 421 additions & 0 deletions

File tree

.mise/tasks/demo.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
# /// mise
3+
# description = "Quick start for testing charge + example together."
4+
# ///
5+
#
6+
# Usage:
7+
# mise run demo
8+
9+
set -e
10+
11+
CHARGE_PORT=8080
12+
CHARGE_URL=http://localhost:$CHARGE_PORT
13+
BE_PORT=8081
14+
BE_CALLBACK_URL=http://localhost:$BE_PORT/callback
15+
16+
SIGNING_KEY_RAW=$(openssl ecparam -genkey -name prime256v1 -noout)
17+
SIGNING_KEY=$(printf '%s' "$SIGNING_KEY_RAW" | sed ':a;N;$!ba;s/\n/\\n/g')
18+
19+
echo "starting charge on :$CHARGE_PORT"
20+
CHARGE_DEPLOYMENT_URL=$CHARGE_URL \
21+
CHARGE_SIGNING_KEYS="[{\"id\":\"dev-key\",\"pem\":\"$SIGNING_KEY\",\"alg\":\"ES256\"}]" \
22+
CHARGE_PORT=$CHARGE_PORT \
23+
CHARGE_ALLOW_INSECURE_ORIGINS=true \
24+
go run ./cmd/charge &
25+
CHARGE_PID=$!
26+
27+
sleep 1
28+
29+
echo "starting example backend on :$BE_PORT"
30+
(
31+
cd example
32+
CHARGE_URL=$CHARGE_URL \
33+
CALLBACK_URL=$BE_CALLBACK_URL \
34+
PORT=$BE_PORT \
35+
go run server.go
36+
) &
37+
BE_PID=$!
38+
39+
echo ""
40+
echo "charge: $CHARGE_URL"
41+
echo "example: http://localhost:$BE_PORT"
42+
echo "press Ctrl+C to stop"
43+
echo ""
44+
45+
trap 'kill $CHARGE_PID $BE_PID 2>/dev/null; exit' INT TERM
46+
47+
wait

example/client.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const statusEl = document.getElementById("status");
2+
const messagesEl = document.getElementById("messages");
3+
const formEl = document.getElementById("form");
4+
const inputEl = document.getElementById("input");
5+
const submitBtn = formEl.querySelector("button");
6+
7+
function setStatus(text) {
8+
statusEl.textContent = text;
9+
}
10+
11+
function addMessage(text) {
12+
const li = document.createElement("li");
13+
li.textContent = text;
14+
messagesEl.appendChild(li);
15+
}
16+
17+
function setConnected(connected) {
18+
statusEl.textContent = connected ? "Connected" : "Connecting...";
19+
inputEl.disabled = !connected;
20+
submitBtn.disabled = !connected;
21+
}
22+
23+
// Connect to the SSE endpoint and handle messages
24+
function connect(client_token, beCallbackUrl, chargeUrl) {
25+
setConnected(false);
26+
27+
const url = `${chargeUrl}/sse?token=${encodeURIComponent(client_token)}&callback_url=${encodeURIComponent(beCallbackUrl)}`;
28+
const eventSource = new EventSource(url);
29+
30+
eventSource.onopen = () => setConnected(true);
31+
eventSource.onmessage = (e) => addMessage(e.data);
32+
eventSource.onerror = () => {
33+
setConnected(false);
34+
eventSource.close();
35+
36+
// Attempt to reconnect after a delay
37+
setTimeout(() => connect(client_token, beCallbackUrl, chargeUrl), 1000);
38+
};
39+
}
40+
41+
async function sendMessage(msg) {}
42+
43+
formEl.addEventListener("submit", async function (e) {
44+
e.preventDefault();
45+
const msg = inputEl.value.trim();
46+
inputEl.value = "";
47+
await sendMessage(msg);
48+
49+
if (!msg) return;
50+
51+
// Send the message to the backend
52+
const res = await fetch(`/sendMessage`, {
53+
method: "POST",
54+
body: msg,
55+
});
56+
if (!res.ok) {
57+
console.log("Failed to send message:", res.statusText);
58+
}
59+
});
60+
61+
const beData = await (await fetch("/connect")).json();
62+
connect(beData.client_token, beData.callback_url, beData.charge_url);

example/examples

12.6 MB
Binary file not shown.

example/go.mod

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module sidus.io/charge/examples
2+
3+
go 1.26.0
4+
5+
require (
6+
github.com/lestrrat-go/jwx/v4 v4.0.2
7+
github.com/tidwall/gjson v1.19.0
8+
)
9+
10+
require (
11+
github.com/lestrrat-go/dsig v1.3.0 // indirect
12+
github.com/lestrrat-go/option/v3 v3.0.0-alpha1 // indirect
13+
github.com/tidwall/match v1.1.1 // indirect
14+
github.com/tidwall/pretty v1.2.0 // indirect
15+
github.com/valyala/fastjson v1.6.10 // indirect
16+
golang.org/x/crypto v0.50.0 // indirect
17+
)

example/go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/lestrrat-go/dsig v1.3.0 h1:phjMOCXvYzhuIgn7Voe2rex8z166vGfxRxmqM25P9/Q=
4+
github.com/lestrrat-go/dsig v1.3.0/go.mod h1:RD2eOaidyPvpc7IJQoO3Qq52RWdy8ZcJs8lrOnoa1Kc=
5+
github.com/lestrrat-go/jwx/v4 v4.0.2 h1:T3lzN2dynOt6SuowT08ZWo/cPs3YsB0GHZSXKvfE0uQ=
6+
github.com/lestrrat-go/jwx/v4 v4.0.2/go.mod h1:F2a0rSyXsqLAL0orBZGOXrzQGv018Tx4eiEWWYR7Yzo=
7+
github.com/lestrrat-go/option/v3 v3.0.0-alpha1 h1:dvdzLwm/Ba5CJUF3jQP7w/iNYSLfy7yyh9XXNa1WjxI=
8+
github.com/lestrrat-go/option/v3 v3.0.0-alpha1/go.mod h1:5KSg20dfsKkNJtjDmaQRLZVXuUrzuCCcz/gbDK0pfKk=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
12+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
13+
github.com/tidwall/gjson v1.19.0 h1:xwxm7n691Uf3u5OFjzngavjGTh55KX5q/9w9xHW88JU=
14+
github.com/tidwall/gjson v1.19.0/go.mod h1:V37/opeE/JbLUOfH0QTXiNez2l0RUjYUhpT4szFQAfc=
15+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
16+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
17+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
18+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
19+
github.com/valyala/fastjson v1.6.10 h1:/yjJg8jaVQdYR3arGxPE2X5z89xrlhS0eGXdv+ADTh4=
20+
github.com/valyala/fastjson v1.6.10/go.mod h1:e6FubmQouUNP73jtMLmcbxS6ydWIpOfhz34TSfO3JaE=
21+
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
22+
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
23+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
24+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

example/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Chargé Example</title>
7+
</head>
8+
<body>
9+
<h1>Chargé Example</h1>
10+
<p id="status">Connecting...</p>
11+
<ul id="messages"></ul>
12+
<form id="form">
13+
<input id="input" type="text" placeholder="Message" disabled />
14+
<button type="submit" disabled>Send</button>
15+
</form>
16+
<script type="module" src="/client.js"></script>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)