feat: add TypeScript server skeleton
This commit is contained in:
14
src/index.ts
Normal file
14
src/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { createServer } from "node:http";
|
||||
import { env } from "./shared/env.js";
|
||||
import { createRequestHandler } from "./server/request-handler.js";
|
||||
|
||||
const handler = createRequestHandler();
|
||||
|
||||
const server = createServer((req, res) => {
|
||||
void handler(req, res);
|
||||
});
|
||||
|
||||
server.listen(env.port, env.host, () => {
|
||||
console.log(`mapy-mg listening on http://${env.host}:${env.port}`);
|
||||
});
|
||||
|
||||
21
src/server/request-handler.ts
Normal file
21
src/server/request-handler.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
|
||||
export function createRequestHandler() {
|
||||
return async function handleRequest(_req: IncomingMessage, res: ServerResponse) {
|
||||
res.statusCode = 200;
|
||||
res.setHeader("content-type", "application/json; charset=utf-8");
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
name: "mapy-mg",
|
||||
status: "ok",
|
||||
features: [
|
||||
"photo upload",
|
||||
"EXIF location extraction",
|
||||
"map markers",
|
||||
"route preview"
|
||||
]
|
||||
})
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
15
src/shared/env.ts
Normal file
15
src/shared/env.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
function parsePort(value: string | undefined): number {
|
||||
const port = Number(value ?? "3000");
|
||||
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
throw new Error("PORT must be an integer between 1 and 65535");
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
export const env = {
|
||||
host: process.env.HOST ?? "0.0.0.0",
|
||||
port: parsePort(process.env.PORT)
|
||||
} as const;
|
||||
|
||||
16
tsconfig.json
Normal file
16
tsconfig.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user