Skip to content

Commit

Permalink
Adapt the server-side website construct to the new Construct syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jun 26, 2021
1 parent 4a13659 commit 84fafe9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/classes/AwsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Storage } from "../constructs/Storage";
import { Queue } from "../constructs/Queue";
import { Webhook } from "../constructs/Webhook";
import { StaticWebsite } from "../constructs/StaticWebsite";
import { ServerSideWebsite } from "../constructs/ServerSideWebsite";

export class AwsProvider {
private static readonly constructClasses: Record<string, StaticConstructInterface> = {};
Expand Down Expand Up @@ -124,4 +125,4 @@ export class AwsProvider {
* If they use TypeScript, `registerConstructs()` will validate that the construct class
* implements both static fields (type, schema, create(), …) and non-static fields (outputs(), references(), …).
*/
AwsProvider.registerConstructs(Storage, Queue, Webhook, StaticWebsite);
AwsProvider.registerConstructs(Storage, Queue, Webhook, StaticWebsite, ServerSideWebsite);
2 changes: 1 addition & 1 deletion src/classes/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "aws-sdk/clients/s3";
import { CreateInvalidationRequest, CreateInvalidationResult } from "aws-sdk/clients/cloudfront";
import { Provider as LegacyAwsProvider } from "../types/serverless";
import AwsProvider from "./AwsProvider";
import { AwsProvider } from "./AwsProvider";

// This is defined as a separate function to allow mocking in tests
export async function awsRequest<Input, Output>(
Expand Down
32 changes: 18 additions & 14 deletions src/constructs/ServerSideWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ import {
OriginRequestQueryStringBehavior,
ViewerProtocolPolicy,
} from "@aws-cdk/aws-cloudfront";
import { Construct as CdkConstruct, CfnOutput, Duration, Fn, RemovalPolicy } from "@aws-cdk/core";
import { CfnOutput, Construct, Duration, Fn, RemovalPolicy } from "@aws-cdk/core";
import { FromSchema } from "json-schema-to-ts";
import chalk from "chalk";
import { HttpOrigin, S3Origin } from "@aws-cdk/aws-cloudfront-origins";
import * as acm from "@aws-cdk/aws-certificatemanager";
import { BehaviorOptions } from "@aws-cdk/aws-cloudfront/lib/distribution";
import * as path from "path";
import * as fs from "fs";
import { flatten } from "lodash";
import { log } from "../utils/logger";
import { s3Put, s3Sync } from "../utils/s3-sync";
import AwsProvider from "../classes/AwsProvider";
import Construct from "../classes/Construct";
import { emptyBucket, invalidateCloudFrontCache } from "../classes/aws";
import { AwsConstruct, AwsProvider } from "../classes";
import { ConstructCommands } from "../classes/Construct";

export const SERVER_SIDE_WEBSITE_DEFINITION = {
const SCHEMA = {
type: "object",
properties: {
type: { const: "server-side-website" },
Expand All @@ -54,17 +55,26 @@ export const SERVER_SIDE_WEBSITE_DEFINITION = {
required: ["assets"],
} as const;

type Configuration = FromSchema<typeof SERVER_SIDE_WEBSITE_DEFINITION>;
type Configuration = FromSchema<typeof SCHEMA>;

export class ServerSideWebsite extends AwsConstruct {
public static type = "server-side-website";
public static schema = SCHEMA;
public static commands: ConstructCommands = {
"assets:upload": {
usage: "Upload assets directly to S3 without going through a CloudFormation deployment.",
handler: ServerSideWebsite.prototype.uploadAssets,
},
};

export class ServerSideWebsite extends CdkConstruct implements Construct {
private readonly distribution: Distribution;
private readonly bucketNameOutput: CfnOutput;
private readonly domainOutput: CfnOutput;
private readonly cnameOutput: CfnOutput;
private readonly distributionIdOutput: CfnOutput;

constructor(
scope: CdkConstruct,
scope: Construct,
private readonly id: string,
readonly configuration: Configuration,
private readonly provider: AwsProvider
Expand Down Expand Up @@ -122,7 +132,7 @@ export class ServerSideWebsite extends CdkConstruct implements Construct {
const apiGatewayDomain = Fn.join(".", [Fn.ref(apiId), `execute-api.${this.provider.region}.amazonaws.com`]);

// Cast the domains to an array
const domains = configuration.domain !== undefined ? [configuration.domain].flat() : undefined;
const domains = configuration.domain !== undefined ? flatten([configuration.domain]) : undefined;
const certificate =
configuration.certificate !== undefined
? acm.Certificate.fromCertificateArn(this, "Certificate", configuration.certificate)
Expand Down Expand Up @@ -187,12 +197,6 @@ export class ServerSideWebsite extends CdkConstruct implements Construct {
});
}

commands(): Record<string, () => Promise<void>> {
return {
"assets:upload": this.uploadAssets.bind(this),
};
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {
url: () => this.getUrl(),
Expand Down
8 changes: 4 additions & 4 deletions test/unit/serverSideWebsite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("server-side website", () => {

it("should create all required resources", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
cliArgs: ["package"],
command: "package",
config: Object.assign(baseConfig, {
constructs: {
backend: {
Expand Down Expand Up @@ -199,7 +199,7 @@ describe("server-side website", () => {

it("should support a custom domain", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
cliArgs: ["package"],
command: "package",
config: Object.assign(baseConfig, {
constructs: {
backend: {
Expand Down Expand Up @@ -247,7 +247,7 @@ describe("server-side website", () => {

it("should support multiple custom domains", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
cliArgs: ["package"],
command: "package",
config: Object.assign(baseConfig, {
constructs: {
backend: {
Expand Down Expand Up @@ -317,7 +317,7 @@ describe("server-side website", () => {
await runServerless({
fixture: "serverSideWebsite",
configExt: pluginConfigExt,
cliArgs: ["backend:assets:upload"],
command: "backend:assets:upload",
});

// scripts.js and styles.css were updated
Expand Down

0 comments on commit 84fafe9

Please sign in to comment.