From 8e803cdd0346aba4eafab590cf5002c9f4bbdba6 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 22 Feb 2024 11:55:32 +0100 Subject: [PATCH] Split of server file --- scripts/osm2pgsql/tilecountServer.ts | 113 +-------------------------- scripts/server.ts | 112 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 112 deletions(-) create mode 100644 scripts/server.ts diff --git a/scripts/osm2pgsql/tilecountServer.ts b/scripts/osm2pgsql/tilecountServer.ts index 607adfb3ac..6f92f6e8e1 100644 --- a/scripts/osm2pgsql/tilecountServer.ts +++ b/scripts/osm2pgsql/tilecountServer.ts @@ -1,6 +1,6 @@ import { Client } from "pg" -import http from "node:http" import { Tiles } from "../../src/Models/TileRange" +import { Server } from "../server" /** * Just the OSM2PGSL default database @@ -160,117 +160,6 @@ class CachedSqlCount { } } -class Server { - constructor( - port: number, - options: { - ignorePathPrefix?: string[] - }, - handle: { - mustMatch: string | RegExp - mimetype: string - handle: (path: string) => Promise - }[] - ) { - handle.push({ - mustMatch: "", - mimetype: "text/html", - handle: async () => { - return `Supported endpoints are ` - }, - }) - http.createServer(async (req: http.IncomingMessage, res) => { - try { - console.log( - req.method + " " + req.url, - "from:", - req.headers.origin, - new Date().toISOString() - ) - - const url = new URL(`http://127.0.0.1/` + req.url) - let path = url.pathname - while (path.startsWith("/")) { - path = path.substring(1) - } - if (options?.ignorePathPrefix) { - for (const toIgnore of options.ignorePathPrefix) { - if (path.startsWith(toIgnore)) { - path = path.substring(toIgnore.length + 1) - break - } - } - } - const handler = handle.find((h) => { - if (typeof h.mustMatch === "string") { - return h.mustMatch === path - } - if (path.match(h.mustMatch)) { - return true - } - }) - - if (handler === undefined || handler === null) { - res.writeHead(404, { "Content-Type": "text/html" }) - res.write("

Not found...

") - res.end() - return - } - - res.setHeader( - "Access-Control-Allow-Headers", - "Origin, X-Requested-With, Content-Type, Accept" - ) - res.setHeader("Access-Control-Allow-Origin", req.headers.origin ?? "*") - if (req.method === "OPTIONS") { - res.setHeader( - "Access-Control-Allow-Methods", - "POST, GET, OPTIONS, DELETE, UPDATE" - ) - res.writeHead(204, { "Content-Type": handler.mimetype }) - res.end() - return - } - if (req.method === "POST" || req.method === "UPDATE") { - return - } - - if (req.method === "DELETE") { - return - } - - try { - const result = await handler.handle(path) - res.writeHead(200, { "Content-Type": handler.mimetype }) - res.write(result) - res.end() - } catch (e) { - console.error("Could not handle request:", e) - res.writeHead(500) - res.write(e) - res.end() - } - } catch (e) { - console.error("FATAL:", e) - res.end() - } - }).listen(port) - console.log( - "Server is running on port " + port, - ". Supported endpoints are: " + handle.map((h) => h.mustMatch).join(", ") - ) - } -} - const connectionString = "postgresql://user:password@localhost:5444/osm-poi" const tcs = new OsmPoiDatabase(connectionString) const withCache = new CachedSqlCount(tcs, 60 * 60 * 24) diff --git a/scripts/server.ts b/scripts/server.ts new file mode 100644 index 0000000000..8c036721cc --- /dev/null +++ b/scripts/server.ts @@ -0,0 +1,112 @@ +import http from "node:http" + +export class Server { + constructor( + port: number, + options: { + ignorePathPrefix?: string[] + }, + handle: { + mustMatch: string | RegExp + mimetype: string + handle: (path: string) => Promise + }[] + ) { + handle.push({ + mustMatch: "", + mimetype: "text/html", + handle: async () => { + return `Supported endpoints are
    ${handle + .filter((h) => h.mustMatch !== "") + .map((h) => { + let l = h.mustMatch + if (typeof h.mustMatch === "string") { + l = `${l}` + } + return "
  • " + l + "
  • " + }) + .join("")}
` + }, + }) + http.createServer(async (req: http.IncomingMessage, res) => { + try { + console.log( + req.method + " " + req.url, + "from:", + req.headers.origin, + new Date().toISOString() + ) + + const url = new URL(`http://127.0.0.1/` + req.url) + let path = url.pathname + while (path.startsWith("/")) { + path = path.substring(1) + } + if (options?.ignorePathPrefix) { + for (const toIgnore of options.ignorePathPrefix) { + if (path.startsWith(toIgnore)) { + path = path.substring(toIgnore.length + 1) + break + } + } + } + const handler = handle.find((h) => { + if (typeof h.mustMatch === "string") { + return h.mustMatch === path + } + if (path.match(h.mustMatch)) { + return true + } + }) + + if (handler === undefined || handler === null) { + res.writeHead(404, { "Content-Type": "text/html" }) + res.write("

Not found...

") + res.end() + return + } + + res.setHeader( + "Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept" + ) + res.setHeader("Access-Control-Allow-Origin", req.headers.origin ?? "*") + if (req.method === "OPTIONS") { + res.setHeader( + "Access-Control-Allow-Methods", + "POST, GET, OPTIONS, DELETE, UPDATE" + ) + res.writeHead(204, { "Content-Type": handler.mimetype }) + res.end() + return + } + if (req.method === "POST" || req.method === "UPDATE") { + return + } + + if (req.method === "DELETE") { + return + } + + try { + const result = await handler.handle(path) + res.writeHead(200, { "Content-Type": handler.mimetype }) + res.write(result) + res.end() + } catch (e) { + console.error("Could not handle request:", e) + res.writeHead(500) + res.write(e) + res.end() + } + } catch (e) { + console.error("FATAL:", e) + res.end() + } + }).listen(port) + console.log( + "Server is running on port " + port, + ". Supported endpoints are: " + handle.map((h) => h.mustMatch).join(", ") + ) + } +}