Last active
January 27, 2022 22:53
-
-
Save yusukebe/d65193c6d96cf28752958ef3b4a1526d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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