Merge pull request #2 from k-puig/feature/endpoints
added actions, routes, and socketio bun engine
This commit is contained in:
@@ -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
|
||||||
|
|||||||
27
concord-server/src/routes/actions.ts
Normal file
27
concord-server/src/routes/actions.ts
Normal 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
|
||||||
11
concord-server/src/routes/index.ts
Normal file
11
concord-server/src/routes/index.ts
Normal 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;
|
||||||
50
concord-server/src/services/actions.ts
Normal file
50
concord-server/src/services/actions.ts
Normal 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<{}
|
||||||
Reference in New Issue
Block a user