Move demo to fastify

This commit is contained in:
Tiago
2025-03-15 10:35:40 +00:00
parent 2ff7a16f05
commit cf1e147dc3
4 changed files with 35 additions and 24 deletions
+3 -2
View File
@@ -81,6 +81,7 @@
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget"></script>
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget/cap-floating.min.js"></script>
<!-- <script src="/cap.js"></script> -->
<!-- <script src="/cap-floating.js"></script> -->
<!-- to use local resources, uncomment this: -->
<!-- <script src="/cap.js"></script>
<script src="/cap-floating.js"></script> -->
</html>
+4 -2
View File
@@ -6,11 +6,13 @@
"type": "commonjs",
"main": "server.js",
"scripts": {
"start": "bun server.js",
"start": "bun run server.js",
"dev": "bun run --watch server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@cap.js/server": "^1.0.5",
"express": "^4.21.2"
"@fastify/static": "^8.1.1",
"fastify": "^5.2.1"
}
}
+28 -19
View File
@@ -1,23 +1,32 @@
const express = require("express");
const Cap = require("@cap.js/server");
const app = express();
app.use(express.json());
// app.use("/cap.js", express.static('../widget/src/cap.js'));
// app.use("/cap-floating.js", express.static('../widget/src/cap-floating.js'));
import path from "path";
import Cap from "@cap.js/server";
import fastifyStatic from "@fastify/static";
import Fastify from "fastify";
const fastify = Fastify();
const cap = new Cap({
tokens_store_path: ".data/tokensList.json",
});
app.get("/", (req, res) => {
res.sendFile("index.html", { root: __dirname });
fastify.register(fastifyStatic, {
root: path.join(__dirname),
prefix: "/",
});
app.post("/api/challenge", (req, res) => {
res.json(
fastify.get("/", (req, res) => {
res.sendFile("index.html");
});
fastify.get("/cap.js", (req, res) => {
res.sendFile("../widget/src/cap.js");
});
fastify.get("/cap-floating.js", (req, res) => {
res.sendFile("../widget/src/cap-floating.js");
});
fastify.post("/api/challenge", (req, res) => {
res.send(
cap.createChallenge({
challengeCount: 32,
challengeDifficulty: 3,
@@ -25,15 +34,15 @@ app.post("/api/challenge", (req, res) => {
);
});
app.post("/api/redeem", async (req, res) => {
fastify.post("/api/redeem", async (req, res) => {
const { token, solutions } = req.body;
if (!token || !solutions) {
return res.status(400).json({ success: false });
return res.code(400).send({ success: false });
}
res.json(await cap.redeemChallenge({ token, solutions }));
res.send(await cap.redeemChallenge({ token, solutions }));
});
app.listen(3000, () => {
console.log("Listening on http://localhost:3000");
fastify.listen({ port: 3000, host: "0.0.0.0" }).then(() => {
console.log("Server is running on http://localhost:3000");
});
-1
View File
@@ -1,6 +1,5 @@
const express = require('express');
const crypto = require('crypto');
const fs = require('fs').promises;
const Cap = require('./index.js');
const app = express();