Page MenuHomeSealhub

http.ts
No OneTemporary

import { Context, default as Koa } from "koa";
import Static from "koa-static";
import Router from "@koa/router";
import { Server } from "http";
import mount from "koa-mount";
import installQS from "koa-qs";
import handleError from "./handle-error";
import { App } from "../main";
export default class HttpServer {
name: "www-serer";
private server: Server;
koa: Koa;
router: Router;
config: {
port: number;
"session-cookie-name": string;
"max-payload-bytes": number;
"api-base": string;
};
constructor(public app: App) {
this.koa = new Koa();
installQS(this.koa);
this.koa.context.$app = this.app;
this.router = new Router();
// const rest_url_base = this.config["api-base"];
}
async start() {
this.koa.use(handleError());
this.koa.use(this.router.routes());
this.config = this.app.ConfigManager.get("www-server");
this.server = this.koa.listen(this.config.port);
this.app.Logger.info(
"STARTED",
`App running. URL set in manifest: ${this.app.manifest.base_url}`
);
}
async stop() {
this.server.close();
}
addStaticRoute(url_path: string, local_path: string) {
this.koa.use(
mount(
url_path,
Static(local_path, {
maxage: 1000 * 60 * 60 * 24 * 15,
defer: true,
})
)
);
this.koa.use(
mount(url_path, async function (ctx: Context, next) {
ctx.set("ETag", `W/"${ctx.URL.toString()}"`);
ctx.set("cache-control", "public, max-age=2592000");
if (
ctx.response.header.etag &&
ctx.request.headers["if-none-match"] ==
ctx.response.header.etag
) {
ctx.status = 304;
return;
}
await next();
})
);
}
}

File Metadata

Mime Type
text/x-java
Expires
Fri, Nov 28, 15:07 (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1052142
Default Alt Text
http.ts (1 KB)

Event Timeline