-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
119 lines (102 loc) · 3.27 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Application, Router, send } from "https://deno.land/x/oak@v12.1.0/mod.ts";
import { Handlebars } from "https://deno.land/x/handlebars@v0.9.0/mod.ts";
import { existsSync } from "https://deno.land/std@0.179.0/fs/mod.ts";
import { Marked } from "https://raw.githubusercontent.com/meSingh/markdown/v3.0.0/mod.ts";
const AIRTABLE_KEY = Deno.env.get("AIRTABLE_KEY");
const app = new Application();
const handle = new Handlebars();
// by default uses this config:
// const DEFAULT_HANDLEBARS_CONFIG: HandlebarsConfig = {
// baseDir: "views",
// extname: ".hbs",
// layoutsDir: "layouts/",
// partialsDir: "partials/",
// defaultLayout: "main",
// helpers: undefined,
// compilerOptions: undefined,
// };
// Logger
app.use(async (ctx: any, next: any) => {
await next();
const rt = ctx.response.headers.get("X-Response-Time");
console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});
// Timing
app.use(async (ctx: any, next: any) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.response.headers.set("X-Response-Time", `${ms}ms`);
});
const router = new Router();
router
.get("/", async (context: any) => {
let projects: any[] = [];
const res = await fetch(
"https://api.airtable.com/v0/appDmsiM7p756BLff/projects?maxRecords=100&view=published",
{
headers: {
"Authorization": `Bearer ${AIRTABLE_KEY}`,
},
},
);
const records = await res.json();
projects = projects.concat(records.records);
context.response.body = await handle.renderView(
"index",
{ projects: projects.reverse() },
);
})
.get("/project/:id", async (context: any) => {
if (context.params && context.params.id) {
const res = await fetch(
`https://api.airtable.com/v0/appDmsiM7p756BLff/projects?maxRecords=100&view=published&filterByFormula={slug}="${context.params.id}"`,
// `https://api.airtable.com/v0/appDmsiM7p756BLff/projects/${context.params.id}`,
{
headers: {
"Authorization": `Bearer ${AIRTABLE_KEY}`,
},
},
);
const projects = await res.json();
const project = projects.records[0];
if (project.fields.description_long) {
project.fields.description_long = Marked.parse(
project.fields.description_long,
);
}
project.fields.description_encoded = encodeURIComponent(
project.fields.description,
);
context.response.body = await handle.renderView(
"project",
{ project },
);
}
});
// Listen to server events
app.addEventListener("listen", ({ hostname, port, secure }: any) => {
console.log(
`Listening on: ${secure ? "https://" : "http://"}${hostname ??
"localhost"}:${port}`,
);
});
// Listen to server errors
app.addEventListener("error", (evt) => {
// Will log the thrown error to the console.
console.log(evt.error);
});
app.use(router.routes());
app.use(router.allowedMethods());
// Serve static content
app.use(async (context, next) => {
try {
await context.send({ root: `${Deno.cwd()}/static` });
} catch {
await next();
}
});
const DEFAULT_PORT = 8000;
const envPort = Deno.env.get("PORT");
const port = envPort ? Number(envPort) : DEFAULT_PORT;
await app.listen({ port });