-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (132 loc) · 3.9 KB
/
server.js
File metadata and controls
146 lines (132 loc) · 3.9 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
// library dependencies
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import { z } from 'zod';
import 'dotenv/config';
import morgan from 'morgan';
import cookieParser from 'cookie-parser';
// routes
import authAws from './routes/auth.aws.js';
import authGoogle from './routes/auth.google.js';
import mcpRouter from './routes/mcp.js';
import agentRouter from './routes/agent.js';
import githubAuthRouter from './routes/auth.github.js';
import deploymentsRouter from './routes/deployments.js';
import authRouter from './routes/authRoutes.js';
import userRouter from './routes/usersRoutes.js';
import pipelineCommitRouter from './routes/pipelineCommit.js';
import pipelineSessionsRouter from './routes/pipelineSessions.js';
// app.use(authRoutes);
import jenkinsRouter from './routes/jenkins.js';
// helper functions / constants / other data
import { healthCheck } from './db.js';
import { query } from './db.js';
const app = express();
app.use(express.json());
app.use(cors({ origin: true, credentials: true }));
app.use(helmet());
app.use(morgan('dev'));
app.use(cookieParser());
// --- Request Logging Middleware ---
app.use((req, _res, next) => {
const user = req.headers['x-user-id'] || 'anonymous';
console.log(
`[${new Date().toISOString()}] ${req.method} ${
req.originalUrl
} | user=${user}`
);
next();
});
// Health & DB ping
app.get('/health', (_req, res) =>
res.json({ ok: true, uptime: process.uptime() })
);
app.get('/db/ping', async (_req, res) => {
try {
const ok = await healthCheck();
res.json({ ok });
} catch (e) {
res.status(500).json({ ok: false, error: e.message });
}
});
// Routes
app.use('/', userRouter);
app.use('/deployments', deploymentsRouter);
app.use('/agent', agentRouter);
app.use('/mcp/v1', pipelineCommitRouter);
app.use('/mcp/v1', mcpRouter);
app.use('/auth/github', githubAuthRouter);
app.use(authRouter);
app.use('/auth/aws', authAws);
app.use('/auth/google', authGoogle);
app.use('/jenkins', jenkinsRouter);
app.use('/pipeline-sessions', pipelineSessionsRouter);
/** Users */
const UserBody = z.object({
email: z.string().email(),
github_username: z.string().min(1).optional(),
});
// Create or upsert user by email
app.post('/users', async (req, res) => {
const parse = UserBody.safeParse(req.body);
if (!parse.success)
return res.status(400).json({ error: parse.error.message });
const { email, github_username } = parse.data;
// upsert on email; requires a unique index on users.email
try {
const rows = await query(
`
insert into users (email, github_username)
values ($1, $2)
on conflict (email) do update set github_username = excluded.github_username
returning *;
`,
[email, github_username ?? null]
);
res.status(201).json({ user: rows[0] });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.get('/users', async (_req, res) => {
try {
const rows = await query(`
select
u.id as user_id,
u.email,
u.github_username,
c.provider,
c.access_token,
c.created_at
from users u
left join connections c on u.id = c.user_id
order by c.created_at desc
limit 100;
`);
res.json({ users: rows });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.get('/connections', async (_req, res) => {
try {
const rows = await query(
`select * from connections order by created_at desc limit 100;`
);
res.json({ connections: rows });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// --- Global Error Handler ---
app.use((err, _req, res, _next) => {
console.error('Global Error:', err);
res.status(500).json({
success: false,
error: 'Internal Server Error',
message: err.message,
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`API on http://localhost:${port}`));