Skip to content

Commit 5d1b318

Browse files
committed
do the redirect at api server level, use directly from s3
1 parent 61d01ea commit 5d1b318

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

cloudformation/main.yml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,6 @@ Resources:
629629
S3OriginConfig:
630630
OriginAccessIdentity: ''
631631
OriginAccessControlId: !GetAtt AppCloudfrontS3OAC.Id
632-
- Id: S3DocsOrigin
633-
DomainName: !GetAtt AppFrontendS3Bucket.RegionalDomainName
634-
S3OriginConfig:
635-
OriginAccessIdentity: ''
636-
OriginAccessControlId: !GetAtt AppCloudfrontS3OAC.Id
637-
OriginPath: "/swagger"
638632
- Id: LambdaOrigin
639633
DomainName: !Select [0, !Split ['/', !Select [1, !Split ['https://', !GetAtt AppLambdaUrl.FunctionUrl]]]]
640634
CustomOriginConfig:
@@ -705,24 +699,6 @@ Resources:
705699
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6"
706700
OriginRequestPolicyId: b689b0a8-53d0-40ab-baf2-68738e2966ac
707701
Compress: true
708-
- PathPattern: "/api/documentation*"
709-
Compress: true
710-
TargetOriginId: S3DocsOrigin
711-
ViewerProtocolPolicy: redirect-to-https
712-
AllowedMethods:
713-
- GET
714-
- HEAD
715-
CachedMethods:
716-
- GET
717-
- HEAD
718-
ForwardedValues:
719-
QueryString: true
720-
Cookies:
721-
Forward: none
722-
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6 # caching-optimized
723-
# LambdaFunctionAssociations:
724-
# - EventType: origin-request
725-
# LambdaFunctionARN: !Ref AppFrontendEdgeLambdaVersion
726702
- PathPattern: "/api/*"
727703
TargetOriginId: LambdaOrigin
728704
ViewerProtocolPolicy: redirect-to-https

src/api/createSwagger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const html = `
1919
<script>
2020
window.onload = () => {
2121
window.ui = SwaggerUIBundle({
22-
url: '/api/documentation/openapi.json',
22+
url: '/swagger/openapi.json',
2323
dom_id: '#swagger-ui',
2424
});
2525
};

src/api/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const now = () => Date.now();
5959
async function init(prettyPrint: boolean = false, initClients: boolean = true) {
6060
const isRunningInLambda =
6161
process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME;
62+
let isSwaggerServer = true;
6263
const transport = prettyPrint
6364
? {
6465
target: "pino-pretty",
@@ -205,6 +206,7 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) {
205206
});
206207
} catch (e) {
207208
app.log.warn("Fastify Swagger not created!");
209+
isSwaggerServer = false;
208210
}
209211
await app.register(fastifyStatic, {
210212
root: path.join(__dirname, "public"),
@@ -213,7 +215,18 @@ async function init(prettyPrint: boolean = false, initClients: boolean = true) {
213215
if (!process.env.RunEnvironment) {
214216
process.env.RunEnvironment = "dev";
215217
}
216-
218+
if (isRunningInLambda && !isSwaggerServer) {
219+
// Serve docs from S3
220+
app.get("/api/documentation", (_request, response) => {
221+
response.redirect("/swagger/index.html", 308);
222+
});
223+
app.get("/api/documentation/json", (_request, response) => {
224+
response.redirect("/swagger/openapi.json", 308);
225+
});
226+
app.get("/api/documentation/yaml", (_request, response) => {
227+
response.redirect("/swagger/openapi.yaml", 308);
228+
});
229+
}
217230
if (!runEnvironments.includes(process.env.RunEnvironment as RunEnvironment)) {
218231
throw new InternalServerError({
219232
message: `Invalid run environment ${app.runEnvironment}.`,

0 commit comments

Comments
 (0)