Skip to content

Commit

Permalink
Add blog deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejearley committed Oct 19, 2023
1 parent 3f59db9 commit 44568ae
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 100 deletions.
6 changes: 3 additions & 3 deletions infrastructure/cdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as cdk from 'aws-cdk-lib'
import { Config } from './providers/config'
import { BlogStack } from './providers/blog'
import { UsersStack } from './providers/users'
import { NetworkStack } from './providers/network'
import { AnalyticsStack } from './providers/analytics'
import { LandingStack } from './providers/landing'
import { NodesStack } from './providers/nodes'
import { DnsStack } from './providers/dns'
import { WebStack } from './providers/web'
import { DocsStack } from './providers/docs'

const config = new Config()
const { env, stage } = config
Expand All @@ -21,7 +21,7 @@ if (stage !== 'prod') {
new UsersStack(app, config.getFullStackName('users'), { env, certificate, hostedZone, vpc })
new WebStack(app, config.getFullStackName('web'), { env, certificate, hostedZone })
} else {
new DocsStack(app, config.getFullStackName('docs'), { env, certificate, hostedZone })
new NodesStack(app, config.getFullStackName('nodes'), { env, hostedZone })
new LandingStack(app, config.getFullStackName('landing'), { env, certificate, hostedZone })
}
new BlogStack(app, config.getFullStackName('blog'), { env, certificate, hostedZone, vpc })
new LandingStack(app, config.getFullStackName('landing'), { env, certificate, hostedZone })
2 changes: 1 addition & 1 deletion infrastructure/cdk/src/interfaces/ProjectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ProjectConfig {
stage: string
rootDomain: string
subdomains: {
docsEthereum: string
blog: string
landing: string
nodes: string
users: string
Expand Down
6 changes: 6 additions & 0 deletions infrastructure/cdk/src/interfaces/StackProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface NodesStackProps extends cdk.StackProps {
hostedZone: route53.HostedZone
}

export interface BlogStackProps extends cdk.StackProps {
certificate: certmgr.Certificate
hostedZone: route53.HostedZone
vpc: ec2.Vpc
}

export interface UsersStackProps extends cdk.StackProps {
certificate: certmgr.Certificate
hostedZone: route53.HostedZone
Expand Down
59 changes: 59 additions & 0 deletions infrastructure/cdk/src/providers/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Construct } from 'constructs'
import * as cdk from 'aws-cdk-lib'
import * as ecrAssets from 'aws-cdk-lib/aws-ecr-assets'
import * as ecs from 'aws-cdk-lib/aws-ecs'
import * as ecsPatterns from 'aws-cdk-lib/aws-ecs-patterns'
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager'
import { BlogStackProps } from '../interfaces/StackProps'
import { Config } from './config'
import { kebabCase } from '@casimir/format'

/**
* Blog service stack
*/
export class BlogStack extends cdk.Stack {
public readonly name = 'blog'
public readonly assetPath = 'services/blog/Dockerfile'
public readonly contextPath = '../../'

constructor(scope: Construct, id: string, props: BlogStackProps) {
super(scope, id, props)

const config = new Config()
const { project, stage, rootDomain, subdomains } = config
const { certificate, hostedZone, vpc } = props

const imageAsset = new ecrAssets.DockerImageAsset(this, config.getFullStackResourceName(this.name, 'image'), {
directory: this.contextPath,
file: this.assetPath,
platform: ecrAssets.Platform.LINUX_AMD64,
ignoreMode: cdk.IgnoreMode.GIT
})

const hackmdToken = ecs.Secret.fromSecretsManager(
secretsmanager.Secret.fromSecretNameV2(this, config.getFullStackResourceName(this.name, 'hackmd-token'), kebabCase(config.getFullStackResourceName(this.name, 'hackmd-token')))
)
const fargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, config.getFullStackResourceName(this.name, 'fargate'), {
assignPublicIp: true,
certificate,
domainName: `${subdomains.blog}.${rootDomain}`, // e.g. blog.casimir.co or blog.dev.casimir.co
domainZone: hostedZone,
taskImageOptions: {
containerPort: 4000,
image: ecs.ContainerImage.fromDockerImageAsset(imageAsset),
environment: {
PROJECT: project,
STAGE: stage
},
secrets: {
HACKMD_TOKEN: hackmdToken
}
},
vpc
})

fargateService.targetGroup.configureHealthCheck({
path: '/health'
})
}
}
2 changes: 1 addition & 1 deletion infrastructure/cdk/src/providers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Config implements ProjectConfig {
}
this.rootDomain = `${this.stage === 'prod' ? '' : `${this.stage}.`}casimir.co`
this.subdomains = {
docsEthereum: 'docs.ethereum',
blog: 'blog',
nodes: 'nodes',
landing: 'www',
users: 'users',
Expand Down
83 changes: 0 additions & 83 deletions infrastructure/cdk/src/providers/docs.ts

This file was deleted.

14 changes: 7 additions & 7 deletions infrastructure/cdk/test/all.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as cdk from 'aws-cdk-lib'
import * as assertions from 'aws-cdk-lib/assertions'
import { Config } from '../src/providers/config'
import { BlogStack } from '../src/providers/blog'
import { UsersStack } from '../src/providers/users'
import { NetworkStack } from '../src/providers/network'
import { AnalyticsStack } from '../src/providers/analytics'
import { LandingStack } from '../src/providers/landing'
import { NodesStack } from '../src/providers/nodes'
import { DnsStack } from '../src/providers/dns'
import { WebStack } from '../src/providers/web'
import { DocsStack } from '../src/providers/docs'

test('All stacks created', () => {
const config = new Config()
Expand All @@ -19,9 +19,9 @@ test('All stacks created', () => {
const { vpc } = new NetworkStack(app, config.getFullStackName('network'), { env })

const analyticsStack = new AnalyticsStack(app, config.getFullStackName('analytics'), { env })
const docsStack = new DocsStack(app, config.getFullStackName('docs'), { env, certificate, hostedZone })
const landingStack = new LandingStack(app, config.getFullStackName('landing'), { env, certificate, hostedZone })
const nodesStack = new NodesStack(app, config.getFullStackName('nodes'), { env, hostedZone })
const blogStack = new BlogStack(app, config.getFullStackName('blog'), { env, certificate, hostedZone, vpc })
const usersStack = new UsersStack(app, config.getFullStackName('users'), { env, certificate, hostedZone, vpc })
const webStack = new WebStack(app, config.getFullStackName('web'), { env, certificate, hostedZone })

Expand All @@ -30,6 +30,11 @@ test('All stacks created', () => {
expect(output).toBeDefined()
})

const blogTemplate = assertions.Template.fromStack(blogStack)
Object.keys(blogTemplate.findOutputs('*')).forEach(output => {
expect(output).toBeDefined()
})

const usersTemplate = assertions.Template.fromStack(usersStack)
Object.keys(usersTemplate.findOutputs('*')).forEach(output => {
expect(output).toBeDefined()
Expand All @@ -40,11 +45,6 @@ test('All stacks created', () => {
expect(output).toBeDefined()
})

const docsTemplate = assertions.Template.fromStack(docsStack)
Object.keys(docsTemplate.findOutputs('*')).forEach(output => {
expect(output).toBeDefined()
})

const nodesTemplate = assertions.Template.fromStack(nodesStack)
Object.keys(nodesTemplate.findOutputs('*')).forEach(output => {
expect(output).toBeDefined()
Expand Down
28 changes: 28 additions & 0 deletions services/blog/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Note: This Dockerfile assumes context is set to the git root (../../)

# Use Node 18 Alpine as the base image
FROM node:18-alpine

# Install git
RUN apk add --no-cache git

# Copy all files to /tmp
COPY . .

# Install dependencies
RUN npm install --ignore-scripts

# Build the blog service
RUN npm run build --workspace @casimir/blog

# Move the blog service build assets to /app
RUN mv services/blog/dist /app

# Set the working directory to /app
WORKDIR /app

# Expose port 4001
EXPOSE 4001

# Run the app when the container launches
CMD ["node", "index.js"]
6 changes: 3 additions & 3 deletions services/blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const app = express()
app.use(cors())
app.use(express.json())

const hackMdUrl = 'https://api.hackmd.io/v1/teams'
const hackmdUrl = 'https://api.hackmd.io/v1/teams'

app.get('/articles', async (_req, res) => {
const options = {
Expand All @@ -16,7 +16,7 @@ app.get('/articles', async (_req, res) => {
'Content-Type': 'application/json',
}
}
const response = await fetch(`${hackMdUrl}/consensusnetworks/notes`, options)
const response = await fetch(`${hackmdUrl}/consensusnetworks/notes`, options)
if (!response.ok) {
res.status(response.status).send('Error fetching articles')
} else {
Expand All @@ -35,7 +35,7 @@ app.get('/articles/:id', async (req, res) => {
'Content-Type': 'application/json',
},
}
const response = await fetch(`${hackMdUrl}/consensusnetworks/notes/${notesId}`, options)
const response = await fetch(`${hackmdUrl}/consensusnetworks/notes/${notesId}`, options)
if (!response.ok) {
res.status(response.status).send(`Error fetching article ${notesId}`)
} else {
Expand Down
22 changes: 22 additions & 0 deletions services/blog/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "ESNext",
"strict": true,
"preserveConstEnums": true,
"noEmit": true,
"sourceMap": false,
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
},
"exclude": [
"node_modules",
"build.js"
],
"include": [
"./src/*"
]
}
3 changes: 1 addition & 2 deletions services/users/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"build.js"
],
"include": [
"./src/*",
"env.d.ts"
"./src/*"
]
}

0 comments on commit 44568ae

Please sign in to comment.