-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
161 lines (132 loc) · 3.91 KB
/
Copy pathserver.js
File metadata and controls
161 lines (132 loc) · 3.91 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const { AccessToken } = require("livekit-server-sdk");
const { createClient } = require("@supabase/supabase-js");
const {
initTimeTrackerSync,
handleManualSync,
} = require("./timeTrackerSync");
const requiredEnvVars = [
"SUPABASE_URL",
"SUPABASE_SERVICE_ROLE_KEY",
"LIVEKIT_API_KEY",
"LIVEKIT_API_SECRET",
];
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
console.error(`Missing required environment variable: ${envVar}`);
process.exit(1);
}
}
const app = express();
app.use(cors());
app.use(express.json());
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY
);
// Time-tracker sync engine (Wakatime/Hackatime): manual endpoint + daily cron.
const cleanupTimeTracker = initTimeTrackerSync(supabase);
app.post("/sync-time-trackers", handleManualSync);
const PORT = process.env.PORT || 3000;
let server;
function createServer() {
return app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://0.0.0.0:${PORT}`);
});
}
function gracefulShutdown(signal) {
console.log(`Received ${signal}. Starting graceful shutdown...`);
if (cleanupTimeTracker) {
try {
cleanupTimeTracker();
console.log('Time tracker cron stopped');
} catch (err) {
console.error('Error stopping time tracker cron:', err);
}
}
if (server) {
server.close((err) => {
if (err) {
console.error('Error during server close:', err);
process.exit(1);
}
console.log('Server closed. Exiting process.');
process.exit(0);
});
setTimeout(() => {
console.error('Forced shutdown after timeout');
process.exit(1);
}, 10000);
} else {
process.exit(0);
}
}
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
gracefulShutdown('uncaughtException');
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
server = createServer();
app.get("/health", (req, res) => {
res.status(200).json({
status: "ok",
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage(),
pid: process.pid
});
});
app.get("/getToken", async (req, res) => {
try {
if (req.method === "OPTIONS") {
return res.sendStatus(200);
}
const room = req.query.room;
const username = req.query.username;
if (!room || !username) {
return res.status(400).json({ error: "room and username are required" });
}
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: "unauthorized" });
}
const sessionToken = authHeader.replace("Bearer ", "");
const { data: { user }, error: authError } = await supabase.auth.getUser(sessionToken);
if (authError || !user) {
return res.status(401).json({ error: "invalid session" });
}
const { data: profile, error: profileError } = await supabase
.from("profiles")
.select("username")
.eq("id", user.id)
.single();
if (profileError || !profile) {
return res.status(401).json({ error: "profile not found" });
}
if (profile.username !== username) {
return res.status(403).json({ error: "username mismatch" });
}
const at = new AccessToken(
process.env.LIVEKIT_API_KEY,
process.env.LIVEKIT_API_SECRET,
{ identity: username }
);
at.addGrant({
roomJoin: true,
room,
canPublish: true,
canSubscribe: true,
});
const token = await at.toJwt();
res.json({ token });
} catch (err) {
console.error(err);
res.status(500).json({ error: "failed to generate token" });
}
});