45 lines
1.0 KiB
JavaScript
45 lines
1.0 KiB
JavaScript
const HTTPServer = require("./_http");
|
|
const { redirectPage } = HTTPServer;
|
|
|
|
function logger (...p) {
|
|
return;
|
|
console.log("[DEBUG.http.redirector]:", ...p);
|
|
}
|
|
|
|
module.exports = class HTTPRedirector extends HTTPServer {
|
|
static type () {
|
|
return "redirect";
|
|
}
|
|
|
|
constructor (adr, syntaxTree) {
|
|
super(adr, syntaxTree);
|
|
}
|
|
|
|
run () {
|
|
this.preinit();
|
|
const redirectTo = this.syntax.target ?? "https://example.org";
|
|
|
|
this.app.use("*", async (req, res) => {
|
|
logger("originalUrl:>", req.originalUrl);
|
|
const currentDomain = req.hostname;
|
|
logger("hostname:", currentDomain);
|
|
const isNotDomained = !this.syntax.domains[currentDomain];
|
|
if (!isNotDomained) {
|
|
return this
|
|
.callDomain(currentDomain, this.syntax.domains[currentDomain])
|
|
.argv(this.syntax.domains[currentDomain].Type.type(), req, res);
|
|
}
|
|
return redirectPage(redirectTo, req, res);
|
|
});
|
|
|
|
this.postinit((err) => {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
console.log(`HTTP redirect server listened at ${
|
|
this.adr.address
|
|
}:${this.adr.port}`);
|
|
});
|
|
}
|
|
};
|