refactor: optimize backend server codebase

main
Justin Xiao 2023-07-18 03:23:24 +08:00
parent 6da0ae0065
commit 53560add19
22 changed files with 6329 additions and 1223 deletions

View File

@ -0,0 +1,8 @@
# Environment Name
NODE_ENV=development
# Server listen to this port
PORT=3001
#Cors
CORS_URL=*

View File

@ -0,0 +1,10 @@
# .env.example
# Environment Name
NODE_ENV=development
# Server listen to this port
PORT=3000
#Cors
CORS_URL=*

View File

@ -0,0 +1,8 @@
# Environment Name
NODE_ENV=production
# Server listen to this port
PORT=3001
#Cors
CORS_URL=*

View File

@ -0,0 +1,8 @@
.vscode/
node_modules/
# Build products
build/
dist/
tools/

21
backend/.eslintrc 100644
View File

@ -0,0 +1,21 @@
{
"rules": {
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-function-return-types": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
},
"extends": [
"plugin:@typescript-eslint/recommended" // Uses the recommended rules from the @typescript-eslint/eslint-plugin
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest", // Allows for the parsing of modern ECMAScript features
"sourceType": "module" // Allows for the use of imports
},
"env": {
"node": true
}
}

38
backend/.gitignore vendored 100644
View File

