some final fixes
This commit is contained in:
parent
ef41d23771
commit
fa7b92e535
@ -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
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
@ -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();
|
||||
|
@ -6,9 +6,11 @@ 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;
|
||||
|
@ -1,6 +1,6 @@
|
||||
declare function $__getHTTP(): string;
|
||||
declare var $_url: string;
|
||||
|
||||
export const environment = {
|
||||
production: true,
|
||||
apiURL: $__getHTTP(),
|
||||
apiURL: $_url,
|
||||
};
|
||||
|
@ -1,4 +1,6 @@
|
||||
declare var $_url: string;
|
||||
|
||||
export const environment = {
|
||||
production: false,
|
||||
apiURL: 'http://127.0.0.1/api',
|
||||
apiURL: `${$_url}/api`,
|
||||
};
|
||||
|
@ -6,6 +6,7 @@
|
||||
<base href="/">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
<script src="/http-url.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
|
Loading…
Reference in New Issue
Block a user