-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
53 lines (47 loc) · 1.18 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
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv";
import cors from "cors";
import { generateCards } from "./src/requests";
import { generatePromptMessage } from "./src/helpers";
const app = express();
dotenv.config();
app.use(express.json());
var corsOptions = {
origin: process.env.FE_URL,
optionsSuccessStatus: 200,
allowedHeaders: ["Content-Type"],
methods: ["POST"],
credentials: true,
};
const length = {
LOW: "180",
MEDIUM: "280",
HIGH: "400",
};
app.options("/api/create", cors(corsOptions));
app.post(
"/api/create",
cors(corsOptions),
async (req: Request, res: Response) => {
const { numberOfCards, topic, difficulty, complexity } = req.body;
try {
const promptMessage = generatePromptMessage(
numberOfCards,
topic,
difficulty,
length[complexity]
);
const modelResponse = await generateCards(promptMessage);
res.status(200).json(modelResponse.data.choices[0].message);
} catch (e) {
res
.status(500)
.json({ error: "Something went wrong, who knows what..." });
}
}
);
app.listen(process.env.PORT, () => {
console.log(
`Server is listening on port ${process.env.PORT}. So keep quiet!`
);
});