-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (23 loc) 路 911 Bytes
/
Copy pathserver.js
File metadata and controls
30 lines (23 loc) 路 911 Bytes
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
import express from "express";
import dotenv from "dotenv";
import { getLatest, getAll, getById } from "./store.js";
import { startBot } from "./bot.js";
dotenv.config();
const app = express();
app.use(express.json());
function apiAuth(req, res, next) {
if (req.headers["x-api-key"] !== process.env.NODEMCU_KEY) {
return res.status(401).json({ error: "Unauthorized" });
}
next();
}
// Protected routes
app.get("/api/latest", apiAuth, (req, res) => res.json(((m) => m && ({ name: m.name, etone: m.tone, summary: m.summary }))(getLatest())));
app.get("/api/all", apiAuth, (req, res) => res.json(getAll()));
app.get("/api/msg/:id", apiAuth, (req, res) => res.json(getById(req.params.id)));
// Health
app.get("/", (req, res) => res.send("Bot + API Running"));
const PORT = process.env.PORT;
app.listen(PORT, "0.0.0.0", () => console.log("API running on port", PORT));
// Start the bot
startBot();