added actions, routes, and socketio bun engine

This commit is contained in:
PrimarchPaul
2025-09-27 02:52:38 -04:00
parent 007a5a6d84
commit 58abd3c241
4 changed files with 135 additions and 3 deletions

View File

@@ -1,9 +1,53 @@
import { Hono } from 'hono' 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() const app = new Hono()
app.get('/', (c) => { app.use('*', cors({
return c.text('Hello Hono!') origin: 'http://localhost:5173',
}) allowHeaders: ['Content-Type', 'Authorization'],
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
credentials: true
}))
app.use(routes)
export default app export default app

View File

@@ -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

View File

@@ -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;

View File

@@ -0,0 +1,50 @@
//send a ping to a user
export async function getUserChannels(userId: string): Promise<string[] | null> {
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<{}