@@ -3,10 +3,9 @@ import express from 'express';
33import cors from 'cors' ;
44import helmet from 'helmet' ;
55import morgan from 'morgan' ;
6- import { healthCheck , query } from './db.js' ;
6+ import { healthCheck } from './db.js' ;
77import githubAuthRouter from './routes/auth.github.js' ;
8- import { z } from 'zod' ;
9- import mcpRoutes from './routes/mcp.js' ;
8+ import userRouter from './routes/usersRoutes.js' ;
109
1110const app = express ( ) ;
1211app . use ( express . json ( ) ) ;
@@ -27,69 +26,33 @@ app.get('/db/ping', async (_req, res) => {
2726 }
2827} ) ;
2928
30- /** Users */
31- const UserBody = z . object ( {
32- email : z . string ( ) . email ( ) ,
33- github_username : z . string ( ) . min ( 1 ) . optional ( ) ,
34- } ) ;
35-
36- // Create or upsert user by email
37- app . post ( '/users' , async ( req , res ) => {
38- const parse = UserBody . safeParse ( req . body ) ;
39- if ( ! parse . success )
40- return res . status ( 400 ) . json ( { error : parse . error . message } ) ;
41- const { email, github_username } = parse . data ;
42-
43- // upsert on email; requires a unique index on users.email
44- try {
45- const rows = await query (
46- `
47- insert into users (email, github_username)
48- values ($1, $2)
49- on conflict (email) do update set github_username = excluded.github_username
50- returning *;
51- ` ,
52- [ email , github_username ?? null ]
53- ) ;
54- res . status ( 201 ) . json ( { user : rows [ 0 ] } ) ;
55- } catch ( e ) {
56- res . status ( 500 ) . json ( { error : e . message } ) ;
57- }
58- } ) ;
59-
60- app . get ( '/users' , async ( _req , res ) => {
61- try {
62- const rows = await query (
63- `select * from users order by created_at desc limit 100;`
64- ) ;
65- res . json ( { users : rows } ) ;
66- } catch ( e ) {
67- res . status ( 500 ) . json ( { error : e . message } ) ;
68- }
69- } ) ;
29+ // Mount users route at /users
30+ app . use ( '/' , userRouter ) ;
7031
7132// --- Request Logging Middleware ---
7233app . use ( ( req , res , next ) => {
73- const user = req . headers [ "x-user-id" ] || "anonymous" ;
74- console . log ( `[${ new Date ( ) . toISOString ( ) } ] ${ req . method } ${ req . originalUrl } | user=${ user } ` ) ;
34+ const user = req . headers [ 'x-user-id' ] || 'anonymous' ;
35+ console . log (
36+ `[${ new Date ( ) . toISOString ( ) } ] ${ req . method } ${
37+ req . originalUrl
38+ } | user=${ user } `
39+ ) ;
7540 next ( ) ;
7641} ) ;
7742
78- // -- Agent entry point
43+ // -- Agent entry point
7944app . use ( '/mcp/v1' , mcpRoutes ) ;
8045
8146// --- Global Error Handler ---
8247app . use ( ( err , req , res , next ) => {
83- console . error ( " Global Error:" , err ) ;
48+ console . error ( ' Global Error:' , err ) ;
8449 res . status ( 500 ) . json ( {
8550 success : false ,
86- error : " Internal Server Error" ,
51+ error : ' Internal Server Error' ,
8752 message : err . message ,
8853 } ) ;
8954} ) ;
9055
91-
92-
9356// Mount GitHub OAuth routes at /auth/github
9457app . use ( '/auth/github' , githubAuthRouter ) ;
9558
0 commit comments