This commit is contained in:
Leandro Facchinetti 2022-11-07 19:40:58 +00:00
parent a239e33a22
commit 4794bcc2f1
1 changed files with 128 additions and 160 deletions

View File

@ -212,7 +212,7 @@ await commander.program
processType, processType,
processNumber, processNumber,
}: { }: {
processType: "main" | "server" | "worker"; processType: "main" | "web" | "email";
processNumber: string; processNumber: string;
} }
) => { ) => {
@ -241,18 +241,26 @@ await commander.program
}); });
const application = { const application = {
name: "courselore", name: "kill-the-newsletter",
version, version,
process: { process: {
id: Math.random().toString(36).slice(2), id: Math.random().toString(36).slice(2),
type: processType, type: processType,
number: number: (typeof processNumber === "string"
typeof processNumber === "string"
? Number(processNumber) ? Number(processNumber)
: undefined, : undefined) as number,
}, },
configuration: (await import(url.pathToFileURL(configuration).href)) configuration: (await import(url.pathToFileURL(configuration).href))
.default, .default as {
hostname: string;
dataDirectory: string;
administratorEmail: string;
environment: "production" | "development" | "other";
tunnel: boolean;
alternativeHostnames: string[];
hstsPreload: boolean;
caddy: string;
},
static: JSON.parse( static: JSON.parse(
await fs.readFile( await fs.readFile(
new URL("../static/paths.json", import.meta.url), new URL("../static/paths.json", import.meta.url),
@ -260,37 +268,25 @@ await commander.program
) )
), ),
ports: { ports: {
server: lodash.times( web: lodash.times(
os.cpus().length, os.cpus().length,
(processNumber) => 6000 + processNumber (processNumber) => 6000 + processNumber
), ),
serverEvents: lodash.times(
os.cpus().length,
(processNumber) => 7000 + processNumber
),
workerEvents: lodash.times(
os.cpus().length,
(processNumber) => 8000 + processNumber
),
}, },
addresses: { web: express(),
canonicalHostname: "courselore.org", email: "TODO",
metaCourseloreInvitation: "https://meta.courselore.org", };
tryHostname: "try.courselore.org",
},
server: express() as any,
serverEvents: express() as any,
workerEvents: express() as any,
} as Application;
application.configuration.environment ??= "production"; application.configuration.environment ??= "production";
application.configuration.demonstration ??=
application.configuration.environment !== "production";
application.configuration.tunnel ??= false; application.configuration.tunnel ??= false;
application.configuration.alternativeHostnames ??= []; application.configuration.alternativeHostnames ??= [];
application.configuration.hstsPreload ??= false; application.configuration.hstsPreload ??= false;
application.configuration.caddy ??= caddyfile``; application.configuration.caddy ??= caddyfile``;
application.web.get("/", (request, response) => {
response.send("TODO");
});
// #!/usr/bin/env node // #!/usr/bin/env node
// import path from "path"; // import path from "path";
@ -814,8 +810,9 @@ await commander.program
const childProcesses = new Set<ExecaChildProcess>(); const childProcesses = new Set<ExecaChildProcess>();
let restartChildProcesses = true; let restartChildProcesses = true;
for (const execaArguments of [ for (const execaArguments of [
...["server", "worker"].flatMap((processType) => ...Object.entries({ web: os.cpus().length, email: 1 }).flatMap(
lodash.times(os.cpus().length, (processNumber) => ({ ([processType, processCount]) =>
lodash.times(processCount, (processNumber) => ({
file: process.argv[0], file: process.argv[0],
arguments: [ arguments: [
process.argv[1], process.argv[1],
@ -876,9 +873,9 @@ await commander.program
} }
${[ ${[
application.configuration.tunnel ...(application.configuration.tunnel
? [] ? []
: [application.configuration.hostname], : [application.configuration.hostname]),
...application.configuration.alternativeHostnames, ...application.configuration.alternativeHostnames,
] ]
.map((hostname) => `http://${hostname}`) .map((hostname) => `http://${hostname}`)
@ -925,21 +922,7 @@ await commander.program
file_server file_server
} }
} }
route /files/* { reverse_proxy ${application.ports.web
root * ${JSON.stringify(
path.resolve(application.configuration.dataDirectory)
)}
@file_exists file
route @file_exists {
header Cache-Control "private, max-age=31536000, immutable"
@must_be_downloaded not path *.png *.jpg *.jpeg *.gif *.mp3 *.mp4 *.m4v *.ogg *.mov *.mpeg *.avi *.pdf *.txt
header @must_be_downloaded Content-Disposition attachment
@may_be_embedded_in_other_sites path *.png *.jpg *.jpeg *.gif *.mp3 *.mp4 *.m4v *.ogg *.mov *.mpeg *.avi *.pdf
header @may_be_embedded_in_other_sites Cross-Origin-Resource-Policy cross-origin
file_server
}
}
reverse_proxy ${application.ports.server
.map((port) => `127.0.0.1:${port}`) .map((port) => `127.0.0.1:${port}`)
.join(" ")} .join(" ")}
} }
@ -979,37 +962,22 @@ await commander.program
break; break;
} }
case "server": { case "web": {
const serverApplication = application.server; const webApplication = application.web;
const eventsApplication = application.serverEvents; webApplication.emit("start");
serverApplication.emit("start"); const server = webApplication.listen(
eventsApplication.emit("start"); application.ports.web[application.process.number],
const server = serverApplication.listen(
application.ports.server[application.process.number],
"127.0.0.1"
);
const events = eventsApplication.listen(
application.ports.serverEvents[application.process.number],
"127.0.0.1" "127.0.0.1"
); );
await stop; await stop;
server.close(); server.close();
events.close(); webApplication.emit("stop");
serverApplication.emit("stop");
eventsApplication.emit("stop");
break; break;
} }
case "worker": { case "email": {
const eventsApplication = application.workerEvents; // TODO
eventsApplication.emit("start");
const events = eventsApplication.listen(
application.ports.workerEvents[application.process.number],
"127.0.0.1"
);
await stop; await stop;
events.close();
eventsApplication.emit("stop");
break; break;
} }
} }