Commit 40611ea022
40611ea02242cb3fcb81c0ca10d3d46be2d4394b
parent: 3e2d719d47
Unsigned
Alicia Sykes <alicia@omg.lol> · 2024-03-08 18:09 UTC
Updates API landing for Hono on CF workers for Swagger
Layout: unified · split
api/src/api.ts
+6 −20
| @@ -9,34 +9,20 @@ import { |
| 9 | 9 | renderRemoteIndex, |
| 10 | 10 | } from './utils'; |
| 11 | 11 | |
| 12 | import index from '../public/index.html' |
| 13 | |
| 12 | 14 | /* Create new Hono app */ |
| 13 | | const app = new Hono<{}, {}>({ strict: false }); |
| 15 | const app = new Hono({ strict: false }); |
| 14 | 16 | |
| 17 | /* Serve the index page (which contains the Swagger Docs) */ |
| 15 | 18 | app.get("/", async (ctx) => { |
| 16 | | console.log('ctx.env', ctx.env); |
| 17 | | |
| 18 | | if (ctx.env?.ASSETS) { |
| 19 | | let response: Response | undefined; |
| 20 | | try { |
| 21 | | response = await ctx.env.ASSETS.fetch( |
| 22 | | ctx.request.url, |
| 23 | | ctx.request.clone() |
| 24 | | ); |
| 25 | | response = |
| 26 | | response && response.status >= 200 && response.status < 400 |
| 27 | | ? new Response(response.body, response) |
| 28 | | : undefined; |
| 29 | | } catch {} |
| 30 | | if (!response) { |
| 31 | | return renderRemoteIndex(ctx); |
| 32 | | } |
| 19 | if (index) { |
| 20 | return ctx.html(index) |
| 33 | 21 | } else { |
| 34 | 22 | return renderRemoteIndex(ctx); |
| 35 | 23 | } |
| 36 | | |
| 37 | 24 | }); |
| 38 | 25 | |
| 39 | | |
| 40 | 26 | /* Returns an array all service objects */ |
| 41 | 27 | app.get('/services', async (c) => { |
| 42 | 28 | return c.json(await fetchAllServices()); |
api/src/index.ts
+1
| @@ -1,3 +1,4 @@ |
| 1 | |
| 1 | 2 | import api from './api'; |
| 2 | 3 | |
| 3 | 4 | api.fire(); |
api/wrangler.toml
+5 −1
| @@ -2,7 +2,11 @@ |
| 2 | 2 | name = "awesome-privacy-api" |
| 3 | 3 | compatibility_date = "2024-03-04" |
| 4 | 4 | main = "src/index.ts" |
| 5 | | # node_compat = true |
| 5 | |
| 6 | rules = [ |
| 7 | { type = "Text", globs = ["public/*.html"], fallthrough = true } |
| 8 | ] |
| 9 | |
| 6 | 10 | |
| 7 | 11 | [site] |
| 8 | 12 | bucket = "./public" # Path to your static files |