Skip to content

Commit

Permalink
streaming and 404
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeshay committed Oct 13, 2023
1 parent 942d062 commit 48d293a
Show file tree
Hide file tree
Showing 37 changed files with 1,224 additions and 715 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-pets-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astro-aws/adapter": minor
---

Added AWS Lambda powertools
6 changes: 6 additions & 0 deletions .changeset/serious-guests-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@astro-aws/constructs": minor
"@astro-aws/adapter": minor
---

Added support for 404 pages
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
rules: {
"import/no-useless-path-segments": "off",
"no-console": "off",
"no-warning-comments": "off",
},
settings: {
"import/parsers": {
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cSpell.words": ["astro", "awslambda", "streamify"],
"cSpell.words": ["astro", "awslambda", "powertools", "streamify"],
"files.associations": {
"*.mdx": "markdown"
},
Expand Down
63 changes: 26 additions & 37 deletions apps/infra/src/bin/infra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import {
} from "../lib/constants/environments.js"
import { MonitoringStack } from "../lib/stacks/monitoring-stack.js"
import { WebsiteStack } from "../lib/stacks/website-stack.js"
import { RedirectStack } from "../lib/stacks/redirect-stack.js"
import type { CertificateStackProps } from "../lib/stacks/certificate-stack.js"
import { CertificateStack } from "../lib/stacks/certificate-stack.js"
import { GitHubOIDCStack } from "../lib/stacks/github-oidc-stack.js"
import { GitHubUsersStack } from "../lib/stacks/github-users-stack.js"

const app = new App()

const createStackName = (environment: string, stack: string) =>
`AstroAWS-${environment}-${stack}`
const createStackName = (environment: string, stack: string, mode?: string) =>
["AstroAWS", environment, stack, mode].filter(Boolean).join("-")

Object.entries(ENVIRONMENT_PROPS).forEach(([environment, environmentProps]) => {
const monitoringStack = new MonitoringStack(
Expand All @@ -24,15 +22,33 @@ Object.entries(ENVIRONMENT_PROPS).forEach(([environment, environmentProps]) => {
environmentProps,
)

let certificateStack: CertificateStack | undefined
environmentProps.websites.forEach((websiteProps) => {
let certificateStack: CertificateStack | undefined

if (environmentProps.hostedZoneName) {
certificateStack = new CertificateStack(
if (websiteProps.hostedZoneName && websiteProps.aliases) {
certificateStack = new CertificateStack(
app,
createStackName(environment, "Certificate", websiteProps.mode),
{
...environmentProps,
aliases: websiteProps.aliases,
hostedZoneName: websiteProps.hostedZoneName,
},
)
}

new WebsiteStack(
app,
createStackName(environment, "Certificate"),
environmentProps as CertificateStackProps,
createStackName(environment, "Website", websiteProps.mode),
{
...environmentProps,
...websiteProps,
certificate: certificateStack?.certificate,
cloudwatchDashboard: monitoringStack.cloudwatchDashboard,
hostedZone: certificateStack?.hostedZone,
},
)
}
})

if (environment === Environments.DEV) {
new GitHubUsersStack(
Expand All @@ -48,33 +64,6 @@ Object.entries(ENVIRONMENT_PROPS).forEach(([environment, environmentProps]) => {
)
}

new WebsiteStack(app, createStackName(environment, "Website"), {
...environmentProps,
certificate: certificateStack?.certificate,
cloudwatchDashboard: monitoringStack.cloudwatchDashboard,
hostedZone: certificateStack?.hostedZone,
})

if (environment === Environments.PROD && environmentProps.hostedZoneName) {
const redirectProps = {
...environmentProps,
alias: ["www", environmentProps.alias].filter(Boolean).join("."),
}

const redirectCertificateStack = new CertificateStack(
app,
createStackName(environment, "RedirectCertificate"),
redirectProps as CertificateStackProps,
)

new RedirectStack(app, createStackName(environment, "Redirect"), {
...redirectProps,
certificate: redirectCertificateStack.certificate,
cloudwatchDashboard: monitoringStack.cloudwatchDashboard,
hostedZone: redirectCertificateStack.hostedZone,
})
}

Tags.of(app).add("Project", "AstroAWS")
Tags.of(app).add("Environment", environment)
})
98 changes: 60 additions & 38 deletions apps/infra/src/lib/constants/environments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { env } from "node:process"

import type { AstroAWSStackProps } from "../types/astro-aws-stack-props.js"
import { type StaticWebsiteStackProps } from "../stacks/website-stack.js"

const base = {
analyticsReporting: false,
Expand All @@ -14,61 +15,82 @@ const base = {

const Environments = {
DEV: "DEV",
EDGE: "EDGE",
PERSONAL: "PERSONAL",
PROD: "PROD",
SSR: "SSR",
STREAM: "STREAM",
} as const

type Environment = (typeof Environments)[keyof typeof Environments]

const ENVIRONMENT_PROPS: Record<Environment, AstroAWSStackProps> = {
type EnvironmentProps = Readonly<
AstroAWSStackProps & {
websites: readonly (Partial<AstroAWSStackProps> & StaticWebsiteStackProps)[]
}
>

const ENVIRONMENT_PROPS: Record<Environment, EnvironmentProps> = {
[Environments.DEV]: {
...base,
alias: "static.dev",
environment: Environments.DEV,
hostedZoneName: "astro-aws.org",
package: "@astro-aws/docs",
},
[Environments.EDGE]: {
...base,
alias: "edge.dev",
distDir: "dist/edge",
env: {
...base.env,
region: "us-east-1",
},
environment: Environments.EDGE,
hostedZoneName: "astro-aws.org",
package: "@astro-aws/examples-base",
websites: [
{
aliases: ["www.static.dev", "static.dev"],
hostedZoneName: "astro-aws.org",
mode: "static",
package: "@astro-aws/docs",
},
{
aliases: ["www.ssr.dev", "ssr.dev"],
hostedZoneName: "astro-aws.org",
mode: "ssr",
package: "@astro-aws/examples-base",
},
{
aliases: ["www.stream.dev", "stream.dev"],
hostedZoneName: "astro-aws.org",
mode: "ssr-stream",
package: "@astro-aws/examples-base",
},
{
aliases: ["www.edge.dev", "edge.dev"],
env: {
...base.env,
region: "us-east-1",
},
hostedZoneName: "astro-aws.org",
mode: "edge",
package: "@astro-aws/examples-base",
},
],
},
[Environments.PROD]: {
...base,
environment: Environments.PROD,
hostedZoneName: "astro-aws.org",
package: "@astro-aws/docs",
websites: [
{
aliases: ["www", "*"],
hostedZoneName: "astro-aws.org",
mode: "static",
package: "@astro-aws/docs",
},
],
},
[Environments.PERSONAL]: {
...base,
environment: Environments.PERSONAL,
package: "@astro-aws/docs",
},
[Environments.SSR]: {
...base,
alias: "ssr.dev",
distDir: "dist/ssr",
environment: Environments.SSR,
hostedZoneName: "astro-aws.org",
package: "@astro-aws/examples-base",
},
[Environments.STREAM]: {
...base,
alias: "stream.dev",
distDir: "dist/ssr-stream",
environment: Environments.STREAM,
hostedZoneName: "astro-aws.org",
package: "@astro-aws/examples-base",
websites: [
{
mode: "static",
package: "@astro-aws/docs",
},
{
mode: "ssr",
package: "@astro-aws/examples-base",
},
{
mode: "ssr-stream",
package: "@astro-aws/examples-base",
},
],
},
} as const

Expand Down
4 changes: 2 additions & 2 deletions apps/infra/src/lib/constructs/basic-graph-widget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GraphWidgetProps, Metric } from "aws-cdk-lib/aws-cloudwatch"
import { GraphWidget } from "aws-cdk-lib/aws-cloudwatch"
import { GRID_WIDTH, GraphWidget } from "aws-cdk-lib/aws-cloudwatch"

export type BasicGraphWidgetProps = Omit<
GraphWidgetProps,
Expand All @@ -13,7 +13,7 @@ export class BasicGraphWidget extends GraphWidget {
const { metric, ...graphWidgetProps } = props

super({
width: 12,
width: GRID_WIDTH / 2,
...graphWidgetProps,
left: [metric],
title: metric.label,
Expand Down
9 changes: 6 additions & 3 deletions apps/infra/src/lib/stacks/certificate-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HostedZone } from "aws-cdk-lib/aws-route53"
import type { AstroAWSStackProps } from "../types/astro-aws-stack-props.js"

export type CertificateStackProps = AstroAWSStackProps & {
aliases: readonly [string, ...string[]]
hostedZoneName: string
}

Expand All @@ -30,16 +31,18 @@ export class CertificateStack extends Stack {
},
})

const { hostedZoneName, alias } = props

const domainName = [alias, hostedZoneName].filter(Boolean).join(".")
const { hostedZoneName, aliases } = props
const [domainName, ...restDomainNames] = aliases.map((alias) =>
[alias, hostedZoneName].join("."),
) as [string, ...string[]]

this.hostedZone = HostedZone.fromLookup(this, "HostedZone", {
domainName: hostedZoneName,
})

this.certificate = new Certificate(this, "Certificate", {
domainName,
subjectAlternativeNames: restDomainNames,
validation: CertificateValidation.fromDns(this.hostedZone),
})
}
Expand Down
Loading

0 comments on commit 48d293a

Please sign in to comment.