-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
executable file
·59 lines (53 loc) · 1.51 KB
/
mod.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
import { addCors } from "./cors.ts";
import { staticFns } from "./src/statics/mod.ts";
import { dynamicFns } from "./src/dynamics/mod.ts";
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { handleFakeDb } from "./src/utils/bootstrap/initializeDB.ts";
import { firstPageSlice } from "./src/isdb/blog/blogFirstPage/mod.ts";
import { DynamicModels, parsBody, StaticModels } from "./src/utils/mod.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
await firstPageSlice.setup();
Deno.args.includes("--gen-data") && (await handleFakeDb());
const corsRespond = {
headers: addCors(),
};
for await (const req of s) {
try {
//for handling cors issue
req.method === "OPTIONS" &&
req.respond({
...corsRespond,
});
const response = async () => {
const {
contents,
wants: { model, doit },
details,
context,
} = await parsBody(req);
return {
["dynamic"]: () =>
dynamicFns(model as DynamicModels, doit, details, context),
["static"]: () => staticFns(model as StaticModels, doit),
}[contents]();
};
req.respond({
...corsRespond,
body: JSON.stringify({
success: true,
body: await response(),
}),
status: 200,
});
} catch (error) {
req.respond({
...corsRespond,
body: JSON.stringify({
success: false,
body: error.message || "I don't know what happens ...",
}),
status: 500,
});
}
}