clonegur/backend/main.ts
2023-07-01 01:20:20 +03:00

46 lines
1.6 KiB
TypeScript

import clonegur from "./clonegur.ts";
import { APIRouter } from "./routers/APIRouter.ts";
import AppRouter from "./routers/AppRouter.ts";
import { StaticRouter } from "./routers/StaticRouter.ts";
import { route } from "./server/decorators.ts";
import RestRequest from "./server/RestRequest.ts";
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());
}
})().catch(console.error);
}
}
else {
class DevelRouter extends AppRouter {
@route('api/*') api;
@route('*') static;
public constructor(api: APIRouter, _static: StaticRouter) {
super();
this.api = api;
this.static = _static;
}
}
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);
}