-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (76 loc) · 2.03 KB
/
server.js
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
import {
always,
applySpec,
cond,
converge,
curry,
map,
path,
prop,
pipe,
test
} from "https://deno.land/x/[email protected]/mod.ts";
import Task from "../functional/library/Task.js";
import { assertIsInstance, encodeText } from "https://deno.land/x/[email protected]/library/utilities.js";
import startHTTPServer from "../functional-http-server/library/server.js";
import File from "https://deno.land/x/[email protected]/library/File.js";
import Response from "https://deno.land/x/[email protected]/library/Response.js";
import { readFile } from "https://deno.land/x/[email protected]/library/fs.js";
const factorizeResponse = curry(Response.Success);
const resolveMimeType = cond([
[
test(/\.js$/),
always("text/javascript")
],
[
test(/\.css$/),
always("text/css")
],
[
test(/\.html$/),
always("text/html")
],
[
always(true),
always("text/plain")
]
]);
const catchMap = curry((unaryFunction, task) => Task.prototype.catch.call(task, unaryFunction));
startHTTPServer(
{ port: Number(Deno.env.get("PORT")) || 8080 },
pipe(
path([ "headers", "url" ]),
path => path.replace(/\?.*/, "").replace(/(?<=\.[jts]{2})\:[0-9]+\:[0-9]+/, ""),
path => File.fromPath(
`${Deno.cwd()}/${
path === "/" || !path.includes("/assets") && !path.includes("/fragments")
? "index.html"
: path
}`),
readFile,
map(
converge(
factorizeResponse,
[
pipe(
prop("path"),
applySpec({
headers: {
"Access-Control-Allow-Origin": always("*"),
"Cache-Control": always("max-age=31536000"),
"Content-Type": resolveMimeType
},
status: always(200)
}),
),
prop("raw")
]
)
),
catchMap(error =>
assertIsInstance(Deno.errors.NotFound, error)
? Response.NotFound({}, new Uint8Array([]))
: Response.InternalServerError({}, encodeText(error.message))
)
)
);