-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.ts
More file actions
125 lines (120 loc) · 3.54 KB
/
Copy pathpreview.ts
File metadata and controls
125 lines (120 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { file, serve } from 'bun';
import index from '#/entry/index.html';
import { MessageType } from './web/websocket/type';
process.env.PREVIEW_MODE = 'true';
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const host = process.env.HOST || 'localhost';
console.log('Starting Minecraft Server Manager in PREVIEW mode...');
const server = serve({
port,
hostname: host,
routes: {
'/': index,
'/mockServiceWorker.js': file(
import.meta.resolveSync('#/entry/mockServiceWorker.js'),
),
'/api/server-manage': async () =>
Response.json({ status: 'ok', message: 'Mocked action successful' }),
'/api/server-instance': async () =>
Response.json({
success: true,
data: { instances: ['preview-server-1', 'preview-server-2'] },
}),
'/api/server-resource': async () =>
Response.json({
status: 'ok',
data: {
name: 'preview-server-1',
cpu: '120m',
memory: '512Mi',
allocatedCpu: '1',
allocatedMemory: '2Gi',
},
}),
'/api/server-logs': async () =>
Response.json({
status: 'ok',
data: '[12:00:00] [Server thread/INFO]: Mock preview log line\n[12:00:01] [Server thread/INFO]: Server is running',
}),
'/api/file-system': async (req: Request) => {
const url = new URL(req.url);
const type = url.searchParams.get('type');
if (type === 'structure') {
return Response.json({
success: true,
data: {
name: '/',
type: 'directory',
children: [
{ name: 'server.properties', type: 'file' },
{ name: 'world', type: 'directory', children: [] },
],
},
});
}
return Response.json({
success: true,
data: '# Mock Content\nmotd=A Minecraft Server Preview',
});
},
},
websocket: {
open(ws) {},
message(ws, message) {
const msg = JSON.parse(message.toString());
switch (msg.type) {
case MessageType.HEARTBEAT:
ws.send(
JSON.stringify({
type: MessageType.HEARTBEAT,
payload: { timestamp: Date.now() },
}),
);
break;
case MessageType.SERVERINFO:
ws.send(
JSON.stringify({
type: MessageType.SERVERINFO,
payload: {
servers: [
{
id: 'preview-server-1',
name: 'Preview Server 1',
status: 'running',
address: 'preview1.example.com',
playersOnline: 5,
},
{
id: 'preview-server-2',
name: 'Preview Server 2',
status: 'stopped',
address: 'preview2.example.com',
playersOnline: 0,
},
],
},
}),
);
break;
case MessageType.RCON:
ws.send(
JSON.stringify({
type: MessageType.RCON,
payload: {
status: 'success',
response: `[Mock] Executed command: ${
(msg.payload as any).command
}`,
serverName: (msg.payload as any).serverName,
},
}),
);
break;
}
return;
},
},
});
console.log(
`Preview server started at http://${server.hostname}:${server.port}`,
);