mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-01-31 14:57:49 +00:00
feat: websocket auto detect server info change
This commit is contained in:
@@ -36,6 +36,28 @@ const bootstrap = async () => {
|
||||
console.log("Default user created");
|
||||
};
|
||||
|
||||
|
||||
const connectedClients = new Set<any>();
|
||||
const broadcastServerChange = (action: string, serverType: string, serverId: string) => {
|
||||
const message = {
|
||||
type: "SERVER_CHANGE",
|
||||
action,
|
||||
serverType,
|
||||
serverId,
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
connectedClients.forEach(client => {
|
||||
try {
|
||||
client.send(JSON.stringify(message));
|
||||
} catch (error) {
|
||||
console.error("Error sending WebSocket message:", error);
|
||||
connectedClients.delete(client);
|
||||
}
|
||||
});
|
||||
console.log(`Notified Velocity proxy: ${action} ${serverType} ${serverId}`);
|
||||
};
|
||||
|
||||
const app = new Elysia()
|
||||
.use(swagger({
|
||||
path: '/swagger',
|
||||
@@ -46,6 +68,26 @@ const app = new Elysia()
|
||||
}
|
||||
}
|
||||
}))
|
||||
.ws("/ws", {
|
||||
open(ws) {
|
||||
const apiKey = ws.data.query.apiKey;
|
||||
if (!apiKey) {
|
||||
console.log("apiKey required");
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
|
||||
connectedClients.add(ws);
|
||||
console.log("Velocity proxy connected via WebSocket");
|
||||
},
|
||||
close(ws) {
|
||||
connectedClients.delete(ws);
|
||||
console.log("Velocity proxy disconnected from WebSocket");
|
||||
},
|
||||
message(ws, message) {
|
||||
console.log("Received message from Velocity proxy:", message);
|
||||
},
|
||||
})
|
||||
.group('/api', app => app
|
||||
.derive(async ({ headers, cookie: { session_token }, path }) => {
|
||||
// Skip token validation for login route
|
||||
@@ -150,19 +192,6 @@ const app = new Elysia()
|
||||
}),
|
||||
}
|
||||
)
|
||||
.ws("/ws", {
|
||||
body: t.Object({
|
||||
message: t.String(),
|
||||
}),
|
||||
message(ws, { message }) {
|
||||
const { id } = ws.data.query;
|
||||
ws.send({
|
||||
id,
|
||||
message,
|
||||
time: Date.now(),
|
||||
});
|
||||
},
|
||||
})
|
||||
.post("/logout", async ({ session, cookie: { session_token } }) => {
|
||||
if (!session) return { success: true };
|
||||
|
||||
@@ -175,6 +204,24 @@ const app = new Elysia()
|
||||
};
|
||||
})
|
||||
.get("/servers", async ({ session }) => {
|
||||
// Broadcast to all connected WebSocket clients
|
||||
const message = {
|
||||
type: "test",
|
||||
endpoint: "/servers",
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
connectedClients.forEach(client => {
|
||||
try {
|
||||
client.send(JSON.stringify(message));
|
||||
} catch (error) {
|
||||
console.error("Error sending WebSocket message:", error);
|
||||
connectedClients.delete(client);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`/servers API called, notified ${connectedClients.size} WebSocket clients`);
|
||||
|
||||
return await ServerService.getAllServers(!session);
|
||||
})
|
||||
.get("/servers/:id", async ({ session, params: { id } }) => {
|
||||
@@ -205,6 +252,8 @@ const app = new Elysia()
|
||||
memory: body.memory,
|
||||
});
|
||||
|
||||
broadcastServerChange("CREATE", "SERVER", server.id,);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
@@ -252,6 +301,8 @@ const app = new Elysia()
|
||||
|
||||
const newServer = await ServerService.getServerById(id, !session);
|
||||
|
||||
broadcastServerChange("UPDATE", "SERVER", server.id);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
@@ -280,6 +331,8 @@ const app = new Elysia()
|
||||
where: { id },
|
||||
});
|
||||
|
||||
broadcastServerChange("DELETE", "SERVER", server.id);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
@@ -314,6 +367,8 @@ const app = new Elysia()
|
||||
memory: body.memory,
|
||||
});
|
||||
|
||||
broadcastServerChange("CREATE", "REVERSE_PROXY_SERVER", server.id);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
@@ -371,6 +426,8 @@ const app = new Elysia()
|
||||
!session
|
||||
);
|
||||
|
||||
broadcastServerChange("UPDATE", "REVERSE_PROXY_SERVER", server.id);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
@@ -404,6 +461,8 @@ const app = new Elysia()
|
||||
where: { id },
|
||||
});
|
||||
|
||||
broadcastServerChange("DELETE", "REVERSE_PROXY_SERVER", server.id);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user