npm workspaces, NodeNext for server
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Request, Response } from "express";
|
||||
import type { User } from "@types";
|
||||
import type { User } from "@chessu/types";
|
||||
import xss from "xss";
|
||||
|
||||
export const getCurrentSession = async (req: Request, res: Response) => {
|
||||
|
||||
@@ -1,30 +1,28 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { activeGames } from "../db/models/game.model";
|
||||
import type { Game, User } from "@types";
|
||||
import { activeGames } from "../db/models/game.model.js";
|
||||
import type { Game, User } from "@chessu/types";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
export const getActiveGames = async (req: Request, res: Response) => {
|
||||
try {
|
||||
//if (!req.query || !req.query.code) {
|
||||
res.status(200).json(activeGames);
|
||||
res.status(200).json(activeGames.filter((g) => !g.unlisted));
|
||||
//}
|
||||
|
||||
/*
|
||||
// todo: if code query is URL, convert to code (or do it on client side?)
|
||||
const code =
|
||||
(req.query.code as string).startsWith("http") ||
|
||||
(req.query.code as string).startsWith("ches.su")
|
||||
? path.posix.basename(url.parse(req.query.code as string).pathname as string)
|
||||
: req.query.code;
|
||||
// const code =
|
||||
// (req.query.code as string).startsWith("http") ||
|
||||
// (req.query.code as string).startsWith("ches.su")
|
||||
// ? path.posix.basename(url.parse(req.query.code as string).pathname as string)
|
||||
// : req.query.code;
|
||||
|
||||
console.log(code);
|
||||
const game = activeGames.find((g) => g.code === code);
|
||||
// console.log(code);
|
||||
// const game = activeGames.find((g) => g.code === code);
|
||||
|
||||
if (!game) {
|
||||
res.status(404).end();
|
||||
} else {
|
||||
res.status(200).json(game);
|
||||
}*/
|
||||
// if (!game) {
|
||||
// res.status(404).end();
|
||||
// } else {
|
||||
// res.status(200).json(game);
|
||||
// }
|
||||
} catch (err: unknown) {
|
||||
console.log(err);
|
||||
res.status(500).end();
|
||||
@@ -40,9 +38,10 @@ export const createGame = async (req: Request, res: Response) => {
|
||||
return;
|
||||
}
|
||||
const user: User = req.session.user;
|
||||
const unlisted: boolean = req.body.unlisted ?? false;
|
||||
const game: Game = {
|
||||
code: nanoid(6),
|
||||
open: true,
|
||||
unlisted,
|
||||
host: user
|
||||
};
|
||||
if (req.body.side === "white") {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import { Pool } from "pg";
|
||||
import pg from "pg";
|
||||
|
||||
export const db = new Pool();
|
||||
export const db = new pg.Pool();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { db } from "..";
|
||||
import { Game } from "@types";
|
||||
import { db } from "../index.js";
|
||||
import type { Game } from "@chessu/types";
|
||||
|
||||
export const activeGames: Array<Game> = [];
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { db } from "..";
|
||||
import type { User } from "@types";
|
||||
import { db } from "../index.js";
|
||||
import type { User } from "@chessu/types";
|
||||
|
||||
const create = async (user: User, password: string) => {
|
||||
if (user.name === "Guest" || user.email === undefined) {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
import session, { Session } from "express-session";
|
||||
import type { Session } from "express-session";
|
||||
import session from "express-session";
|
||||
import PGSimple from "connect-pg-simple";
|
||||
import { db } from "../db";
|
||||
import { db } from "../db/index.js";
|
||||
import type { User } from "@chessu/types";
|
||||
|
||||
const PGSession = PGSimple(session);
|
||||
|
||||
import type { User } from "@types";
|
||||
declare module "express-session" {
|
||||
interface SessionData {
|
||||
user: User;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Router } from "express";
|
||||
const router = Router();
|
||||
import * as controller from "../controllers/auth.controller.js";
|
||||
|
||||
import * as controller from "../controllers/auth.controller";
|
||||
const router = Router();
|
||||
|
||||
router.route("/").get(controller.getCurrentSession);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Router } from "express";
|
||||
const router = Router();
|
||||
import * as controller from "../controllers/games.controller.js";
|
||||
|
||||
import * as controller from "../controllers/games.controller";
|
||||
const router = Router();
|
||||
|
||||
router.route("/").get(controller.getActiveGames).post(controller.createGame);
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Router } from "express";
|
||||
const router = Router();
|
||||
import games from "./games.route.js";
|
||||
import auth from "./auth.route.js";
|
||||
|
||||
import games from "./games.route";
|
||||
import auth from "./auth.route";
|
||||
const router = Router();
|
||||
|
||||
router.use("/games", games);
|
||||
router.use("/auth", auth);
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import "dotenv/config";
|
||||
import cors from "cors";
|
||||
const corsConfig = { origin: "https://ches.su", credentials: true };
|
||||
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
import express from "express";
|
||||
import { createServer } from "http";
|
||||
import session from "./middleware/session";
|
||||
import session from "./middleware/session.js";
|
||||
import { Server } from "socket.io";
|
||||
import { init as initSocket } from "./socket";
|
||||
import { db } from "./db";
|
||||
import { init as initSocket } from "./socket/index.js";
|
||||
import { db } from "./db/index.js";
|
||||
import routes from "./routes/index.js";
|
||||
|
||||
import routes from "./routes";
|
||||
const corsConfig = { origin: "https://ches.su", credentials: true };
|
||||
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { activeGames } from "../db/models/game.model";
|
||||
import { activeGames } from "../db/models/game.model.js";
|
||||
import type { Socket } from "socket.io";
|
||||
import { Chess } from "chess.js";
|
||||
import { io } from "../server";
|
||||
import { io } from "../server.js";
|
||||
|
||||
export async function joinLobby(this: Socket, gameCode: string) {
|
||||
const game = activeGames.find((g) => g.code === gameCode);
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import type { Socket } from "socket.io";
|
||||
import { io } from "../server";
|
||||
import { joinLobby, leaveLobby, getLatestGame, sendMove, joinAsPlayer, chat } from "./game.socket";
|
||||
import { io } from "../server.js";
|
||||
import {
|
||||
joinLobby,
|
||||
leaveLobby,
|
||||
getLatestGame,
|
||||
sendMove,
|
||||
joinAsPlayer,
|
||||
chat
|
||||
} from "./game.socket.js";
|
||||
|
||||
const socketConnect = (socket: Socket) => {
|
||||
const req = socket.request;
|
||||
|
||||
Reference in New Issue
Block a user