diff --git a/packages/cubby/src/server.tsx b/packages/cubby/src/server.tsx index a8a8d7b..0fcc191 100644 --- a/packages/cubby/src/server.tsx +++ b/packages/cubby/src/server.tsx @@ -9,10 +9,15 @@ import { Layout } from "./components/Layout" import { Project } from "./components/Project" const PROJECTS_DIR = ".." -const WORKSHOP_REPO = "https://github.com/probablycorey/the-rabbit-hole" +const WORKSHOP_REPO = "https://github.com/probablycorey/the-workshop" const CUBBY_DIR = "./cubby" +// ---------------------------------------------------------------------------- +// Setup +// ---------------------------------------------------------------------------- + const app = new Hono() +const api = new Hono() app.use("*", async (c, next) => { const start = Date.now() @@ -26,17 +31,9 @@ app.use('/js/*', serveStatic({ root: './public' })) app.use('/src/css/*', serveStatic({ root: '.' })) app.use('/src/js/*', serveStatic({ root: '.' })) -async function projects(): Promise { - const subdirs = readdirSync(PROJECTS_DIR, { withFileTypes: true }) - .filter((entry: any) => entry.isDirectory()) - .map((entry: any) => entry.name).sort() - - return subdirs -} - -function tsx(node: any) { - return "" + render({node}) -} +// ---------------------------------------------------------------------------- +// Web routes +// ---------------------------------------------------------------------------- app.get("/", async (c) => { const names = await projects() @@ -122,6 +119,37 @@ app.delete('/:id/delete/:filename', async c => { } }) +// ---------------------------------------------------------------------------- +// API routes +// ---------------------------------------------------------------------------- + +api.get('/projects', async c => { + const names = await projects() + return c.json(names) +}) + +// ---------------------------------------------------------------------------- +// Helper functions +// ---------------------------------------------------------------------------- + +async function projects(): Promise { + const subdirs = readdirSync(PROJECTS_DIR, { withFileTypes: true }) + .filter((entry: any) => entry.isDirectory()) + .map((entry: any) => entry.name).sort() + + return subdirs +} + +function tsx(node: any) { + return "" + render({node}) +} + +// ---------------------------------------------------------------------------- +// Server +// ---------------------------------------------------------------------------- + +app.route('/api', api) + export default { port: process.env.PORT || 3000, fetch: app.fetch,