Skip to content

Commit

Permalink
fix(docs): use cdn when loading docs from public s3 bucket (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Dec 12, 2024
1 parent 96859d2 commit 0289e92
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/ui/docs-bundle/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const DOCS_FILES_ALLOWLIST = [
hostname: "fdr-dev2-docs-files-public.s3.amazonaws.com",
port: "",
},
{
protocol: "https",
hostname: "files.buildwithfern.com",
port: "",
},
{
protocol: "https",
hostname: "files-dev2.buildwithfern.com",
port: "",
},
];

/** @type {import("next").NextConfig} */
Expand Down
4 changes: 4 additions & 0 deletions servers/fdr-deploy/scripts/fdr-deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export class FdrDeployStack extends Stack {
REDIS_ENABLED: options.redis.toString(),
REDIS_CLUSTERING_MODE_ENABLED: options.redisClusteringModeEnabled.toString(),
APPLICATION_ENVIRONMENT: getEnvironmentVariableOrThrow("APPLICATION_ENVIRONMENT"),
PUBLIC_DOCS_CDN_URL:
environmentType === "DEV2"
? "https://files-dev2.buildwithfern.com"
: "https://files.buildwithfern.com",
},
containerName: CONTAINER_NAME,
containerPort: 8080,
Expand Down
1 change: 1 addition & 0 deletions servers/fdr/src/__test__/local/s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("S3 Service", () => {
redisEnabled: true,
redisClusteringEnabled: true,
applicationEnvironment: "string",
cdnPublicDocsUrl: "string",
});
const startUploadDocsResponse = await s3Service.createPresignedDocsAssetsUploadUrlWithClient({
domain: "buildwithfern.com",
Expand Down
1 change: 1 addition & 0 deletions servers/fdr/src/__test__/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MockRevalidatorService implements RevalidatorService {
export const baseMockFdrConfig: FdrConfig = {
awsAccessKey: "",
awsSecretKey: "",
cdnPublicDocsUrl: "https://files.buildwithfern.com",
publicDocsS3: {
bucketName: "fdr",
bucketRegion: "us-east-1",
Expand Down
3 changes: 3 additions & 0 deletions servers/fdr/src/app/FdrConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ENABLE_CUSTOMER_NOTIFICATIONS_ENV_VAR = "ENABLE_CUSTOMER_NOTIFICATIONS";
const REDIS_ENABLED_ENV_VAR = "REDIS_ENABLED";
const REDIS_CLUSTERING_ENABLED_ENV_VAR = "REDIS_CLUSTERING_ENABLED";
const APPLICATION_ENVIRONMENT_ENV_VAR = "APPLICATION_ENVIRONMENT";
const PUBLIC_DOCS_CDN_URL = "PUBLIC_DOCS_CDN_URL";

export interface S3Config {
bucketName: string;
Expand All @@ -38,6 +39,7 @@ export interface FdrConfig {
venusUrl: string;
awsAccessKey: string;
awsSecretKey: string;
cdnPublicDocsUrl: string;
publicDocsS3: S3Config;
privateDocsS3: S3Config;
privateApiDefinitionSourceS3: S3Config;
Expand Down Expand Up @@ -89,6 +91,7 @@ export function getConfig(): FdrConfig {
redisEnabled: process.env[REDIS_ENABLED_ENV_VAR] === "true",
redisClusteringEnabled: process.env[REDIS_CLUSTERING_ENABLED_ENV_VAR] === "true",
applicationEnvironment: getEnvironmentVariableOrThrow(APPLICATION_ENVIRONMENT_ENV_VAR),
cdnPublicDocsUrl: getEnvironmentVariableOrThrow(PUBLIC_DOCS_CDN_URL),
};
}

Expand Down
4 changes: 3 additions & 1 deletion servers/fdr/src/services/s3/S3Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ export interface S3Service {
}

export class S3ServiceImpl implements S3Service {
private publicDocsCDNUrl: string;
private publicDocsS3: S3Client;
private privateDocsS3: S3Client;
private privateApiDefinitionSourceS3: S3Client;
private presignedDownloadUrlCache = new Cache<string>(10_000, ONE_WEEK_IN_SECONDS);

constructor(private readonly config: FdrConfig) {
this.publicDocsCDNUrl = config.cdnPublicDocsUrl;
this.publicDocsS3 = new S3Client({
...(config.publicDocsS3.urlOverride != null ? { endpoint: config.publicDocsS3.urlOverride } : {}),
region: config.publicDocsS3.bucketRegion,
Expand Down Expand Up @@ -111,7 +113,7 @@ export class S3ServiceImpl implements S3Service {
return FdrAPI.Url(signedUrl);
}

return FdrAPI.Url(`https://${this.config.publicDocsS3.bucketName}.s3.amazonaws.com/${key}`);
return FdrAPI.Url(`https://${this.publicDocsCDNUrl}/${key}`);
}

async getPresignedDocsAssetsUploadUrls({
Expand Down

0 comments on commit 0289e92

Please sign in to comment.