You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

import { Application } from "https://deno.land/x/oak@v7.3.0/mod.ts";
4 years ago
import config from "./config/config.ts";
4 years ago
import router from "./backend/api/index.ts";
import { homeTXT } from "./backend/txt/index.ts";
import { bootstrapBlocks } from "./backend/blocks/index.ts";
import { pageviews, pageviewsTxt } from "./backend/pageviews/index.ts";
4 years ago
bootstrapBlocks();
4 years ago
const app = new Application();
app.use(router.routes());
app.use(async (context) => {
const accepts = context.request.accepts();
4 years ago
if ([".js", ".css", ".json", ".ico", "png"].some((extension) => context.request.url.pathname.endsWith(extension))) {
await context.send({
root: `${Deno.cwd()}/frontend/dist`,
index: "index.html",
});
} else if (
accepts &&
((accepts.length === 1 && accepts[0] === "*/*") ||
accepts.includes("text/plain") ||
context.request.url.pathname === "/index.txt")
) {
await pageviewsTxt();
context.response.body = homeTXT();
} else {
await pageviews();
await context.send({
root: `${Deno.cwd()}/frontend/dist`,
path: `index.html`,
});
}
4 years ago
});
app.addEventListener("listen", ({ hostname, port, secure }) => {
const host = hostname ?? "localhost";
const protocol = secure ? "https://" : "http://";
console.log(`Listening on: ${protocol}${host}:${port}`);
});
await app.listen({
hostname: config.server.host,
port: config.server.port,
});