-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
46 lines (36 loc) · 1.17 KB
/
main.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
import {
Application,
Context,
helpers,
Router,
} from "https://deno.land/x/[email protected]/mod.ts";
import getStarCrossingDataByBirthdays from "./components/getStarCrossingDataByBirthdays.ts";
import { getAllStars, upsertStar } from "./db.ts";
import createStarCatalog from "./util/createStarCatalog.ts";
import { oakCors } from "https://deno.land/x/[email protected]/mod.ts";
// Populate DB
const starCatalog = await getAllStars();
if (starCatalog.length < 3) {
const createdStarCatalog = createStarCatalog();
for (const star of createdStarCatalog) {
upsertStar(star);
}
}
// Start server
const { getQuery } = helpers;
const router = new Router();
router.get("/", (ctx: Context) => {
ctx.response.body = "Successful Get request!";
});
router.get("/star-catalog", async (ctx: Context) => {
ctx.response.body = await getAllStars();
});
router.get("/star-crossings/:birthdays", (ctx: Context) => {
const { birthdays } = getQuery(ctx, { mergeParams: true });
ctx.response.body = getStarCrossingDataByBirthdays(birthdays);
});
const app = new Application();
app.use(oakCors());
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8000 });