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 renderRemoteIndex, 9 renderRemoteIndex,
10} from './utils'; 10} from './utils';
11 11
12import index from '../public/index.html'
13
12/* Create new Hono app */ 14/* Create new Hono app */
13const app = new Hono<{}, {}>({ strict: false }); 15const app = new Hono({ strict: false });
14 16
17/* Serve the index page (which contains the Swagger Docs) */
15app.get("/", async (ctx) => { 18app.get("/", async (ctx) => {
16 console.log('ctx.env', ctx.env); 19 if (index) {
17 20 return ctx.html(index)
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 }
33 } else { 21 } else {
34 return renderRemoteIndex(ctx); 22 return renderRemoteIndex(ctx);
35 } 23 }
36
37}); 24});
38 25
39
40/* Returns an array all service objects */ 26/* Returns an array all service objects */
41app.get('/services', async (c) => { 27app.get('/services', async (c) => {
42 return c.json(await fetchAllServices()); 28 return c.json(await fetchAllServices());
api/src/index.ts +1
@@ -1,3 +1,4 @@
1
1import api from './api'; 2import api from './api';
2 3
3api.fire(); 4api.fire();
api/wrangler.toml +5 −1
@@ -2,7 +2,11 @@
2name = "awesome-privacy-api" 2name = "awesome-privacy-api"
3compatibility_date = "2024-03-04" 3compatibility_date = "2024-03-04"
4main = "src/index.ts" 4main = "src/index.ts"
5# node_compat = true 5
6rules = [
7 { type = "Text", globs = ["public/*.html"], fallthrough = true }
8]
9
6 10
7[site] 11[site]
8bucket = "./public" # Path to your static files 12bucket = "./public" # Path to your static files