Skip to content

Commit

Permalink
Renames
Browse files Browse the repository at this point in the history
Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
  • Loading branch information
olehermanse committed May 25, 2024
1 parent 23f935d commit 0607845
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 331 deletions.
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:20 AS build
WORKDIR /towers
WORKDIR /trpg
COPY package-lock.json package.json ./
RUN npm install --only=prod
COPY .git .git
Expand All @@ -14,15 +14,15 @@ RUN npm run build
RUN bash add_version.sh

FROM node:20 AS test
WORKDIR /towers
COPY --from=build /towers /towers
WORKDIR /trpg
COPY --from=build /trpg /trpg
COPY test test
RUN npm install
RUN npm run test

FROM denoland/deno:1.34.3 AS run
WORKDIR /towers
COPY --from=build /towers/dist/ dist/
WORKDIR /trpg
COPY --from=build /trpg/dist/ dist/
COPY src/ src/
COPY --from=test /towers/package.json /towers/package.json
COPY --from=test /trpg/package.json /trpg/package.json
CMD [ "deno" , "run", "--allow-net", "--allow-read", "--allow-env", "src/backend/backend.ts"]
22 changes: 3 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
# Towers
# trpg

https://towers.oleherman.com

## Development server

Start the development server:
https://trpg.oleherman.com

```
node server.js
docker build --tag trpg . && docker run -it -p 3000:3000 --name trpg --rm trpg
```

For development purposes it defaults to port 3000:

http://127.0.0.1:3000

## Containers

### docker

```
docker build --tag towers . && docker run -it -p 3000:3000 --name towers --rm towers
```

http://127.0.0.1:80
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Towers</title>
<title>trpg</title>
<link inline rel="stylesheet" type="text/css" href="src/styles/style.css" />
</head>
<body>
<div class="main_div">
<h1 class="heading">
<a href="https://github.com/olehermanse/towers">🏰</a>
<a href="https://github.com/olehermanse/trpg">🏰</a>
</h1>
<canvas id="towers_canvas" class="towers_canvas"></canvas>
<canvas id="trpg_canvas" class="trpg_canvas"></canvas>
<span class="version_number">
<a href="https://github.com/olehermanse/towers/commit/COMMIT_SHA">
<a href="https://github.com/olehermanse/trpg/commit/COMMIT_SHA">
Version COMMIT_DATE-COMMIT_SHA
</a>
</span>
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "towers",
"name": "trpg",
"version": "0.2.0",
"description": "https://towers.oleherman.com",
"description": "https://trpg.oleherman.com",
"main": "index.js",
"private": true,
"author": "",
"license": "MIT",
"homepage": "https://github.com/olehermanse/towers#readme",
"homepage": "https://github.com/olehermanse/trpg#readme",
"scripts": {
"build": "npm run build:vite",
"build:vite": "vite build",
Expand All @@ -15,10 +15,10 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/olehermanse/towers.git"
"url": "git+https://github.com/olehermanse/trpg.git"
},
"bugs": {
"url": "https://github.com/olehermanse/towers/issues"
"url": "https://github.com/olehermanse/trpg/issues"
},
"dependencies": {
"@olehermanse/utils": "0.0.16",
Expand Down
184 changes: 92 additions & 92 deletions src/backend/backend.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
// Start listening on port 3000 of localhost.
const server = Deno.listen({ port: 3000 });
console.log("Backend running on http://localhost:3000/");

for await (const conn of server) {
handleHttp(conn).catch(console.error);
}

function illegalURL(path) {
return (
!path.startsWith("/") ||
path.includes("..") ||
path.includes("//") ||
path.includes(" ") ||
path.includes(";") ||
path.includes(",") ||
path.includes("'") ||
path.includes('"') ||
path.includes("*")
);
}

function getContentType(path: string): string {
if (path === "/index.html") {
return "text/html";
}
if (path === "/favicon.ico") {
return "image/x-icon";
}
if (path.endsWith(".js")) {
return "application/javascript";
}
if (path.endsWith(".css")) {
return "text/css";
}
return "";
}

async function notFound(requestEvent) {
const notFoundResponse = new Response("404 Not Found", { status: 404 });
await requestEvent.respondWith(notFoundResponse);
}

async function handleAPI(requestEvent) {
await notFound(requestEvent);
}

async function handleFile(requestEvent, filepath) {
if (filepath === "/") {
filepath = "/index.html";
}

const contentType: string = getContentType(filepath);
if (contentType === "") {
await notFound(requestEvent);
return;
}

// Try opening the file
let file;
try {
file = await Deno.open(`./dist${filepath}`, { read: true });
} catch {
await notFound(requestEvent);
return;
}

const readableStream = file.readable;
const headers: HeadersInit = { "content-type": contentType };
const response = new Response(readableStream, { headers: headers });
await requestEvent.respondWith(response);
return;
}

async function handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {
const url = new URL(requestEvent.request.url);
let filepath = decodeURIComponent(url.pathname);

if (illegalURL(filepath)) {
await notFound(requestEvent);
continue;
}
if (filepath.startsWith("/api/")) {
await handleAPI(requestEvent, filepath);
continue;
}

await handleFile(requestEvent, filepath);
}
}
// Start listening on port 3000 of localhost.
const server = Deno.listen({ port: 3000 });
console.log("Backend running on http://localhost:3000/");

for await (const conn of server) {
handleHttp(conn).catch(console.error);
}

function illegalURL(path) {
return (
!path.startsWith("/") ||
path.includes("..") ||
path.includes("//") ||
path.includes(" ") ||
path.includes(";") ||
path.includes(",") ||
path.includes("'") ||
path.includes('"') ||
path.includes("*")
);
}

function getContentType(path: string): string {
if (path === "/index.html") {
return "text/html";
}
if (path === "/favicon.ico") {
return "image/x-icon";
}
if (path.endsWith(".js")) {
return "application/javascript";
}
if (path.endsWith(".css")) {
return "text/css";
}
return "";
}

async function notFound(requestEvent) {
const notFoundResponse = new Response("404 Not Found", { status: 404 });
await requestEvent.respondWith(notFoundResponse);
}

async function handleAPI(requestEvent) {
await notFound(requestEvent);
}

async function handleFile(requestEvent, filepath) {
if (filepath === "/") {
filepath = "/index.html";
}

const contentType: string = getContentType(filepath);
if (contentType === "") {
await notFound(requestEvent);
return;
}

// Try opening the file
let file;
try {
file = await Deno.open(`./dist${filepath}`, { read: true });
} catch {
await notFound(requestEvent);
return;
}

const readableStream = file.readable;
const headers: HeadersInit = { "content-type": contentType };
const response = new Response(readableStream, { headers: headers });
await requestEvent.respondWith(response);
return;
}

async function handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {
const url = new URL(requestEvent.request.url);
let filepath = decodeURIComponent(url.pathname);

if (illegalURL(filepath)) {
await notFound(requestEvent);
continue;
}
if (filepath.startsWith("/api/")) {
await handleAPI(requestEvent, filepath);
continue;
}

await handleFile(requestEvent, filepath);
}
}
Loading

0 comments on commit 0607845

Please sign in to comment.