shadowfacts.net/lib/index.ts

84 lines
2.0 KiB
TypeScript

import { Page } from "./metadata";
import generators from "./generate";
import express, { Router } from "express";
import morgan from "morgan";
import bodyParser from "body-parser";
import activitypub from "./activitypub";
import validateHttpSig from "./activitypub/middleware/http-signature";
import "reflect-metadata";
import { createConnection} from "typeorm";
import * as path from "path";
async function generate(): Promise<Page[]> {
generators.copy();
generators.css();
generators.missing();
const tutorials = await generators.tutorials();
const posts = await generators.posts();
generators.homepage(posts);
generators.redirects(posts);
const categories = await generators.categories(posts);
await generators.rss(posts, categories, tutorials);
return posts;
}
(async () => {
const app = express();
app.use(morgan("dev"));
app.use(bodyParser.json({ type: "application/activity+json" }));
const connection = await createConnection({
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "blog",
"password": "blog",
"database": "blog",
"synchronize": true,
"logging": true,
"entities": [
path.join(__dirname, "entity/**/*.{ts,js}")
],
"migrations": [
path.join(__dirname, "migration/**/*.{ts,js}")
],
"subscribers": [
path.join(__dirname, "subscriber/**/*.{ts,js}")
],
"cli": {
"entitiesDir": "lib/entity",
"migrationsDir": "lib/migration",
"subscribersDir": "lib/subscriber"
}
});
const posts = await generate();
await activitypub.articles.setup(posts);
const toFederate = await activitypub.articles.toFederate();
const apRouter = Router();
apRouter.use(validateHttpSig);
await activitypub.actor(apRouter);
activitypub.comments(apRouter);
activitypub.followers(apRouter);
activitypub.inbox(apRouter);
activitypub.webfinger(apRouter);
activitypub.articles.route(apRouter);
app.use(apRouter);
app.use(express.static("out"));
const port = process.env.PORT || 8083;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
activitypub.federate(toFederate);
});
})();