@ -0,0 +1,38 @@
# Add any directories, files, or patterns you don't want to be tracked by version control
.idea
# ignore vs code project config files
.vs
# ignore logs
logs
*.log
# ignore 3rd party lib
node_modules
# ignore key
*.pem
# Ignore built files
build
# ignore test converage
coverage
# Environment varibles
*.env
*.env.test
*.env copy
#keys
keys/*
!keys/*.md
!keys/*.example
#temp
temp
.DS_Store
*.save
*.save.*

View File

@ -0,0 +1,6 @@
build/
coverage/
keys/
logs/
node_modules/
*.md

View File

@ -0,0 +1,8 @@
{
"useTabs": false,
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2
}

View File

@ -0,0 +1 @@
# Socket.io Node.js Backend Typescript Project

5828
backend/package-lock.json generated 100644

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,44 @@
{
"name": "express-socketio-demo",
"version": "1.0.0",
"description": "Socket.io Backend Typescript Project",
"main": "index.js",
"scripts": {
"dev": "npm run build && SET NODE_ENV=development&& node build/server.js",
"prod": "NODE_ENV=production npm run build && node build/server.js",
"build": "npm run clean && npm run build-ts",
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run watch-node\"",
"watch-node": "nodemon -r dotenv/config build/server.js",
"clean": "rimraf ./build",
"build-ts": "tsc",
"watch-ts": "tsc -w",
"eslint": "eslint . --ext .js,.ts"
},
"keywords": [],
"author": "Justin Xiao",
"license": "MIT",
"dependencies": {
"axios": "^1.2.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"redis": "^4.6.7",
"socket.io": "^4.7.1",
"winston": "^3.8.2",
"winston-daily-rotate-file": "^4.7.1"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.15",
"@types/node": "^18.11.17",
"@types/request": "^2.48.8",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"dotenv": "^16.0.3",
"eslint": "^8.29.0",
"nodemon": "^2.0.20",
"prettier": "^2.8.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
}

41
backend/src/app.ts 100644
View File

@ -0,0 +1,41 @@
import express, { Request, Response, NextFunction } from 'express';
import Logger from './core/Logger';
import cors from 'cors';
import { corsUrl, environment } from './config';
import {
NotFoundError,
ApiError,
InternalError,
ErrorType,
} from './core/ApiError';
process.on('uncaughtException', (e) => {
Logger.error(e);
});
const app = express();
app.use(cors({ origin: corsUrl, optionsSuccessStatus: 200 }));
// catch 404 and forward to error handler
app.use((req, res, next) => next(new NotFoundError()));
// Middleware Error Handler
// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
if (err instanceof ApiError) {
ApiError.handle(err, res);
if (err.type === ErrorType.INTERNAL)
Logger.error(
`500 - ${err.message} - ${req.originalUrl} - ${req.method} - ${req.ip}`,
);
} else {
Logger.error(
`500 - ${err.message} - ${req.originalUrl} - ${req.method} - ${req.ip}`,
);
Logger.error(err);
if (environment === 'development') return res.status(500).send(err);
ApiError.handle(new InternalError(), res);
}
});
export default app;

View File

@ -0,0 +1,5 @@
// Mapper for environment variables
export const environment = process.env.NODE_ENV;
export const port = process.env.PORT;
export const corsUrl = process.env.CORS_URL;
export const logDirectory = process.env.LOG_DIR;

View File

@ -0,0 +1,40 @@
import { Response } from 'express';
import { environment } from '../config';
import { InternalErrorResponse } from './ApiResponse';
export enum ErrorType {
INTERNAL = 'InternalError',
NOT_FOUND = 'NotFoundError',
}
export abstract class ApiError extends Error {
constructor(public type: ErrorType, public message: string = 'error') {
super(type);
}
public static handle(err: ApiError, res: Response): Response {
switch (err.type) {
case ErrorType.INTERNAL:
return new InternalErrorResponse(err.message).send(res);
case ErrorType.NOT_FOUND:
default: {
let message = err.message;
// Do not send failure message in production as it may send sensitive data
if (environment === 'production') message = 'Something wrong happened.';
return new InternalErrorResponse(message).send(res);
}
}
}
}
export class InternalError extends ApiError {
constructor(message = 'Internal error') {
super(ErrorType.INTERNAL, message);
}
}
export class NotFoundError extends ApiError {
constructor(message = 'Not Found') {
super(ErrorType.NOT_FOUND, message);
}
}

View File

@ -0,0 +1,53 @@
import { Response } from 'express';
// Helper code for the API consumer to understand the error and handle is accordingly
enum StatusCode {
SUCCESS = '10000',
FAILURE = '10001',
RETRY = '10002',
INVALID_ACCESS_TOKEN = '10003',
}
enum ResponseStatus {
SUCCESS = 200,
INTERNAL_ERROR = 500,
}
abstract class ApiResponse {
constructor(
protected statusCode: StatusCode,
protected status: ResponseStatus,
protected message: string,
) {}
protected prepare<T extends ApiResponse>(
res: Response,
response: T,
headers: { [key: string]: string },
): Response {
for (const [key, value] of Object.entries(headers)) res.append(key, value);
return res.status(this.status).json(ApiResponse.sanitize(response));
}
public send(
res: Response,
headers: { [key: string]: string } = {},
): Response {
return this.prepare<ApiResponse>(res, this, headers);
}
private static sanitize<T extends ApiResponse>(response: T): T {
const clone: T = {} as T;
Object.assign(clone, response);
// @ts-ignore
delete clone.status;
for (const i in clone) if (typeof clone[i] === 'undefined') delete clone[i];
return clone;
}
}
export class InternalErrorResponse extends ApiResponse {
constructor(message = 'Internal Error') {
super(StatusCode.FAILURE, ResponseStatus.INTERNAL_ERROR, message);
}
}

View File

@ -0,0 +1,44 @@
import { createLogger, transports, format } from 'winston';
import fs from 'fs';
import path from 'path';
import DailyRotateFile from 'winston-daily-rotate-file';
import { environment, logDirectory } from '../config';
let dir = logDirectory;
if (!dir) dir = path.resolve('logs');
// create directory if it is not present
if (!fs.existsSync(dir)) fs.mkdirSync(dir); // Create the directory if it does not exist
const logLevel = environment === 'development' ? 'debug' : 'warn';
const dailyRotateFile = new DailyRotateFile({
level: logLevel,
// @ts-ignore
filename: dir + '/%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
handleExceptions: true,
maxSize: '20m',
maxFiles: '14d',
format: format.combine(
format.errors({ stack: true }),
format.timestamp(),
format.json(),
),
});
export default createLogger({
transports: [
new transports.Console({
level: logLevel,
format: format.combine(
format.errors({ stack: true }),
format.prettyPrint(),
),
}),
dailyRotateFile,
],
exceptionHandlers: [dailyRotateFile],
exitOnError: false, // do not exit on handled exceptions
});

View File

@ -0,0 +1,60 @@
import { config } from 'dotenv';
config({ path: `.env.${process.env.NODE_ENV}` });
import Logger from './core/Logger';
import { port } from './config';
import app from './app';
import http from 'http';
import { Server } from 'socket.io';
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: '*',
},
});
const userSockets = new Map();
io.on('connection', (socket) => {
userSockets.set(socket.id, socket.id);
console.log(
'PRODUCTION SERVER: user connected, online user count:',
userSockets.size,
);
socket.on('message', (message, callback) => {
console.log('PRODUCTION SERVER: ', message);
const { from: sourceSocketId, to: targetSocketId } = message;
io.to(targetSocketId).emit('message', message);
io.to(sourceSocketId).emit('message', message);
if (callback) {
callback({
ok: true,
});
}
});
socket.on('online_user', () => {
console.log('PRODUCTION SERVER: online_user');
const onlineUsers = Array.from(userSockets.values());
io.to(socket.id).emit('online_user', onlineUsers);
});
socket.on('disconnect', () => {
userSockets.delete(socket.id);
console.log(
'PRODUCTION SERVER: user disconnected, online user count:',
userSockets.size,
);
});
});
server
.listen(port, () => {
console.log(`server running on port : ${port}`);
Logger.info(`server running on port : ${port}`);
})
.on('error', (e) => {
console.log(e);
Logger.error(e);
});

View File

@ -0,0 +1,105 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "ES2019" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "CommonJS" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"baseUrl": "." /* Specify the base directory to resolve non-relative module names. */,
"paths": {
"*": ["node_modules/*", "src/types/*"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
"resolveJsonModule": true /* Enable importing .json files. */,
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
"allowJs": false /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */,
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
"sourceMap": true /* Create source map files for emitted JavaScript files. */,
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "build" /* Specify an output folder for all emitted files. */,
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
// "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* When type checking, take into account 'null' and 'undefined'. */,
"strictFunctionTypes": false /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */,
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
"noImplicitThis": false /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
// "skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}

View File

@ -45,6 +45,7 @@ const useSocketStore = create<Store>((set, get) => {
console.log("Socket already connected", socket); console.log("Socket already connected", socket);
toast.error("Socket already connected"); toast.error("Socket already connected");
} else { } else {
console.log("Connecting to socket", SOCKET_URL);
const socket = io( const socket = io(
SOCKET_URL, SOCKET_URL,
process.env.NODE_ENV === "development" process.env.NODE_ENV === "development"

View File

@ -1,49 +0,0 @@
import express from "express";
import http from "http";
const app = express();
const server = http.createServer(app);
import { Server } from "socket.io";
const io = new Server(server, {
cors: {
origin: "*",
},
});
const userSockets = new Map();
io.on("connection", (socket) => {
console.log(socket.id);
socket.on("join", (userId) => {
userSockets.set(userId, socket.id);
console.log(userSockets);
});
socket.on("message", (message, callback) => {
console.log("PRODUCTION SERVER: ", message);
const { from: sourceSocketId, to: targetSocketId } = message;
io.to(targetSocketId).emit("message", message);
io.to(sourceSocketId).emit("message", message);
if (callback) {
callback({
ok: true,
});
}
});
socket.on("disconnect", () => {
// Remove the user ID mapping when the socket disconnects
userSockets.forEach((socketId, userId) => {
if (socketId === socket.id) {
userSockets.delete(userId);
}
});
});
socket.on("clear", () => io.emit("clear"));
});
server.listen(3001, () => {
console.log("Server listening on port 3001");
});

1154
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +0,0 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"nodemon": "^2.0.22",
"socket.io": "^4.7.1",
"uuid": "^9.0.0"
}
}