2019-02-20 22:12:29 +00:00
|
|
|
import { Page } from "./metadata";
|
|
|
|
import generators from "./generate";
|
2019-01-04 18:14:53 +00:00
|
|
|
|
2019-02-20 23:07:29 +00:00
|
|
|
import express, { Router } from "express";
|
2019-02-20 22:12:29 +00:00
|
|
|
import morgan from "morgan";
|
|
|
|
import bodyParser from "body-parser";
|
|
|
|
import activitypub from "./activitypub";
|
2019-02-20 23:07:29 +00:00
|
|
|
import validateHttpSig from "./activitypub/middleware/http-signature";
|
2019-02-20 22:12:29 +00:00
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
import "reflect-metadata";
|
|
|
|
import { createConnection} from "typeorm";
|
2019-08-17 19:25:19 +00:00
|
|
|
import * as path from "path";
|
2019-02-20 22:12:29 +00:00
|
|
|
|
|
|
|
async function generate(): Promise<Page[]> {
|
2019-01-04 18:36:40 +00:00
|
|
|
generators.copy();
|
2019-01-04 18:14:53 +00:00
|
|
|
generators.css();
|
2019-09-17 19:30:47 +00:00
|
|
|
generators.errors();
|
2019-01-04 18:14:53 +00:00
|
|
|
|
|
|
|
const tutorials = await generators.tutorials();
|
|
|
|
|
|
|
|
const posts = await generators.posts();
|
|
|
|
generators.homepage(posts);
|
|
|
|
const categories = await generators.categories(posts);
|
2019-02-18 18:47:43 +00:00
|
|
|
await generators.rss(posts, categories, tutorials);
|
|
|
|
|
2019-02-20 22:12:29 +00:00
|
|
|
return posts;
|
2019-01-04 18:14:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 22:12:29 +00:00
|
|
|
(async () => {
|
2019-08-17 18:50:18 +00:00
|
|
|
const app = express();
|
2019-08-17 19:25:19 +00:00
|
|
|
app.use(morgan("dev"));
|
2019-08-17 18:50:18 +00:00
|
|
|
app.use(bodyParser.json({ type: "application/activity+json" }));
|
|
|
|
|
2019-08-17 19:25:19 +00:00
|
|
|
const connection = await createConnection({
|
|
|
|
"type": "postgres",
|
2019-08-18 22:15:52 +00:00
|
|
|
"host": process.env.DB_HOST || "localhost",
|
|
|
|
"port": process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
|
|
|
|
"username": process.env.DB_USERNAME || "blog",
|
|
|
|
"password": process.env.DB_PASSWORD || "blog",
|
|
|
|
"database": process.env.DB_DATABASE || "blog",
|
2019-08-17 19:25:19 +00:00
|
|
|
"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"
|
|
|
|
}
|
|
|
|
});
|
2019-08-17 18:50:18 +00:00
|
|
|
|
2019-02-20 22:12:29 +00:00
|
|
|
const posts = await generate();
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
await activitypub.articles.setup(posts);
|
|
|
|
const toFederate = await activitypub.articles.toFederate();
|
2019-02-20 22:12:29 +00:00
|
|
|
|
2019-02-20 23:07:29 +00:00
|
|
|
const apRouter = Router();
|
|
|
|
apRouter.use(validateHttpSig);
|
|
|
|
await activitypub.actor(apRouter);
|
2019-03-01 23:42:28 +00:00
|
|
|
activitypub.comments(apRouter);
|
2019-02-20 23:07:29 +00:00
|
|
|
activitypub.followers(apRouter);
|
|
|
|
activitypub.inbox(apRouter);
|
2019-09-17 18:36:54 +00:00
|
|
|
await activitypub.nodeinfo(apRouter);
|
2019-02-20 23:07:29 +00:00
|
|
|
activitypub.webfinger(apRouter);
|
|
|
|
activitypub.articles.route(apRouter);
|
|
|
|
app.use(apRouter);
|
2019-02-20 22:12:29 +00:00
|
|
|
app.use(express.static("out"));
|
|
|
|
|
2019-09-18 17:15:47 +00:00
|
|
|
// redirect posts with changed permalinks
|
|
|
|
for (const post of posts) {
|
|
|
|
if (post.metadata.oldPermalink) {
|
|
|
|
app.get(post.metadata.oldPermalink, (req, res) => {
|
|
|
|
res.status(301).redirect(post.metadata.permalink);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 19:30:47 +00:00
|
|
|
app.use((err, req, res, next) => {
|
2019-09-17 19:42:40 +00:00
|
|
|
console.error("Unhandled error:", err);
|
2019-09-17 19:30:47 +00:00
|
|
|
res.status(500).sendFile("500.html", {
|
|
|
|
root: "out"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.status(404).sendFile("404.html", {
|
|
|
|
root: "out"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-20 22:12:29 +00:00
|
|
|
const port = process.env.PORT || 8083;
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Listening on port ${port}`);
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
activitypub.federate(toFederate);
|
2019-02-20 22:12:29 +00:00
|
|
|
});
|
|
|
|
})();
|
|
|
|
|