-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
219 lines (202 loc) · 7.5 KB
/
server.ts
File metadata and controls
219 lines (202 loc) · 7.5 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { serve} from "https://deno.land/std/http/server.ts"
import { Status } from 'https://deno.land/std@0.53.0/http/http_status.ts';
import { LeadSheet } from "https://raw.githubusercontent.com/cfjello/leadsheet/main/mod.ts"
type RequestExtended = Request & { params: Record<string, string>, query: Record<string, string> }
//
// Initialize data for main page
//
const LS = new LeadSheet()
LS.debug = false
await LS.loadAllSheets()
//
// Mime types
//
const extToMime = new Map([
[ 'html', "text/html; charset=utf-8" ],
[ 'txt', "text/html; charset=utf-8" ],
[ 'png', "image/svg+xml" ],
[ 'svg', "image/svg+xml" ],
[ 'json', "application/json; charset=utf-8" ]
])
//
// Routes
//
type RouteIntf = {
name: string; // name of the route, just for tracking
path: string; // path pattern for handler
handler: (req: RequestExtended) => Promise<Response>; // handler to handle request
}
const routes: RouteIntf[] = [
{ name: "static", path: "/html/:fileName", handler: staticFile},
{ name: "menu", path: "/api/v1/menu", handler: menuHandler},
{ name: "sheet", path: "/api/v1/sheet/:sheetName", handler: sheetHandler},
{ name: "test", path: "/test/:arg1/:arg2", handler: testHandler },
{ name: "favicon", path: "/favicon.ico", handler: pngHandler },
{ name: "main", path: "/", handler: mainPage},
]
function routeNotFound(req: RequestExtended): Response {
const body = JSON.stringify({ message: `${req.params.path} NOT FOUND` })
return new Response(body, {
status: Status.NotFound,
headers: {
"content-type": "application/json; charset=utf-8",
},
})
}
async function router(req: Request): Promise<Response> {
console.info(`${new Date().toISOString()}\t${req.method}\t${req.url}`)
const url = new URL(req.url);
const fullPath = url.pathname;
//
// create query object
//
(req as RequestExtended).query = {};
for(const p of url.searchParams) (req as RequestExtended).query[p[0]]=p[1];
let found = false
for (const route of routes) {
if ( found ) break;
const basePath = route.path.replace(/[:].*$/,'')
if ( fullPath.startsWith(basePath) ) {
const routePathArr = route.path.split('/');
const fullPathArr = fullPath.split('/');
(req as RequestExtended).params = {path: basePath.replaceAll('%20', ' ')};
let i = 0
for( const p of routePathArr ) {
if ( p.startsWith(':') ) {
(req as RequestExtended).params[p.replace(':', '')] = (fullPathArr[i] ?? '').replaceAll('%20', ' ')
}
i++
}
// Add any additional anonymous path elements
for ( let j = i ; j < fullPathArr.length; j++ )
(req as RequestExtended).params[`p${j}`] = fullPathArr[j]
found = true
return await route.handler(req as RequestExtended)
}
}
return routeNotFound(req as RequestExtended)
}
//
// Handlers
//
async function handler( req: RequestExtended) {
// List the posts in the `blog` directory located at the root of the repository.
try {
const posts = [];
for await (const post of Deno.readDir(`./blog`)) {
const content = await Deno.readFile('./blog/' + post.name )
posts.push(post);
posts.push({ name: post.name, content: content });
}
// Return JSON.
return new Response(JSON.stringify(posts, null, 2), {
headers: {
"content-type": "application/json",
},
});
} catch(err) {
console.log(`handler() got: ${err}`)
return routeNotFound(req)
}
}
async function staticFile(req: RequestExtended, __filePath = '' ): Promise<Response> {
// handle static files
let filePath = ''
try {
const _filePath = __filePath !== '' ? __filePath : req.params.path + '/' + req.params.fileName
filePath = _filePath.startsWith('/') ? '.' + _filePath : './' + _filePath
console.debug(`Trying to read: '${filePath}'`)
// Reading relative path
const dataUint = await Deno.readFile(filePath)
const ext = filePath.split('.').pop()
console.log(`Server sends file: ${filePath} with content-type: ${extToMime.get(ext ?? 'html')!}`)
if ( ext === 'html' || ext === 'svg' ) {
// Decode the Uint8Array as string.
const data = new TextDecoder().decode(dataUint);
return new Response(data, {
status: Status.OK,
headers: {
"content-type": extToMime.get(ext ?? 'html')!,
},
});
}
else {
return new Response(dataUint, {
status: Status.OK,
headers: {
"content-type": extToMime.get(ext ?? 'html')!,
},
});
}
} catch (err) {
console.error(`staticFile handler for ${filePath} in directory ${Deno.cwd()} got: ${err}`)
// return handler(req)
return routeNotFound(req)
}
}
async function pngHandler(req: RequestExtended ): Promise<Response> {
return await staticFile(req, req.params.path.replace('.ico', '.png'))
}
async function testHandler(req: RequestExtended): Promise<Response> {
console.log("Method:", req.method);
const url = new URL(req.url);
const path = "\nfullPath:" + url.pathname
const params = JSON.stringify(req.params)
const query = JSON.stringify(req.query)
// const params = "\nsearchParams:" + url.searchParams
const body = await req.text()
let headers = '\nHeaders:'
for (const [key, value] of req.headers.entries()) {
headers += `\n\t${key}: ${value}`
}
return new Response(`CWD: '${Deno.cwd()}'` + path + '\nParams:' + params + '\nQuery:' + query + "\nURL:\n" + url.toString() + headers + `\nBody: '${body}'`);
}
async function mainPage(req: RequestExtended): Promise<Response> {
const fileName = "/html/leadSheetVue.html"
return await staticFile(req, fileName)
}
async function menuHandler(req: RequestExtended): Promise<Response> {
console.log(`Server GOT request for MenuItems`)
if ( ! LS.menuList) {
console.log(`Server cannot find the Menu List`)
return routeNotFound(req)
}
else {
// We have a menu
const data = JSON.stringify(await LS.getMenuItems())
// console.log(`Server sending Menu List data: ${data}`)
return new Response(data , {
status: Status.OK,
headers: {
"content-type": extToMime.get('json')!,
},
})
}
}
async function sheetHandler(req: RequestExtended): Promise<Response> {
console.log(`Server GOT request for Sheet`)
const sheet = req.params.sheetName ?? '__undefined__'
const transpose = parseInt(req.query.t)
const sharpFlat = req.query.sf
const reload = req.query.rl === 'yes' ? true: false
const data = await LS.getRestSheet(sheet, transpose, sharpFlat, reload )
if ( sheet === '__undefined__' || ! data ) {
// console.log(`Server cannot find the Song named ${sheet}`)
return routeNotFound(req)
}
else {
// We have a menu
// console.log(`Server sending Sheet: ${sheet}`)
const dataJson = JSON.stringify(data)
return new Response(dataJson , {
status: 200,
headers: {
"content-type": extToMime.get('json')!,
},
})
}
}
//
// Start the Server
//
serve(router)