- Alle @chessu/ → @michess/ (Package-Namen und Imports) - Theme-Namen chessuDark/Light → michessDark/Light - Alle Display-Texte: chessu/Michess → MiChess - Autor: dotnize → Tom Misch - Copyright: Nathaniel Tampus → Tom Misch - README komplett neu (Deutsch, MiChess-spezifisch) - CONTRIBUTING.md neu - FUNDING.yml + CODE_OF_CONDUCT.md entfernt - Fork-Hinweis auf chessu bleibt im README erhalten (MIT-Pflicht) - pnpm-lock.yaml entfernt (wird beim Build neu generiert) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
86 lines
2.4 KiB
TypeScript
86 lines
2.4 KiB
TypeScript
import cors from "cors";
|
|
import "dotenv/config";
|
|
import type { NextFunction, Request, Response } from "express";
|
|
import express from "express";
|
|
import { createServer } from "http";
|
|
import { Server } from "socket.io";
|
|
|
|
import { INIT_TABLES, db } from "./db/index.js";
|
|
import session from "./middleware/session.js";
|
|
import routes from "./routes/index.js";
|
|
import { init as initSocket } from "./socket/index.js";
|
|
|
|
const corsConfig = {
|
|
origin: process.env.CORS_ORIGIN || "http://localhost:3000",
|
|
credentials: true
|
|
};
|
|
|
|
const app = express();
|
|
const server = createServer(app);
|
|
|
|
// database
|
|
await db.connect();
|
|
db.query(INIT_TABLES, async (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
} else {
|
|
console.log("MiChess tables initialized");
|
|
|
|
// Set admin role for ADMIN_EMAIL if configured
|
|
const adminEmail = process.env.ADMIN_EMAIL;
|
|
if (adminEmail) {
|
|
try {
|
|
await db.query(
|
|
`UPDATE "user" SET role='admin' WHERE email=$1 AND role='user'`,
|
|
[adminEmail]
|
|
);
|
|
console.log(`Admin role ensured for: ${adminEmail}`);
|
|
} catch (e) {
|
|
console.error("Failed to set admin role:", e);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// middleware
|
|
app.use(cors(corsConfig));
|
|
app.use(express.json());
|
|
app.set("trust proxy", 1);
|
|
app.use(session);
|
|
|
|
// Ban check middleware
|
|
app.use("/v1", (req: Request, res: Response, next: NextFunction) => {
|
|
if (req.session.user?.id && typeof req.session.user.id === "number") {
|
|
const user = req.session.user as { banned?: boolean };
|
|
if (user.banned) {
|
|
req.session.destroy(() => {});
|
|
res.status(403).json({ message: "Your account has been banned." });
|
|
return;
|
|
}
|
|
}
|
|
next();
|
|
});
|
|
|
|
app.use("/v1", routes);
|
|
|
|
// socket.io
|
|
export const io = new Server(server, { cors: corsConfig, pingInterval: 30000, pingTimeout: 50000 });
|
|
io.use((socket, next) => {
|
|
session(socket.request as Request, {} as Response, next as NextFunction);
|
|
});
|
|
io.use((socket, next) => {
|
|
const sess = socket.request.session;
|
|
if (sess && sess.user) {
|
|
next();
|
|
} else {
|
|
console.log("io.use: no session");
|
|
socket.disconnect();
|
|
}
|
|
});
|
|
initSocket();
|
|
|
|
const port = process.env.PORT || 3001;
|
|
server.listen(port, () => {
|
|
console.log(`MiChess API server listening on :${port}`);
|
|
});
|