fix tempalting to add redirects to package paths
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
225ed57381
commit
a56f5534a6
103
go_vanity.js
103
go_vanity.js
|
@ -5,18 +5,54 @@
|
|||
* - https://developers.cloudflare.com/workers/about/how-workers-work/
|
||||
* - https://developers.cloudflare.com/workers/recipes/vcl-conversion/delivering-custom-responses/
|
||||
* - https://developers.cloudflare.com/workers/writing-workers/handling-errors/
|
||||
* - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
|
||||
* - https://developer.mozilla.org/en-US/docs/Web/API/Request
|
||||
**/
|
||||
|
||||
/* main config, this is what decides what gets redirected to GitHub */
|
||||
const PACKAGES = {
|
||||
'go.status.im/protocol': 'https://github.com/status-im/status-protocol-go',
|
||||
'go.status.im/status': 'https://github.com/status-im/status-go',
|
||||
'go.status.im/whisper': 'https://github.com/status-im/whisper',
|
||||
}
|
||||
|
||||
const genMetaTags = (pkgs) => {
|
||||
return Object.keys(pkgs).map(name => (
|
||||
`<meta name="go-import" content="${name} git ${pkgs[name]}">`
|
||||
))
|
||||
/* I could use a fancy templating engine, but why bother? */
|
||||
const htmlTemplate = ({meta, body}) => (`
|
||||
<!doctype html>
|
||||
<html class="no-js" lang="">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Status.im Go Vanity</title>
|
||||
<meta name="description" content="This site hosts metadata for Status.im Go packages.">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
${meta || ''}
|
||||
</head>
|
||||
<body>
|
||||
${body || ''}
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
|
||||
const tableTemplate = ({rows}) => (`
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th>Origin</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${rows.join('\n ')}
|
||||
</tbody>
|
||||
</table>
|
||||
`)
|
||||
|
||||
const genMetaTag = (pkg) => {
|
||||
return `<meta name="go-import" content="${pkg} git ${PACKAGES[pkg]}">`
|
||||
}
|
||||
|
||||
const genPkgLink = (pkg) => {
|
||||
return `<p>Redirect: <code>${pkg}</code> to <a href="${PACKAGES[pkg]}">${PACKAGES[pkg]}</a></p>`
|
||||
}
|
||||
|
||||
const genTabRows = (pkgs) => {
|
||||
|
@ -25,36 +61,47 @@ const genTabRows = (pkgs) => {
|
|||
))
|
||||
}
|
||||
|
||||
const INDEX_HTML = `
|
||||
<!doctype html>
|
||||
<html class="no-js" lang="">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Status.im Go Vanity</title>
|
||||
<meta name="description" content="This site hosts metadata for Status.im Go packages.">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
${genMetaTags(PACKAGES).join('\n ')}
|
||||
</head>
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th>Origin</th>
|
||||
</tr>
|
||||
${genTabRows(PACKAGES).join('\n ')}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
const findMatchingPackage = (url) => {
|
||||
return Object.keys(PACKAGES).find((key) => url.endsWith(key))
|
||||
}
|
||||
|
||||
/* fetch and modify request object */
|
||||
const handleRequest = async (request) => {
|
||||
return new Response(INDEX_HTML, {
|
||||
headers: {'Content-Type': 'text/html'}
|
||||
/* Redirect requests to specific repos to GitHub using meta tags */
|
||||
let pkg = findMatchingPackage(request.url)
|
||||
if (pkg != undefined) {
|
||||
return new Response(
|
||||
htmlTemplate({
|
||||
meta: genMetaTag(pkg),
|
||||
body: genPkgLink(pkg),
|
||||
}),
|
||||
{ headers: {'Content-Type': 'text/html'} }
|
||||
)
|
||||
}
|
||||
|
||||
/* All other paths should return a table with all redirects */
|
||||
return new Response(
|
||||
htmlTemplate({
|
||||
body: tableTemplate({rows: genTabRows(PACKAGES)})
|
||||
}),
|
||||
{ headers: {'Content-Type': 'text/html'}
|
||||
})
|
||||
}
|
||||
|
||||
/* wrapper for returning JS errors in response body */
|
||||
const handleErrors = (func) => {
|
||||
try {
|
||||
return func()
|
||||
} catch (e) {
|
||||
return new Response(`Error:\n${e.stack}`)
|
||||
}
|
||||
}
|
||||
|
||||
/* handle every new request */
|
||||
addEventListener('fetch', (event) => {
|
||||
event.respondWith(handleRequest(event.request))
|
||||
event.respondWith(
|
||||
handleErrors(
|
||||
handleRequest.bind(null, event.request)
|
||||
)
|
||||
)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue