summaryrefslogtreecommitdiff
path: root/api/index.ts
blob: 59c49202891b2f4deff5590573241ba151e56192 (plain)
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
import dotenv from "dotenv";
import express, {
  type Request,
  type Response,
  type NextFunction,
} from "express";
import cors from "cors";
import { WORDLIST } from "./wordlist";
import { getGroupName as getCategoryName } from "./ai";
import { rateLimit } from "express-rate-limit";

dotenv.config();

const PORT = 4000;

const limiter = rateLimit({
  windowMs: 1 * 60 * 1000, //5 minute
  limit: 20, // 20 requests per minute
});

const app = express();
app.use(cors());
app.use(express.json());
app.use(limiter);

app.set("trust proxy", "loopback"); // specify a single subnet

app.get("/api/", (req, res) => {
  res.send("HEALTHY");
});

app.get("/api/random-words", (req, res) => {
  let words: string[] = [];
  while (words.length < 16) {
    const candidateWord = WORDLIST[Math.round(Math.random() * WORDLIST.length)];
    if (candidateWord.length < 4) {
      continue;
    }
    words.push(candidateWord);
  }
  res.send(words);
});

app.post("/api/group-words", async (req, res) => {
  res.send(await getCategoryName(req.body.words));
});

app.listen(PORT, () => {
  console.log("Initialized");
});

export default app;