|
1 |
| - |
2 |
| -import { walkSync } from '@std/fs'; |
3 |
| -import { contentType } from '@std/media-types'; |
4 |
| -import { extname } from "@std/path"; |
| 1 | +import { route, type Route } from "@std/http/unstable-route"; |
| 2 | +import { serveDir } from '@std/http/file-server'; |
5 | 3 | import { STEVE } from '@codingap/steve';
|
6 |
| -import { Handler } from "./src/types.ts"; |
7 | 4 | import { getNotFoundResponse } from "./src/middleware.ts";
|
8 | 5 |
|
9 | 6 | const PORT = 1337;
|
10 | 7 |
|
11 | 8 | STEVE.includeDirectory = './views/includes';
|
12 | 9 |
|
13 |
| -// read in static files |
14 |
| -const staticDirectory = 'static'; |
15 |
| -const STATIC_CONTENT: { [key: string]: Uint8Array } = {}; |
16 |
| -Array.from(walkSync(staticDirectory)).filter(file => file.isFile).forEach(file => STATIC_CONTENT[file.path] = Deno.readFileSync(file.path)); |
17 |
| - |
18 | 10 | // routing time
|
19 |
| - |
20 |
| -// ~~~ this should be the only part you need to modify |
21 | 11 | import MAIN_ROUTES from './src/routes.ts';
|
22 | 12 | import API_ROUTES from './src/api.ts';
|
23 | 13 |
|
24 |
| -const routers = [{ router: MAIN_ROUTES, startingPath: '' }, { router: API_ROUTES, startingPath: '/api' }]; |
25 |
| -// ~~~ end modification ~~~ |
26 |
| - |
27 |
| -// combine all routers together |
28 |
| - |
29 |
| -const ROUTES: { [key: string]: { match: (path: string) => { match: boolean, url: { [key: string]: string }}, handler: Handler }[] } = {}; |
30 |
| -routers.forEach(({ router, startingPath }) => { |
31 |
| - Object.keys(router).forEach(method => { |
32 |
| - if (ROUTES[method] === undefined) ROUTES[method] = []; |
33 |
| - Object.keys(router[method]).forEach(route => { |
34 |
| - const routeParts = (startingPath + route).split('/').filter(part => part.length > 0); |
35 |
| - |
36 |
| - ROUTES[method].push({ |
37 |
| - match: (path) => { |
38 |
| - const pathParts = path.split('/').filter(part => part.length > 0); |
39 |
| - if (pathParts.length !== routeParts.length) return { match: false, url: {} }; |
40 |
| - |
41 |
| - let match = true; |
42 |
| - const url: { [key: string]: string } = {}; |
43 |
| - |
44 |
| - for (let i = 0; i < pathParts.length; i++) { |
45 |
| - // parse url parameter if needed |
46 |
| - if (routeParts[i][0] === ':') url[routeParts[i].slice(1)] = pathParts[i]; |
47 |
| - else if (routeParts[i] !== pathParts[i]) { |
48 |
| - match = false; |
49 |
| - break; |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - return { match, url }; |
54 |
| - }, |
55 |
| - handler: router[method][route] |
56 |
| - }); |
57 |
| - }); |
58 |
| - }); |
59 |
| -}); |
| 14 | +const routers: Route[] = [ |
| 15 | + ...MAIN_ROUTES, |
| 16 | + ...API_ROUTES, |
| 17 | + { |
| 18 | + pattern: new URLPattern({ pathname: '/static/*' }), |
| 19 | + handler: (request) => serveDir(request, { quiet: true }) |
| 20 | + } |
| 21 | +]; |
60 | 22 |
|
61 | 23 | Deno.serve({
|
62 | 24 | port: PORT,
|
63 |
| - handler: async (request) => { |
64 |
| - // handle static files |
65 |
| - const url = new URL(request.url); |
66 |
| - const path = url.pathname.slice(1); |
67 |
| - if (path.startsWith(staticDirectory)) { |
68 |
| - const mimeType = contentType(extname(path)) || 'text/plaintext'; |
69 |
| - if (STATIC_CONTENT[path] !== undefined) { |
70 |
| - return new Response(STATIC_CONTENT[path], { |
71 |
| - status: 200, |
72 |
| - headers: { |
73 |
| - 'Content-Type': mimeType |
74 |
| - } |
75 |
| - }); |
76 |
| - } else return getNotFoundResponse(); |
77 |
| - } |
78 |
| - |
79 |
| - // try getting routes |
80 |
| - const method = request.method.toUpperCase(); |
81 |
| - if (ROUTES[method] !== undefined) { |
82 |
| - for (const route of ROUTES[method]) { |
83 |
| - const { match, url } = route.match(path); |
84 |
| - if (match) return await route.handler(request, url); |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - // else, return not found |
89 |
| - return getNotFoundResponse(); |
90 |
| - }, |
| 25 | + handler: route(routers, getNotFoundResponse), |
91 | 26 | onListen: () => {
|
92 | 27 | console.log(`https://codingap.dev is listening on port ${PORT} (http://localhost:${PORT})`);
|
93 | 28 | }
|
|
0 commit comments