-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroutes.ts
174 lines (146 loc) · 5.36 KB
/
routes.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { serveFile } from 'std/http/file-server';
import {
basicLayoutResponse,
generateRandomPositiveInt,
PageContentResult,
serveFileWithSass,
serveFileWithTs,
} from './lib/utils.ts';
import * as notFoundPage from './pages/404.ts';
// NOTE: This won't be necessary once https://github.com/denoland/deploy_feedback/issues/433 is closed
import * as indexPage from './pages/index.ts';
import * as ssrPage from './pages/ssr.ts';
import * as dynamicPage from './pages/dynamic.ts';
import * as formPage from './pages/form.ts';
import * as webComponentPage from './pages/web-component.ts';
import * as reactPage from './pages/react.tsx';
const pages = {
index: indexPage,
ssr: ssrPage,
dynamic: dynamicPage,
form: formPage,
webComponent: webComponentPage,
react: reactPage,
};
export interface Route {
pattern: URLPattern;
handler: (
request: Request,
match: URLPatternResult,
) => Response | Promise<Response>;
}
interface Routes {
[routeKey: string]: Route;
}
type PageFunction = (
request: Request,
match: URLPatternResult,
) => Response | PageContentResult | Promise<Response | PageContentResult>;
interface Page {
pageContent: PageFunction;
pageAction: PageFunction;
}
function createBasicRouteHandler(id: string, pathname: string) {
return {
pattern: new URLPattern({ pathname }),
handler: async (request: Request, match: URLPatternResult) => {
try {
// NOTE: Use this instead once https://github.com/denoland/deploy_feedback/issues/433 is closed
// const { pageContent, pageAction }: Page = await import(`./pages/${id}.ts`);
const { pageContent, pageAction }: Page = pages[id as keyof typeof pages];
if (request.method !== 'GET') {
return pageAction(request, match) as Response;
}
const pageContentResult = await pageContent(request, match);
if (pageContentResult instanceof Response) {
return pageContentResult;
}
const { htmlContent, titlePrefix } = pageContentResult as PageContentResult;
return basicLayoutResponse(htmlContent, { currentPath: match.pathname.input, titlePrefix });
} catch (error) {
if ((error instanceof Error) && error.toString().includes('NotFound')) {
const { htmlContent, titlePrefix } = notFoundPage.pageContent();
return basicLayoutResponse(htmlContent, { titlePrefix, currentPath: match.pathname.input }, 404);
}
console.error(error);
return new Response('Internal Server Error', { status: 500 });
}
},
};
}
const oneDayInSeconds = 24 * 60 * 60;
const routes: Routes = {
sitemap: {
pattern: new URLPattern({ pathname: '/sitemap.xml' }),
handler: async () => {
const fileContents = await Deno.readTextFile(`public/sitemap.xml`);
return new Response(fileContents, {
headers: {
'content-type': 'application/xml; charset=utf-8',
'cache-control': `max-age=${oneDayInSeconds}, public`,
},
});
},
},
robots: {
pattern: new URLPattern({ pathname: '/robots.txt' }),
handler: async (request) => {
const response = await serveFile(request, `public/robots.txt`);
response.headers.set('cache-control', `max-age=${oneDayInSeconds}, public`);
return response;
},
},
favicon: {
pattern: new URLPattern({ pathname: '/favicon.ico' }),
handler: async (request) => {
const response = await serveFile(request, `public/images/favicon.ico`);
response.headers.set('cache-control', `max-age=${oneDayInSeconds}, public`);
return response;
},
},
public: {
pattern: new URLPattern({ pathname: '/public/:filePath*' }),
handler: async (request, match) => {
const { filePath } = match.pathname.groups;
try {
const fullFilePath = `public/${filePath}`;
const fileExtension = filePath!.split('.').pop()?.toLowerCase();
let response: Response;
if (fileExtension === 'ts') {
response = await serveFileWithTs(request, fullFilePath);
} else if (fileExtension === 'scss') {
response = await serveFileWithSass(request, fullFilePath);
} else {
response = await serveFile(request, `public/${filePath}`);
}
response.headers.set('cache-control', `max-age=${oneDayInSeconds}, public`);
return response;
} catch (error) {
if ((error instanceof Error) && error.toString().includes('NotFound')) {
return new Response('Not Found', { status: 404 });
}
console.error(error);
return new Response('Internal Server Error', { status: 500 });
}
},
},
index: createBasicRouteHandler('index', '/'),
ssr: createBasicRouteHandler('ssr', '/ssr'),
dynamic: createBasicRouteHandler('dynamic', '/dynamic'),
form: createBasicRouteHandler('form', '/form'),
webComponent: createBasicRouteHandler('webComponent', '/web-component'),
react: createBasicRouteHandler('react', '/react'),
reactWithInitialCount: createBasicRouteHandler('react', '/react/:count'),
api_v0_random_positive_int: {
pattern: new URLPattern({ pathname: '/api/v0/random-positive-int' }),
handler: () => {
const number = generateRandomPositiveInt();
return new Response(JSON.stringify({ number }), {
headers: {
'content-type': 'application/json; charset=utf-8',
},
});
},
},
};
export default routes;