summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.idea/misc.xml6
-rw-r--r--hashes.json2
-rw-r--r--package-lock.json71
-rw-r--r--package.json1
-rw-r--r--src/api/index.js1
-rw-r--r--src/api/routes.js18
-rw-r--r--src/api/routes/index.js2
-rw-r--r--src/api/routes/indexRoute.js6
-rw-r--r--src/api/routes/statusRoute.js11
-rw-r--r--src/api/start.js40
10 files changed, 155 insertions, 3 deletions
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644

index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml
@@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="JavaScriptSettings"> + <option name="languageLevel" value="ES6" /> + </component> +</project> \ No newline at end of file diff --git a/hashes.json b/hashes.json
index 35acf15..22754ec 100644 --- a/hashes.json +++ b/hashes.json
@@ -1,3 +1,3 @@ { - "npmDepsHash": "sha256-X4ILC6lL+THrUg04KgIw7KAatCeuIgMgsYAgrLFtHjI=" + "npmDepsHash": "sha256-389fxQXojG0uXtVxA7hWFeYYslNxXbBI4K2eIIzQUzw=" } diff --git a/package-lock.json b/package-lock.json
index 8ddc880..8a4c742 100644 --- a/package-lock.json +++ b/package-lock.json
@@ -13,6 +13,7 @@ "joi": "^17.13.3", "jsonwebtoken": "^9.0.2", "mongoose": "^8.15.0", + "morgan": "^1.10.0", "ws": "^8.18.2" }, "devDependencies": { @@ -402,6 +403,24 @@ "dev": true, "license": "MIT" }, + "node_modules/basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "5.1.2" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/basic-auth/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/body-parser": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.0.tgz", @@ -1613,6 +1632,49 @@ "url": "https://opencollective.com/mongoose" } }, + "node_modules/morgan": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", + "integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==", + "license": "MIT", + "dependencies": { + "basic-auth": "~2.0.1", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-finished": "~2.3.0", + "on-headers": "~1.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/morgan/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/morgan/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/morgan/node_modules/on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/mpath": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", @@ -1680,6 +1742,15 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json
index d65d404..a4a26d7 100644 --- a/package.json +++ b/package.json
@@ -32,6 +32,7 @@ "joi": "^17.13.3", "jsonwebtoken": "^9.0.2", "mongoose": "^8.15.0", + "morgan": "^1.10.0", "ws": "^8.18.2" } } diff --git a/src/api/index.js b/src/api/index.js
index e69de29..9bf9b1b 100644 --- a/src/api/index.js +++ b/src/api/index.js
@@ -0,0 +1 @@ +export * from "./routes"; diff --git a/src/api/routes.js b/src/api/routes.js new file mode 100644
index 0000000..6229196 --- /dev/null +++ b/src/api/routes.js
@@ -0,0 +1,18 @@ +import * as routes from "./routes/index.js"; + +export function registerRoutes(app) { + // app.get("/status", routes.statusRoute); + Object.values(routes).forEach((route) => { + console.log("Registering route:", route); + if (!route.route) + throw new Error( + "Route definition is missing 'route' property: " + + JSON.stringify(route), + ); + + if (route.onGet) app.get(route.route, route.onGet); + if (route.onPost) app.post(route.route, route.onPost); + if (route.onPut) app.put(route.route, route.onPut); + if (route.onDelete) app.put(route.route, route.onDelete); + }); +} diff --git a/src/api/routes/index.js b/src/api/routes/index.js new file mode 100644
index 0000000..c35303f --- /dev/null +++ b/src/api/routes/index.js
@@ -0,0 +1,2 @@ +export * from "./statusRoute.js"; +export * from "./indexRoute.js"; diff --git a/src/api/routes/indexRoute.js b/src/api/routes/indexRoute.js new file mode 100644
index 0000000..3685893 --- /dev/null +++ b/src/api/routes/indexRoute.js
@@ -0,0 +1,6 @@ +export const indexRoute = { + route: "/", + onGet(req, res) { + res.send("What art thou doing here???"); + }, +}; diff --git a/src/api/routes/statusRoute.js b/src/api/routes/statusRoute.js new file mode 100644
index 0000000..a829f19 --- /dev/null +++ b/src/api/routes/statusRoute.js
@@ -0,0 +1,11 @@ +export const statusRoute = { + route: "/status", + onGet(req, res) { + const status = { + status: "ok", + timestamp: new Date().toISOString(), + }; + + res.status(200).json(status); + }, +}; diff --git a/src/api/start.js b/src/api/start.js
index 9d9c586..caf7a7c 100644 --- a/src/api/start.js +++ b/src/api/start.js
@@ -1,12 +1,48 @@ import express from "express"; +import morgan from "morgan"; +import { registerRoutes } from "./routes.js"; const app = express(); const PORT = process.env.PORT || 3000; -app.get("/", (req, res) => { - res.send("Welcome to the API!"); +// Configure Express +app.use(express.json()); +app.disable("x-powered-by"); +app.use((req, res, next) => { + res.set("Access-Control-Allow-Origin", "*"); + res.set( + "Content-security-policy", + "default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';", + ); + res.set( + "Access-Control-Allow-Headers", + req.header("Access-Control-Request-Headers") || "*", + ); + res.set( + "Access-Control-Allow-Methods", + req.header("Access-Control-Request-Methods") || "*", + ); + + next(); }); +const logRequests = process.env["LOG_REQUESTS"] ?? "-"; +const requestLogFormat = + ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" ":response-time ms"'; +if (logRequests) { + app.use( + morgan(requestLogFormat, { + skip: (req, res) => { + let skip = !logRequests.includes(res.statusCode); + if (logRequests.startsWith("-")) skip = !skip; + return skip; + }, + }), + ); +} + +registerRoutes(app); + app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });