-
Notifications
You must be signed in to change notification settings - Fork 932
/
server.js
55 lines (47 loc) · 1.19 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
import Fastify from "fastify";
import FastifyVite from "@fastify/vite";
import fastifyEnv from "@fastify/env";
// Fastify + React + Vite configuration
const server = Fastify({
logger: {
transport: {
target: "@fastify/one-line-logger",
},
},
});
const schema = {
type: "object",
required: ["OPENAI_API_KEY"],
properties: {
OPENAI_API_KEY: {
type: "string",
},
},
};
await server.register(fastifyEnv, { dotenv: true, schema });
await server.register(FastifyVite, {
root: import.meta.url,
renderer: "@fastify/react",
});
await server.vite.ready();
// Server-side API route to return an ephemeral realtime session token
server.get("/token", async () => {
const r = await fetch("https://api.openai.com/v1/realtime/sessions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-4o-realtime-preview-2024-12-17",
voice: "verse",
}),
});
return new Response(r.body, {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
});
await server.listen({ port: process.env.PORT || 3000 });