diff options
Diffstat (limited to 'api/index.ts')
-rw-r--r-- | api/index.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/api/index.ts b/api/index.ts new file mode 100644 index 0000000..59c4920 --- /dev/null +++ b/api/index.ts @@ -0,0 +1,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; |