This commit is contained in:
Leandro Facchinetti 2022-11-17 19:38:48 +00:00
parent af5080f6e3
commit 459cfabc80
1 changed files with 81 additions and 37 deletions

View File

@ -9,6 +9,7 @@ import os from "node:os";
import * as commander from "commander"; import * as commander from "commander";
import express from "express"; import express from "express";
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import { Database, sql } from "@leafac/sqlite";
import lodash from "lodash"; import lodash from "lodash";
import { execa, ExecaChildProcess } from "execa"; import { execa, ExecaChildProcess } from "execa";
import caddyfile from "dedent"; import caddyfile from "dedent";
@ -240,7 +241,35 @@ await commander.program
}); });
}); });
const application = { const application: {
name: string;
version: string;
process: {
id: string;
type: "main" | "web" | "email";
number: number;
};
configuration: {
hostname: string;
dataDirectory: string;
administratorEmail: string;
environment: "production" | "development" | "other";
tunnel: boolean;
alternativeHostnames: string[];
hstsPreload: boolean;
caddy: string;
};
static: {
[path: string]: string;
};
ports: {
web: number[];
};
web: Omit<express.Express, "locals"> & Function;
email: "TODO";
log(...messageParts: string[]): void;
database: Database;
} = {
name: "kill-the-newsletter", name: "kill-the-newsletter",
version, version,
process: { process: {
@ -251,16 +280,7 @@ await commander.program
: undefined) as number, : undefined) as number,
}, },
configuration: (await import(url.pathToFileURL(configuration).href)) configuration: (await import(url.pathToFileURL(configuration).href))
.default as { .default,
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),
@ -275,10 +295,7 @@ await commander.program
}, },
web: express(), web: express(),
email: "TODO", email: "TODO",
log: (...messageParts: any[]) => { } as any;
console.log(messageParts.join(" "));
},
};
application.configuration.environment ??= "production"; application.configuration.environment ??= "production";
application.configuration.tunnel ??= false; application.configuration.tunnel ??= false;
@ -354,6 +371,55 @@ await commander.program
} }
); );
await fs.mkdir(application.configuration.dataDirectory, {
recursive: true,
});
application.database = new Database(
path.join(
application.configuration.dataDirectory,
`${application.name}.db`
)
);
process.once("exit", () => {
application.database.close();
});
if (application.process.type === "main") {
application.log("DATABASE MIGRATION", "STARTING...");
application.database.pragma("journal_mode = WAL");
// TODO: STOP USING DEFAULT VALUES.
// TODO: DOUBLE-CHECK THAT THE OLD MIGRATION SYSTEM IS COMPATIBLE WITH THIS, USING SQLITES PRAGMA USER_DATA
await application.database.migrate(
sql`
CREATE TABLE "feeds" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
"reference" TEXT NOT NULL UNIQUE,
"title" TEXT NOT NULL
);
CREATE TABLE "entries" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
"reference" TEXT NOT NULL UNIQUE,
"feed" INTEGER NOT NULL REFERENCES "feeds",
"title" TEXT NOT NULL,
"author" TEXT NOT NULL,
"content" TEXT NOT NULL
);
`,
sql`
CREATE INDEX "entriesFeed" ON "entries" ("feed");
`
);
application.log("DATABASE MIGRATION", "FINISHED");
}
application.web.get<{}, any, {}, {}, ResponseLocalsLogging>( application.web.get<{}, any, {}, {}, ResponseLocalsLogging>(
"/", "/",
(request, response) => { (request, response) => {
@ -391,28 +457,6 @@ await commander.program
// path.join(rootDirectory, "kill-the-newsletter.db") // path.join(rootDirectory, "kill-the-newsletter.db")
// ); // );
// databaseMigrate(database, [ // databaseMigrate(database, [
// sql`
// CREATE TABLE "feeds" (
// "id" INTEGER PRIMARY KEY AUTOINCREMENT,
// "createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
// "updatedAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
// "reference" TEXT NOT NULL UNIQUE,
// "title" TEXT NOT NULL
// );
// CREATE TABLE "entries" (
// "id" INTEGER PRIMARY KEY AUTOINCREMENT,
// "createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
// "reference" TEXT NOT NULL UNIQUE,
// "feed" INTEGER NOT NULL REFERENCES "feeds",
// "title" TEXT NOT NULL,
// "author" TEXT NOT NULL,
// "content" TEXT NOT NULL
// );
// `,
// sql`
// CREATE INDEX "entriesFeed" ON "entries" ("feed");
// `,
// ]); // ]);
// webApplication.use(express.static(path.join(__dirname, "../public"))); // webApplication.use(express.static(path.join(__dirname, "../public")));