From 58abd3c241b5e1eaf61fabf284931b16177459c7 Mon Sep 17 00:00:00 2001 From: PrimarchPaul Date: Sat, 27 Sep 2025 02:52:38 -0400 Subject: [PATCH] added actions, routes, and socketio bun engine --- concord-server/src/index.ts | 50 ++++++++++++++++++++++++-- concord-server/src/routes/actions.ts | 27 ++++++++++++++ concord-server/src/routes/index.ts | 11 ++++++ concord-server/src/services/actions.ts | 50 ++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 concord-server/src/routes/actions.ts create mode 100644 concord-server/src/routes/index.ts create mode 100644 concord-server/src/services/actions.ts diff --git a/concord-server/src/index.ts b/concord-server/src/index.ts index 3191383..da9f8a6 100644 --- a/concord-server/src/index.ts +++ b/concord-server/src/index.ts @@ -1,9 +1,53 @@ import { Hono } from 'hono' +import { cors } from 'hono/cors' +import { Server as Engine } from '@socket.io/bun-engine' +import { Server } from 'socket.io' +import routes from './routes/index' + +//initialize socket.io server +const io = new Server() + +//initialize bun engine +//then bind to socket.io server +const engine = new Engine() +io.bind(engine) + +io.on('connection', (socket) => { + + //get userId and clientId from query params + const userId = socket.handshake.query.userId; + const clientId = socket.handshake.query.clientId; + if(!userId || Array.isArray(userId)){ + socket.disconnect(); + throw new Error('Invalid user ID'); + } + + if(!clientId || Array.isArray(clientId)){ + socket.disconnect() + throw new Error('Invalid client ID') + } + + socket.join(userId) + console.log(`User ${userId} connected. Client ID ${clientId} on socket ${socket.id}`); + + socket.on('disconnect', () => { + console.log(`User ${userId} disconnected from socket ${socket.id}`); + }); +}); const app = new Hono() -app.get('/', (c) => { - return c.text('Hello Hono!') -}) +app.use('*', cors({ + origin: 'http://localhost:5173', + allowHeaders: ['Content-Type', 'Authorization'], + allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], + credentials: true +})) +app.use(routes) + + + + + export default app diff --git a/concord-server/src/routes/actions.ts b/concord-server/src/routes/actions.ts new file mode 100644 index 0000000..68fa198 --- /dev/null +++ b/concord-server/src/routes/actions.ts @@ -0,0 +1,27 @@ +import { Hono } from "hono" +const actions = new Hono() + + +actions.post("actions/message/:id", (c) => { + //todo: pass service function to send a message to user id +}) + +actions.delete("actions/message/delete/:id", (c) => { + //todo: pass service function to delete a message by user id +}) + +actions.get("actions/message/all/:userId", (c) => { + //todo: pass service function to fetch all messages f a user +}) + +actions.get("actions/:id", (c) => { + //todo: pass service function to fetch user data by id +}) + +actions.get("actions/userChannels/:id", (c) => { + //todo: pass service function to fetch all channels the id is part of +}) + + + +export default actions \ No newline at end of file diff --git a/concord-server/src/routes/index.ts b/concord-server/src/routes/index.ts new file mode 100644 index 0000000..4ba151a --- /dev/null +++ b/concord-server/src/routes/index.ts @@ -0,0 +1,11 @@ +//place exported routes below this line +import { Hono } from 'hono'; +import actions from './actions'; + + +const routes = new Hono(); + +routes.route("/", actions) + + +export default routes; \ No newline at end of file diff --git a/concord-server/src/services/actions.ts b/concord-server/src/services/actions.ts new file mode 100644 index 0000000..879d60a --- /dev/null +++ b/concord-server/src/services/actions.ts @@ -0,0 +1,50 @@ +//send a ping to a user + +export async function getUserChannels(userId: string): Promise { + try{ + if(!userId){ + throw new Error('missing userId'); + } + + //TODO: add some prisma code here + //to fetch all the channels a user is part of + + + return ["test1","test2"]; + }catch(err){ + const errMessage = err as Error + + if(errMessage.message === 'missing userId'){ + console.log('services::actions::getUserChannels - missing userId'); + return null; + } + + console.log('services::actions::getUserChannels - unknown error', errMessage); + return null; + } +} + +export async function getUserInformation(userId: string): Promise<{id: string, name: string} | null> { + try{ + if(!userId){ + throw new Error('missing userId'); + } + + //TODO: add some prisma code here + //to fetch user information + + return {id: userId, name: "Test User"}; + }catch(err){ + const errMessage = err as Error + + if(errMessage.message === 'missing userId'){ + console.log('services::actions::getUserInformation - missing userId'); + return null; + } + + console.log('services::actions::getUserInformation - unknown error', errMessage); + return null; + } +} + +export async function getAllMessage(userId: string): Promise<{}