-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
150 lines (117 loc) · 4.11 KB
/
index.ts
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { file, serve } from "bun";
import ChatManager from "./chatManager";
import UserManager, { User } from "./userManager";
import TTS from "./tts-openai";
const tts = new TTS();
const userManager = new UserManager();
const chatManager = new ChatManager(userManager);
const CORS_HEADERS = {
headers: {
"Access-Control-Allow-Origin":
process.env.FRONTEND_URL || "http://localhost:8080",
"Access-Control-Allow-Methods": "OPTIONS, POST, GET, PATCH, DELETE",
"Access-Control-Allow-Headers":
"Origin, X-Requested-With, Content-Type, Accept",
},
};
const port = process.env.PORT || 3000;
const server = serve({
async fetch(req) {
const url = new URL(req.url);
// Handle CORS preflight requests
if (req.method === "OPTIONS") {
const res = new Response("Departed", CORS_HEADERS);
return res;
}
if (url.pathname === "/") return new Response("Home page!", CORS_HEADERS);
if (url.pathname === "/tts" && req.method === "POST") {
// const res = new Response();
// return res;
const data = await req.json();
const text = data?.text;
const audioBuffer = await tts.create(text);
if (!audioBuffer) return new Response("An error occured!", CORS_HEADERS);
const res = new Response(audioBuffer, CORS_HEADERS);
return res;
}
if (url.pathname === "/chat" && req.method === "POST") {
const message = await req.json();
if (typeof message !== "object")
return new Response("An error occured!", CORS_HEADERS);
const response = await chatManager.handleMessage(message);
if (!response) return new Response("An error occured!", CORS_HEADERS);
const answer = {
content: response,
};
return new Response(JSON.stringify(answer), {
headers: {
...CORS_HEADERS.headers,
"Content-Type": "application/json",
},
});
}
if (url.pathname === "/profile" && req.method === "POST") {
const message = await req.json();
if (typeof message !== "object")
return new Response("An error occured!", CORS_HEADERS);
const response = await chatManager.handleProfile(message);
if (!response) return new Response("An error occured!", CORS_HEADERS);
const answer = {
content: response,
};
return new Response(JSON.stringify(answer), {
headers: {
...CORS_HEADERS.headers,
"Content-Type": "application/json",
},
});
}
if (url.pathname === "/user") {
// Create user
if (req.method === "POST") {
const user = (await req.json()) as User;
try {
await userManager.createUser(user);
} catch (error) {
console.log(error);
return new Response("An error occured!", CORS_HEADERS);
}
return new Response("200", CORS_HEADERS);
}
// Update user
if (req.method === "PATCH") {
const user = (await req.json()) as Partial<User> & { id: string };
try {
await userManager.updateUser(user);
} catch (error) {
return new Response("An error occured!", CORS_HEADERS);
}
return new Response("200", CORS_HEADERS);
}
// Delete users
}
if (url.pathname === "/users") {
if (req.method === "GET") {
const users = await userManager.getAllUsers();
if (!users) return new Response("An error occured!", CORS_HEADERS);
return new Response(JSON.stringify(users), {
headers: {
...CORS_HEADERS.headers,
"Content-Type": "application/json",
},
});
}
if (req.method === "DELETE") {
const userIds = (await req.json()) as string[];
// const deletedIds = await userManager.deleteAllUsersExcept(userIds);
// if (deletedIds === undefined)
// return new Response("An error occured!", CORS_HEADERS);
return new Response("200", CORS_HEADERS);
}
}
return new Response("404!", CORS_HEADERS);
},
port,
});
console.log(`Listening on ${server.hostname}:${server.port}`);
// userManager.deleteAllUsersExcept([]);