-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
90 lines (79 loc) · 2.67 KB
/
server.js
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
/* eslint-disable @typescript-eslint/no-require-imports */
const { createServer } = require("http");
const { parse } = require("url");
const next = require("next");
const { Server } = require("socket.io");
const SERVICE_AGENT_ADMIN_BASE_URL =
process.env.SERVICE_AGENT_ADMIN_BASE_URL ||
"https://a.chatbot-demo.dev.2060.io";
const CREDENTIAL_DEFINITION_ID =
process.env.CREDENTIAL_DEFINITION_ID ||
"did:web:chatbot-demo.dev.2060.io?service=anoncreds&relativeRef=/credDef/HngJhYMeTLTZNa5nJxDybmXDsV8J7G1fz2JFSs3jcouT";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const PORT = process.env.NEXT_PUBLIC_PORT
? Number(process.env.NEXT_PUBLIC_PORT)
: 3000;
const PUBLIC_BASE_URL = process.env.NEXT_PUBLIC_BASE_URL
? `${process.env.NEXT_PUBLIC_BASE_URL}`
: `http://localhost:${PORT}`;
const server = createServer((req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
});
const io = new Server(server, { cors: { origin: "*" } });
io.on("connection", (socket) => {
console.log("A client connected info is:", socket.id);
let message = {};
socket.on("generateQR", async (data) => {
try {
const url = `${SERVICE_AGENT_ADMIN_BASE_URL}/v1/invitation/presentation-request`;
const requestBody = {
callbackUrl: `${PUBLIC_BASE_URL}/api/presentation`,
ref: data.socketConnectionId,
requestedCredentials: [
{ credentialDefinitionId: CREDENTIAL_DEFINITION_ID },
],
};
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(requestBody),
});
const result = await response.json();
message = {
ok: true,
shortUrl: result?.shortUrl,
};
} catch (error) {
console.error(error);
message = {
ok: false,
error: `${error}`,
};
} finally {
io.to(data.socketConnectionId).emit("generateQREventMessage", {
...message,
});
}
});
socket.on("presentationEvent", (data) => {
console.log("socket presentationEvent:", data);
io.to(data.ref).emit("presentationEventMessage", data);
});
socket.on("error", (error) => {
console.log("A server error has occurred", error);
});
socket.on("disconnect", () => {
console.log("A client disconnected");
});
});
server.listen(PORT, (err) => {
if (err) throw err;
console.log(`> Ready on ${PUBLIC_BASE_URL}`);
});
});