]> git.neb.cc Git - cc.git/commitdiff
Initial commit master
authorBen Larson <[email protected]>
Sat, 9 Apr 2022 02:14:10 +0000 (21:14 -0500)
committerBen Larson <[email protected]>
Sat, 9 Apr 2022 02:20:06 +0000 (21:20 -0500)
Dockerfile [new file with mode: 0644]
README.md [new file with mode: 0644]
package-lock.json [new file with mode: 0644]
package.json [new file with mode: 0644]
run.sh [new file with mode: 0755]
server.js [new file with mode: 0644]
src/index.js [new file with mode: 0644]
src/index.test.js [new file with mode: 0644]

diff --git a/Dockerfile b/Dockerfile
new file mode 100644 (file)
index 0000000..5678ebd
--- /dev/null
@@ -0,0 +1,9 @@
+FROM node:16-slim
+WORKDIR /app
+COPY ./package.json ./package.json
+RUN npm install --only=production
+COPY . .
+EXPOSE 80
+ENV PORT=80
+
+CMD [ "npm", "run", "start" ]
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..3773bf6
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# cc
diff --git a/package-lock.json b/package-lock.json
new file mode 100644 (file)
index 0000000..354fc15
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "name": "cc",
+  "version": "1.0.0",
+  "lockfileVersion": 2,
+  "requires": true,
+  "packages": {
+    "": {
+      "version": "1.0.0",
+      "license": "ISC"
+    }
+  }
+}
diff --git a/package.json b/package.json
new file mode 100644 (file)
index 0000000..0937dbc
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "name": "cc",
+  "version": "1.0.0",
+  "description": "",
+  "main": "src/index.js",
+  "scripts": {
+    "start": "node server.js",
+    "test": "node src/index.test.js"
+  },
+  "author": "",
+  "license": "ISC"
+}
diff --git a/run.sh b/run.sh
new file mode 100755 (executable)
index 0000000..7043480
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,6 @@
+NAME=cc
+docker stop $NAME-container
+docker rm $NAME-container
+docker build -t $NAME-image .
+docker run -ditp $1:80 --name $NAME-container $NAME-image
+echo "\nRunning at http://localhost:$1/\n"
diff --git a/server.js b/server.js
new file mode 100644 (file)
index 0000000..9562353
--- /dev/null
+++ b/server.js
@@ -0,0 +1,14 @@
+const http = require("http");
+const { app } = require("./src");
+
+const handler = (request, response) => {
+  const { status = 200, body = "", headers = {} } = app(request);
+
+  response.writeHead(status, headers);
+  response.write(body);
+  response.end();
+};
+
+http.createServer(handler).listen(80);
+
+console.log("Node.js web server is running..");
diff --git a/src/index.js b/src/index.js
new file mode 100644 (file)
index 0000000..30794f5
--- /dev/null
@@ -0,0 +1,21 @@
+const redirectTo = ({ to }) => ({
+  status: 301,
+  headers: { location: to },
+});
+
+const match = (url1, url2) =>
+  url1 === url2 || url1 + "/" === url2 || url1 === url2 + "/";
+
+const redirects = [
+  { from: "/", to: "https://www.benlarson.xyz/" },
+  { from: "/so", to: "https://stackoverflow.com/users/10377586/ben-larson" },
+];
+
+module.exports = {
+  app: ({ url }) => {
+    const redirect = redirects.find(({ from }) => match(url, from));
+    if (redirect) return redirectTo(redirect);
+
+    return { status: 404 };
+  },
+};
diff --git a/src/index.test.js b/src/index.test.js
new file mode 100644 (file)
index 0000000..3dd5d25
--- /dev/null
@@ -0,0 +1,29 @@
+const { app } = require(".");
+
+const it = (assertion, callback) => {
+  try {
+    callback();
+  } catch (error) {
+    console.log(`\n${assertion}\n-> ${error}\n`);
+  }
+};
+
+const assertEqual = (actual, expected) => {
+  if (actual !== expected) throw `expected ${expected} but got ${actual}`;
+};
+
+it("returns 404", () => {
+  const { status } = app({ url: "/nothing-here" });
+
+  assertEqual(status, 404);
+});
+
+it("redirects to benlarson.xyz", () => {
+  const {
+    status,
+    headers: { location },
+  } = app({ url: "/" });
+
+  assertEqual(status, 301);
+  assertEqual(location, "https://www.benlarson.xyz/");
+});