inital commit
This commit is contained in:
11
packages/server/package.json
Normal file
11
packages/server/package.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "server",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts"
|
||||
}
|
11
packages/server/src/index.ts
Normal file
11
packages/server/src/index.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { badRequest } from "./messages/badRequest";
|
||||
import { unknownRoute } from "./messages/unknownRoute";
|
||||
|
||||
export const RouteMessages = {
|
||||
unknownRoute: unknownRoute,
|
||||
badRequest: badRequest,
|
||||
};
|
||||
|
||||
export * from "./route/route";
|
||||
export * from "./route/routeManager";
|
||||
export * from "./server/server";
|
10
packages/server/src/messages/badRequest.ts
Normal file
10
packages/server/src/messages/badRequest.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { baseMessage } from "./baseMessage";
|
||||
|
||||
/**
|
||||
* Creates a response for a bad request
|
||||
*
|
||||
* @returns the bad request message
|
||||
*/
|
||||
export function badRequest(message: string) {
|
||||
return baseMessage(true, message);
|
||||
}
|
13
packages/server/src/messages/baseMessage.ts
Normal file
13
packages/server/src/messages/baseMessage.ts
Normal file
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Creates a base message for use in JSON responses
|
||||
*
|
||||
* @param error the error status
|
||||
* @param message the message to show
|
||||
* @returns the base message
|
||||
*/
|
||||
export function baseMessage(error: boolean, message: string) {
|
||||
return {
|
||||
error: error,
|
||||
message: message,
|
||||
};
|
||||
}
|
10
packages/server/src/messages/unknownRoute.ts
Normal file
10
packages/server/src/messages/unknownRoute.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { baseMessage } from "./baseMessage";
|
||||
|
||||
/**
|
||||
* Creates a response for an unknown route
|
||||
*
|
||||
* @returns the unknown route message
|
||||
*/
|
||||
export function unknownRoute() {
|
||||
return baseMessage(true, "Unknown route");
|
||||
}
|
34
packages/server/src/route/route.ts
Normal file
34
packages/server/src/route/route.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
type RouteType = {
|
||||
/**
|
||||
* The path to handle
|
||||
*/
|
||||
path: string;
|
||||
};
|
||||
|
||||
export abstract class Route {
|
||||
private path: string;
|
||||
|
||||
constructor({ path }: RouteType) {
|
||||
this.path = path;
|
||||
console.log(`Created route handler for ${path}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the incoming request
|
||||
*
|
||||
* @param req the request
|
||||
* @param res the response
|
||||
*/
|
||||
abstract handle(req: Request, res: Response): void;
|
||||
|
||||
/**
|
||||
* Get the path of the route
|
||||
*
|
||||
* @returns the path
|
||||
*/
|
||||
getPath() {
|
||||
return this.path;
|
||||
}
|
||||
}
|
33
packages/server/src/route/routeManager.ts
Normal file
33
packages/server/src/route/routeManager.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { Route } from "./route";
|
||||
|
||||
export class RouteManager {
|
||||
private routes: Route[] = [];
|
||||
|
||||
/**
|
||||
* Add a route to the route manager
|
||||
*
|
||||
* @param route the route to add
|
||||
*/
|
||||
addRoute(route: Route) {
|
||||
this.routes.push(route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a route by path
|
||||
*
|
||||
* @param path the path to get the route for
|
||||
* @returns the route or undefined if not found
|
||||
*/
|
||||
getRoute(path: string) {
|
||||
return this.routes.find((route) => route.getPath() === path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all routes
|
||||
*
|
||||
* @returns all routes
|
||||
*/
|
||||
getRoutes() {
|
||||
return this.routes;
|
||||
}
|
||||
}
|
38
packages/server/src/server/server.ts
Normal file
38
packages/server/src/server/server.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import express from "express";
|
||||
|
||||
type ServerType = {
|
||||
/**
|
||||
* The port to listen on
|
||||
* @default 3000
|
||||
*/
|
||||
port: number | string;
|
||||
|
||||
onLoaded?: () => void;
|
||||
};
|
||||
|
||||
export function createServer({ port = 3000, onLoaded }: ServerType) {
|
||||
if (typeof port === "string") {
|
||||
port = Number(port);
|
||||
if (isNaN(port)) {
|
||||
throw new Error("Port must be a number");
|
||||
}
|
||||
}
|
||||
const server = express();
|
||||
|
||||
// Enable JSON parsing
|
||||
server.use(express.json());
|
||||
|
||||
// Remove the X-Powered-By header
|
||||
server.disable("x-powered-by");
|
||||
|
||||
// Turn on ETag support (for caching)
|
||||
server.enable("etag");
|
||||
|
||||
// Listen on the specified port
|
||||
server.listen(port, () => {
|
||||
console.log(`Server listening on http://localhost:${port}`);
|
||||
onLoaded && onLoaded(); // Call the onLoaded callback if it exists
|
||||
});
|
||||
|
||||
return server;
|
||||
}
|
7
packages/server/tsconfig.json
Normal file
7
packages/server/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "tsconfig/server.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user