From fa7b92e535c52c28d404e2fe23b41b6802782940 Mon Sep 17 00:00:00 2001
From: topchetoeu <36534413+TopchetoEU@users.noreply.github.com>
Date: Sat, 1 Jul 2023 00:49:40 +0300
Subject: [PATCH] some final fixes
---
backend/main.ts | 45 ++++++++++---------
backend/routers/StaticRouter.ts | 6 +--
backend/server/staticHandler.ts | 6 ++-
frontend/src/environments/environment.prod.ts | 4 +-
frontend/src/environments/environment.ts | 4 +-
frontend/src/index.html | 1 +
6 files changed, 38 insertions(+), 28 deletions(-)
diff --git a/backend/main.ts b/backend/main.ts
index 39998ab..99c05c1 100644
--- a/backend/main.ts
+++ b/backend/main.ts
@@ -5,7 +5,30 @@ import { StaticRouter } from "./routers/StaticRouter.ts";
import { route } from "./server/decorators.ts";
import RestRequest from "./server/RestRequest.ts";
-if (Deno.args[0] === "devel") {
+if (Deno.args[0] === "prod") {
+ const api = Deno.args[1] ?? 'api.clonegur.topcheto.eu';
+ const stt = Deno.args[2] ?? 'clonegur.topcheto.eu';
+ const app = await clonegur(`http://${api}/`);
+
+ const server = Deno.listen({ port: 80 });
+
+ for await (const conn of server) {
+ (async () => {
+ for await (const req of Deno.serveHttp(conn)) {
+ const url = new URL(req.request.url);
+ let r;
+ if (url.hostname === api) r = await app.api.handle(RestRequest.fromMessage(req));
+ else if (url.hostname === stt) r = await app.static.handle(RestRequest.fromMessage(req));
+
+ if (!r) req.respondWith(new Response("nope :/", {
+ status: 404
+ }));
+ else req.respondWith(r.toFetchResponse());
+ }
+ })();
+ }
+}
+else {
class DevelRouter extends AppRouter {
@route('api/*') api;
@route('*') static;
@@ -17,25 +40,7 @@ if (Deno.args[0] === "devel") {
}
}
- const app = await clonegur('http://localhost:80/');
+ const app = await clonegur('http://localhost:80/api/');
const server = Deno.listen({ port: 80, hostname: '127.0.0.1' });
new DevelRouter(app.api, app.static).attach(server);
-}
-else if (Deno.args[0] === "prod") {
- const api = Deno.args[1] ?? 'api.clonegur.topcheto.eu';
- const stt = Deno.args[2] ?? 'clonegur.topcheto.eu';
- const app = await clonegur('https://:80/');
-
- const server = Deno.listenTls({ port: 80, hostname: '127.0.0.1' });
-
- server.accept().then(async conn => {
- for await (const req of Deno.serveHttp(conn)) {
- const url = new URL(req.request.url);
- if (url.hostname === api) app.api.handle(RestRequest.fromMessage(req));
- else if (url.hostname === stt) app.static.handle(RestRequest.fromMessage(req));
- else req.respondWith(new Response("nope :/", {
- status: 404
- }));
- }
- });
}
\ No newline at end of file
diff --git a/backend/routers/StaticRouter.ts b/backend/routers/StaticRouter.ts
index 4f8353b..55ebce1 100644
--- a/backend/routers/StaticRouter.ts
+++ b/backend/routers/StaticRouter.ts
@@ -7,15 +7,15 @@ import AppRouter from "./AppRouter.ts";
export class StaticRouter extends AppRouter {
@rest('GET', '/http-url.js')
getHttpUrl() {
- return stream(`var $_url = ${this.apiUrl};`);
+ return stream(`var $_url = "${this.apiUrl}";`);
}
- @rest('GET', '/')
+ @route('*') static = staticHandler('static');
+ @rest('GET', '*')
async index() {
return new RestResponse()
.body((await Deno.open('static/index.html')).readable)
.contentType('html');
}
- @route('/*') static = staticHandler('static');
public constructor(private apiUrl: string) {
super();
diff --git a/backend/server/staticHandler.ts b/backend/server/staticHandler.ts
index 3126a35..0764dbe 100644
--- a/backend/server/staticHandler.ts
+++ b/backend/server/staticHandler.ts
@@ -6,10 +6,12 @@ export default function staticHandler(path: string): Handler {
async handle(req) {
try {
const realPath = await Deno.realPath(`${path}/${req.url}`);
- const stream = await Deno.open(realPath);
+ const stream = await Deno.open(realPath, { read: true });
const i = realPath.lastIndexOf('.');
const res = new RestResponse().body(stream.readable);
-
+ await stream.read(new Uint8Array(1));
+ await stream.seek(0, Deno.SeekMode.Start);
+
if (i >= 0) res.contentType(realPath.substring(i + 1));
return res;
}
diff --git a/frontend/src/environments/environment.prod.ts b/frontend/src/environments/environment.prod.ts
index 1632d7c..4030cd4 100644
--- a/frontend/src/environments/environment.prod.ts
+++ b/frontend/src/environments/environment.prod.ts
@@ -1,6 +1,6 @@
-declare function $__getHTTP(): string;
+declare var $_url: string;
export const environment = {
production: true,
- apiURL: $__getHTTP(),
+ apiURL: $_url,
};
diff --git a/frontend/src/environments/environment.ts b/frontend/src/environments/environment.ts
index cdc440f..0ad1150 100644
--- a/frontend/src/environments/environment.ts
+++ b/frontend/src/environments/environment.ts
@@ -1,4 +1,6 @@
+declare var $_url: string;
+
export const environment = {
production: false,
- apiURL: 'http://127.0.0.1/api',
+ apiURL: `${$_url}/api`,
};
diff --git a/frontend/src/index.html b/frontend/src/index.html
index b20aaf4..c88b2dc 100644
--- a/frontend/src/index.html
+++ b/frontend/src/index.html
@@ -6,6 +6,7 @@