-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
69 lines (60 loc) · 1.65 KB
/
app.js
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
import express from "express";
import helmet from "helmet";
import cors from "cors";
import sqlite3 from "sqlite3";
import { open } from "sqlite";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(helmet());
app.use(cors());
// Set trust proxy
app.set("trust proxy", 1);
// Check if user has starred
async function hasUserStarred(username) {
const db = await open({
filename: "./stargazers.db",
driver: sqlite3.Database,
});
try {
const result = await db.get(
"SELECT * FROM existing_stargazers_table WHERE username = ?",
username.toLowerCase()
);
return result !== undefined;
} finally {
await db.close();
}
}
// Routes
app.get("/check-star/:username", async (req, res) => {
try {
console.log(`Received request for username: ${req.params.username}`);
const username = req.params.username;
const hasStarred = await hasUserStarred(username);
res.json({ hasStarred });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/check-star", async (req, res) => {
try {
const username = req.query.username;
console.log(`Received request for username: ${username}`);
if (!username) {
return res.status(400).json({ error: "Username is required" });
}
const hasStarred = await hasUserStarred(username);
res.json({ hasStarred });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});