Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
169 lines (152 loc) · 6.12 KB
/
Copy pathserver.ts
File metadata and controls
169 lines (152 loc) · 6.12 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import express from "express";
import { createServer as createViteServer } from "vite";
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const GAMES_FILE = path.join(__dirname, "src", "games.json");
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// Ensure src directory exists for data files
try {
await fs.mkdir(path.join(__dirname, "src"), { recursive: true });
// Initialize games.json if it doesn't exist
try {
await fs.access(GAMES_FILE);
} catch {
await fs.writeFile(GAMES_FILE, JSON.stringify([
{
"id": "stickman-parkour",
"title": "Stickman Parkour",
"thumbnail": "https://picsum.photos/seed/stickman/400/250",
"iframeUrl": "https://d11jzht7mj96rr.cloudfront.net/games/2024/construct/219/stickman-parkour/index-gg.html",
"category": "Action"
},
{
"id": "retro-snake",
"title": "Retro Snake",
"thumbnail": "https://picsum.photos/seed/snake/400/250",
"type": "internal",
"category": "Classic"
},
{
"id": "bitlife",
"title": "BitLife",
"thumbnail": "https://picsum.photos/seed/bitlife/400/250",
"iframeUrl": "https://macvg-games.github.io/strategy-games/bitlife/",
"category": "Simulation"
},
{
"id": "basketball-shoutout",
"title": "Basketball Shoutout",
"thumbnail": "https://picsum.photos/seed/basketball/400/250",
"iframeUrl": "https://app-197304.games.s3.yandex.net/197304/kj9rcykboy6eol5xnn250jesr7v0hoh1/index.html",
"category": "Sports"
},
{
"id": "atari-breakout",
"title": "Atari Breakout",
"thumbnail": "https://picsum.photos/seed/atari/400/250",
"iframeUrl": "/atari.html",
"category": "Classic"
},
{
"id": "dune-game",
"title": "Dune Dash",
"thumbnail": "https://picsum.photos/seed/dune/400/250",
"iframeUrl": "/dune.html",
"category": "Action"
},
{
"id": "snow-rider-3d",
"title": "Snow Rider 3D",
"thumbnail": "https://picsum.photos/seed/snowrider/400/250",
"iframeUrl": "/snow-rider.html",
"category": "Action"
}
], null, 2));
}
} catch (e) {
console.error("Error initializing data directory:", e);
}
// API routes
app.get("/api/games", async (req, res) => {
try {
const data = await fs.readFile(GAMES_FILE, "utf-8");
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: "Failed to read games file" });
}
});
app.post("/api/games", async (req, res) => {
try {
const games = req.body;
if (!Array.isArray(games)) {
return res.status(400).json({ error: "Invalid data format" });
}
await fs.writeFile(GAMES_FILE, JSON.stringify(games, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: "Failed to save games" });
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "custom", // Changed to custom to handle multi-page better
});
app.use(vite.middlewares);
// In dev, Vite handles the HTML files, but we can help it
app.get(["/", "/index", "/index.html"], async (req, res, next) => {
try {
const html = await fs.readFile(path.resolve(__dirname, "index.html"), "utf-8");
res.status(200).set({ "Content-Type": "text/html" }).end(await vite.transformIndexHtml(req.url, html));
} catch (e) { next(e); }
});
app.get(["/game", "/game.html"], async (req, res, next) => {
try {
const html = await fs.readFile(path.resolve(__dirname, "game.html"), "utf-8");
res.status(200).set({ "Content-Type": "text/html" }).end(await vite.transformIndexHtml(req.url, html));
} catch (e) { next(e); }
});
app.get(["/atari", "/atari.html"], async (req, res, next) => {
try {
const html = await fs.readFile(path.resolve(__dirname, "atari.html"), "utf-8");
res.status(200).set({ "Content-Type": "text/html" }).end(await vite.transformIndexHtml(req.url, html));
} catch (e) { next(e); }
});
app.get(["/dune", "/dune.html"], async (req, res, next) => {
try {
const html = await fs.readFile(path.resolve(__dirname, "dune.html"), "utf-8");
res.status(200).set({ "Content-Type": "text/html" }).end(await vite.transformIndexHtml(req.url, html));
} catch (e) { next(e); }
});
app.get(["/snow-rider", "/snow-rider.html"], async (req, res, next) => {
try {
const html = await fs.readFile(path.resolve(__dirname, "snow-rider.html"), "utf-8");
res.status(200).set({ "Content-Type": "text/html" }).end(await vite.transformIndexHtml(req.url, html));
} catch (e) { next(e); }
});
} else {
// Production: Serve static files from dist
app.use(express.static("dist", { extensions: ["html"] }));
// Handle routes without .html extension in production
app.get("/", (req, res) => res.sendFile(path.join(__dirname, "dist", "index.html")));
app.get("/game", (req, res) => res.sendFile(path.join(__dirname, "dist", "game.html")));
app.get("/atari", (req, res) => res.sendFile(path.join(__dirname, "dist", "atari.html")));
app.get("/dune", (req, res) => res.sendFile(path.join(__dirname, "dist", "dune.html")));
app.get("/snow-rider", (req, res) => res.sendFile(path.join(__dirname, "dist", "snow-rider.html")));
// Catch-all for SPA-like behavior on index.html if needed, but we have specific routes above
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();