v3: added opengraph support to the app, wrote README instructions incl. a Docker image
This commit is contained in:
37
api/main.ts
37
api/main.ts
@@ -13,16 +13,27 @@ import followsRouter from "./routes/follows.ts";
|
||||
import notificationsRouter from "./routes/notifications.ts";
|
||||
import invitesRouter from "./routes/invites.ts";
|
||||
|
||||
import { BASE_URL, HOSTNAME, PORT } from "./config.ts";
|
||||
import { ALLOWED_ORIGINS, BASE_URL, LISTEN_HOST, PORT } from "./config.ts";
|
||||
import { errorMiddleware } from "./middleware/error.ts";
|
||||
import routeStaticFilesFrom from "./lib/static.ts";
|
||||
import { DUMPS_DIR, UPLOADS_DIR } from "./lib/upload.ts";
|
||||
import { UUID_RE } from "./lib/slugify.ts";
|
||||
import { ogMiddleware } from "./middleware/og.ts";
|
||||
import { routeStaticFilesFrom } from "./lib/static.ts";
|
||||
|
||||
const app = new Application();
|
||||
|
||||
const cors = oakCors({ origin: ALLOWED_ORIGINS });
|
||||
|
||||
app.use(errorMiddleware);
|
||||
app.use(oakCors());
|
||||
app.use(ogMiddleware);
|
||||
// Only invoke oakCors when the origin is present and allowed.
|
||||
// The library calls next() without await when origin is falsy, which
|
||||
// corrupts the response in Oak's async middleware chain.
|
||||
app.use(async (ctx, next) => {
|
||||
const origin = ctx.request.headers.get("origin");
|
||||
if (origin && ALLOWED_ORIGINS.includes(origin)) {
|
||||
return await cors(ctx, next);
|
||||
}
|
||||
await next();
|
||||
});
|
||||
app.use(
|
||||
dumpsRouter.routes(),
|
||||
dumpsRouter.allowedMethods(),
|
||||
@@ -82,22 +93,8 @@ app.addEventListener(
|
||||
(e) => console.log(`Uncaught error: ${e.message}`),
|
||||
);
|
||||
|
||||
// Migrate dump files from uploads root to uploads/dumps subfolder
|
||||
async function migrateDumpFiles() {
|
||||
await Deno.mkdir(DUMPS_DIR, { recursive: true });
|
||||
for await (const entry of Deno.readDir(UPLOADS_DIR)) {
|
||||
if (entry.isFile && UUID_RE.test(entry.name)) {
|
||||
await Deno.rename(
|
||||
`${UPLOADS_DIR}/${entry.name}`,
|
||||
`${DUMPS_DIR}/${entry.name}`,
|
||||
).catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
await migrateDumpFiles();
|
||||
await app.listen({ hostname: HOSTNAME, port: PORT });
|
||||
await app.listen({ hostname: LISTEN_HOST, port: PORT });
|
||||
}
|
||||
|
||||
export { app };
|
||||
|
||||
Reference in New Issue
Block a user