2025-09-27 11:13:37 -04:00
|
|
|
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";
|
2025-09-27 13:20:15 -04:00
|
|
|
import { Scalar } from "@scalar/hono-api-reference";
|
|
|
|
|
import { openAPIRouteHandler } from "hono-openapi";
|
2025-09-27 02:52:38 -04:00
|
|
|
|
|
|
|
|
//initialize socket.io server
|
2025-09-27 11:13:37 -04:00
|
|
|
const io = new Server();
|
2025-09-27 02:52:38 -04:00
|
|
|
|
|
|
|
|
//initialize bun engine
|
|
|
|
|
//then bind to socket.io server
|
2025-09-27 11:13:37 -04:00
|
|
|
const engine = new Engine();
|
|
|
|
|
io.bind(engine);
|
2025-09-28 02:10:41 -04:00
|
|
|
/*
|
2025-09-27 11:13:37 -04:00
|
|
|
io.on("connection", (socket) => {
|
|
|
|
|
//get userId and clientId from query params
|
2025-09-28 02:10:41 -04:00
|
|
|
const userId = socket.handshake.query.userId
|
|
|
|
|
const clientId = socket.handshake.query.clientId
|
2025-09-27 11:13:37 -04:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 02:10:41 -04:00
|
|
|
|
2025-09-27 11:13:37 -04:00
|
|
|
socket.join(userId);
|
|
|
|
|
console.log(
|
|
|
|
|
`User ${userId} connected. Client ID ${clientId} on socket ${socket.id}`,
|
2025-09-28 02:10:41 -04:00
|
|
|
)
|
2025-09-27 11:13:37 -04:00
|
|
|
|
|
|
|
|
socket.on("disconnect", () => {
|
|
|
|
|
console.log(`User ${userId} disconnected from socket ${socket.id}`);
|
2025-09-28 02:10:41 -04:00
|
|
|
})
|
2025-09-27 02:52:38 -04:00
|
|
|
});
|
2025-09-28 02:10:41 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
io.on("ping", (socket) => {
|
|
|
|
|
console.log(`New client connected: ${socket.id}`)
|
|
|
|
|
socket.emit("pong", socket. )
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-26 23:36:08 -04:00
|
|
|
|
2025-09-27 11:13:37 -04:00
|
|
|
const app = new Hono();
|
2025-09-26 23:36:08 -04:00
|
|
|
|
2025-09-27 11:13:37 -04:00
|
|
|
app.use(
|
|
|
|
|
"*",
|
|
|
|
|
cors({
|
|
|
|
|
origin: "http://localhost:5173",
|
|
|
|
|
allowHeaders: ["Content-Type", "Authorization"],
|
|
|
|
|
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
|
|
|
credentials: true,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2025-09-27 02:52:38 -04:00
|
|
|
|
2025-09-27 11:13:37 -04:00
|
|
|
app.route("/api", routes);
|
2025-09-27 02:52:38 -04:00
|
|
|
|
2025-09-27 13:20:15 -04:00
|
|
|
app.get(
|
2025-09-27 15:55:20 -04:00
|
|
|
"/openapi",
|
2025-09-27 13:20:15 -04:00
|
|
|
openAPIRouteHandler(app, {
|
|
|
|
|
documentation: {
|
|
|
|
|
info: {
|
2025-09-27 15:55:20 -04:00
|
|
|
title: "Hono API",
|
|
|
|
|
version: "1.0.0",
|
|
|
|
|
description: "Greeting API",
|
2025-09-27 13:20:15 -04:00
|
|
|
},
|
2025-09-27 15:55:20 -04:00
|
|
|
servers: [{ url: "http://localhost:3000", description: "Local Server" }],
|
2025-09-27 13:20:15 -04:00
|
|
|
},
|
2025-09-27 15:55:20 -04:00
|
|
|
}),
|
|
|
|
|
);
|
2025-09-27 13:20:15 -04:00
|
|
|
|
2025-09-27 15:55:20 -04:00
|
|
|
app.get("/scalar", Scalar({ url: "/openapi" }));
|
2025-09-27 13:20:15 -04:00
|
|
|
|
2025-09-27 04:15:51 -04:00
|
|
|
export default app;
|