Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Last active January 27, 2022 22:53
Show Gist options
  • Save yusukebe/d65193c6d96cf28752958ef3b4a1526d to your computer and use it in GitHub Desktop.
Save yusukebe/d65193c6d96cf28752958ef3b4a1526d to your computer and use it in GitHub Desktop.
import { Hono } from 'hono'
const slurpAsset = async (rawPath) => {
let ASSET_MANIFEST = __STATIC_CONTENT_MANIFEST
if (typeof __STATIC_CONTENT_MANIFEST === 'string') {
ASSET_MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST)
}
let ASSET_NAMESPACE = __STATIC_CONTENT
const key = ASSET_MANIFEST[rawPath] || rawPath
const template = await ASSET_NAMESPACE.get(key, { type: 'text' })
return template
}
const app = new Hono()
app.use('*', async (c, next) => {
let Mustache
try {
Mustache = await import('mustache')
} catch (e) {
console.error(`Mustache is not found! ${e}`)
}
c.render = async (file, opt) => {
const template = await slurpAsset(file)
const output = Mustache.render(template, opt)
return c.html(output)
}
await next()
})
app.use('*', async (c, next) => {
try {
await next()
} catch (e) {
console.log(`Error: e`)
}
})
app.get('/', async (c) => {
const name = c.req.query('name') || 'no name'
return await c.render('hello.html', { name: name })
})
app.fire()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment