some final fixes

This commit is contained in:
TopchetoEU 2023-07-01 00:49:40 +03:00
parent ef41d23771
commit fa7b92e535
6 changed files with 38 additions and 28 deletions

View File

@ -5,7 +5,30 @@ import { StaticRouter } from "./routers/StaticRouter.ts";
import { route } from "./server/decorators.ts"; import { route } from "./server/decorators.ts";
import RestRequest from "./server/RestRequest.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 { class DevelRouter extends AppRouter {
@route('api/*') api; @route('api/*') api;
@route('*') static; @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' }); const server = Deno.listen({ port: 80, hostname: '127.0.0.1' });
new DevelRouter(app.api, app.static).attach(server); 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
}));
}
});
}

View File

@ -7,15 +7,15 @@ import AppRouter from "./AppRouter.ts";
export class StaticRouter extends AppRouter { export class StaticRouter extends AppRouter {
@rest('GET', '/http-url.js') @rest('GET', '/http-url.js')
getHttpUrl() { getHttpUrl() {
return stream(`var $_url = ${this.apiUrl};`); return stream(`var $_url = "${this.apiUrl}";`);
} }
@rest('GET', '/') @route('*') static = staticHandler('static');
@rest('GET', '*')
async index() { async index() {
return new RestResponse() return new RestResponse()
.body((await Deno.open('static/index.html')).readable) .body((await Deno.open('static/index.html')).readable)
.contentType('html'); .contentType('html');
} }
@route('/*') static = staticHandler('static');
public constructor(private apiUrl: string) { public constructor(private apiUrl: string) {
super(); super();

View File

@ -6,9 +6,11 @@ export default function staticHandler(path: string): Handler {
async handle(req) { async handle(req) {
try { try {
const realPath = await Deno.realPath(`${path}/${req.url}`); 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 i = realPath.lastIndexOf('.');
const res = new RestResponse().body(stream.readable); 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)); if (i >= 0) res.contentType(realPath.substring(i + 1));
return res; return res;

View File

@ -1,6 +1,6 @@
declare function $__getHTTP(): string; declare var $_url: string;
export const environment = { export const environment = {
production: true, production: true,
apiURL: $__getHTTP(), apiURL: $_url,
}; };

View File

@ -1,4 +1,6 @@
declare var $_url: string;
export const environment = { export const environment = {
production: false, production: false,
apiURL: 'http://127.0.0.1/api', apiURL: `${$_url}/api`,
}; };

View File

@ -6,6 +6,7 @@
<base href="/"> <base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico"> <link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="/http-url.js"></script>
</head> </head>
<body> <body>
<app-root></app-root> <app-root></app-root>