Skip to content

Commit d220dc5

Browse files
committed
add caddy service
1 parent f206ef9 commit d220dc5

File tree

20 files changed

+122
-2533
lines changed

20 files changed

+122
-2533
lines changed

Caddyfile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
http://localhost:8080
2+
3+
route /assets {
4+
reverse_proxy localhost:4010
5+
}
6+
7+
route /http-protocol/example {
8+
reverse_proxy localhost:4010
9+
}
10+
11+
route /http-protocol/login {
12+
reverse_proxy localhost:4010
13+
}
14+
15+
route /http-protocol/stream {
16+
reverse_proxy localhost:4010
17+
}
18+
19+
route /http-protocol/removed {
20+
reverse_proxy localhost:4010
21+
}
22+
23+
route /http-protocol {
24+
reverse_proxy localhost:4010
25+
}
26+
27+
route /js-playwright/users-list {
28+
reverse_proxy localhost:4010
29+
}
30+
31+
route /js-dom-testing-library/users-list {
32+
reverse_proxy localhost:4010
33+
}
34+
35+
route /http-api/* {
36+
uri strip_prefix /http-api
37+
reverse_proxy localhost:4011
38+
}
39+
40+
route /http-protocol/* {
41+
uri strip_prefix /http-protocol
42+
reverse_proxy localhost:4011
43+
}
44+
45+
route /http-playwright/* {
46+
uri strip_prefix /http-playwright
47+
reverse_proxy localhost:4012
48+
}
49+
50+
route /http-postman/* {
51+
uri strip_prefix /http-postman
52+
reverse_proxy localhost:4013
53+
}

Dockerfile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
FROM node:22-alpine
2+
3+
ARG NODE_ENV=production
4+
ENV NODE_ENV $NODE_ENV
5+
6+
RUN apk add --no-cache make caddy
7+
RUN npm install -g @stoplight/prism-cli
8+
9+
COPY package.json package-lock.json* ./
10+
RUN npm ci && npm cache clean --force
11+
212
COPY . .
3-
RUN npm ci
4-
RUN apk add --no-cache make
513
RUN make compile
6-
CMD ["npm", "start"]
14+
15+
EXPOSE 8080
16+
17+
CMD ["make", "start"]

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ dev:
1212
npx fastify start -a 0.0.0.0 server/src/index.js
1313

1414
start:
15-
npx fastify start -a 0.0.0.0 server/src/index.js
15+
prism mock -m -p 4011 --host 0.0.0.0 ./tsp-output/http-api/@typespec/openapi3/openapi.1.0.yaml &
16+
prism mock -m -p 4012 --host 0.0.0.0 ./tsp-output/http-protocol/@typespec/openapi3/openapi.1.0.yaml &
17+
prism mock -m -p 4013 --host 0.0.0.0 ./tsp-output/js-playwright/@typespec/openapi3/openapi.1.0.yaml &
18+
prism mock -m -p 4014 --host 0.0.0.0 ./tsp-output/postman/@typespec/openapi3/openapi.1.0.yaml &
19+
npm start &
20+
caddy run &
1621

1722
test:
1823
echo no tests
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import { OpenAPIBackend } from 'openapi-backend';
2-
import fp from 'fastify-plugin';
31
import path from 'node:path';
4-
import fs from 'node:fs';
5-
import fastifySwagger from '@fastify/swagger';
6-
import swaggerUI from '@fastify/swagger-ui';
72
import fastifyStatic from '@fastify/static';
83
import formbody from '@fastify/formbody';
9-
import { getInitData } from './utils.js';
10-
import getHandlers from './handlers/openapi.js';
114

125
const { dirname } = import.meta;
136

14-
const apps = ['http-api', 'postman', 'http-protocol', 'js-playwright'];
15-
167
const setUpStaticAssets = (app) => {
178
const pathPublic = path.join(dirname, '../assets');
189
app.register(fastifyStatic, {
@@ -21,80 +12,6 @@ const setUpStaticAssets = (app) => {
2112
});
2213
};
2314

24-
const initOpenapi = async (names, app) => {
25-
const openapiApps = names.map((name) => {
26-
const state = getInitData();
27-
const openapiFilePath = path.join(dirname, '../../tsp-output/', name, '/@typespec/openapi3/openapi.1.0.yaml');
28-
29-
const api = new OpenAPIBackend({
30-
definition: openapiFilePath,
31-
handlers: getHandlers(state),
32-
});
33-
34-
api.init();
35-
36-
api.registerSecurityHandler('BearerAuth', (c) => {
37-
const authHeader = c.request.headers['authorization'];
38-
const token = authHeader.replace('Bearer ', '');
39-
const authorized = !!token; //state.tokens.includes(token);
40-
return authorized;
41-
});
42-
43-
api.registerSecurityHandler('ApiKeyAuth', (c) => {
44-
const authorized =
45-
c.request.headers['x-api-key'] === state.appConfig.apiKey;
46-
// truthy return values are interpreted as auth success
47-
// you can also add any auth information to the return value
48-
return authorized;
49-
});
50-
51-
app.route({
52-
method: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
53-
url: `/${name}/*`,
54-
handler: async (request, res) =>
55-
api.handleRequest(
56-
{
57-
method: request.method,
58-
path: request.url,
59-
body: request.body,
60-
query: request.query,
61-
headers: request.headers,
62-
},
63-
request,
64-
res,
65-
),
66-
});
67-
68-
return { name, state, openapiFilePath };
69-
});
70-
71-
const getPromises = (instance) => openapiApps.map(async ({ name, state, openapiFilePath }) => {
72-
return await instance.register(async (instance) => {
73-
await instance.register(fastifySwagger, {
74-
mode: 'static',
75-
title: state.appConfig.title,
76-
exposeRoute: true,
77-
specification: {
78-
path: openapiFilePath,
79-
},
80-
routePrefix: `${state.appConfig.docRoute}`,
81-
});
82-
83-
await instance.register(swaggerUI, {
84-
routePrefix: `/${name}/${state.appConfig.docRoute}`,
85-
title: state.appConfig.title,
86-
staticCSP: true,
87-
transformSpecificationClone: true,
88-
theme: {
89-
title: state.appConfig.title,
90-
},
91-
});
92-
});
93-
})
94-
95-
await app.register(fp((instance) => Promise.all(getPromises(instance))));
96-
};
97-
9815
export default async (app, _options) => {
9916
await app.register(formbody);
10017
setUpStaticAssets(app);
@@ -152,7 +69,5 @@ export default async (app, _options) => {
15269

15370
app.get('/js-dom-testing-library/users-list', (req, res) => res.sendFile('users-list/index.html'));
15471

155-
await initOpenapi(apps, app);
156-
15772
return app;
15873
};

docker-compose.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)