-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
70 lines (56 loc) · 1.78 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const dashboardPrefix = 'DASHBOARD_';
const env = (property: string, fallback: string): string => Deno.env.get(`${dashboardPrefix}${property}`) ?? fallback;
const buildLinkList = (): string => {
const listItems: string[] = [];
const envs = Deno.env.toObject();
Object.keys(envs)
.sort()
.forEach(function (name) {
if (name.startsWith(`${dashboardPrefix}LINK`)) {
const cleanName = name.replace(`${dashboardPrefix}LINK_`, '').replaceAll('_', ' ');
listItems.push(`<a href="${envs[name]}">${cleanName}</a>`);
}
});
return listItems.join('\n\t\t\t\t');
};
const buildDashboardPage = (): string => {
let page = Deno.readTextFileSync('./dashboard.html');
page = page.replaceAll('$projectImage', env('IMAGE', '/enrise.svg'));
page = page.replaceAll('$projectName', env('NAME', 'Dashboard'));
page = page.replaceAll('$linkList', buildLinkList());
return page;
};
const buildStylesheet = (): string => {
let stylesheet = Deno.readTextFileSync('./dashboard.css');
stylesheet = stylesheet.replaceAll('var(text-color)', env('TEXT_COLOR', '#000000'));
stylesheet = stylesheet.replaceAll('var(background-color)', env('BACKGROUND_COLOR', '#f29a00'));
return stylesheet;
};
Deno.serve((request: Request) => {
if (request.url.includes('.css')) {
return new Response(
buildStylesheet(),
{
status: 200,
headers: { 'content-type': 'text/css' },
},
);
}
if (request.url.includes('.svg')) {
return new Response(
Deno.readTextFileSync('./enrise.svg'),
{
status: 200,
headers: { 'content-type': 'image/svg+xml' },
},
);
}
console.log(`Opened dashboard at %c${new Date().toLocaleTimeString()}%c.`, 'color: green', 'color: initial');
return new Response(
buildDashboardPage(),
{
status: 200,
headers: { 'content-type': 'text/html' },
},
);
});