Skip to content

Commit 848da49

Browse files
authored
Merge pull request #5 from davidsilva/feature/refine-workflow
Feature/refine workflow
2 parents 461b18d + 0d5fae8 commit 848da49

File tree

10 files changed

+99
-116
lines changed

10 files changed

+99
-116
lines changed

.devcontainer/devcontainer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"name": "Node.js & TypeScript",
55
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
66
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-22-bookworm",
7+
// Features to add to the dev container. More info: https://containers.dev/features.
78
"features": {
89
"ghcr.io/devcontainers-extra/features/angular-cli:2": {},
910
"ghcr.io/hspaans/devcontainer-features/sshpass:1": {},
@@ -13,17 +14,21 @@
1314
"ghcr.io/devcontainers/features/aws-cli:1": {},
1415
"ghcr.io/robbert229/devcontainer-features/postgresql-client:1": {}
1516
},
17+
// Specify a network that the app's Docker containers and services can share.
18+
// This allows the containers to communicate with each other using their service names,
19+
// which is useful for setting up a consistent development environment where services
20+
// like databases, APIs, and front-end applications can interact seamlessly.
1621
"runArgs": ["--init", "--privileged", "--network=interview-prep_devnetwork"],
1722
"remoteUser": "root",
1823
"mounts": [
24+
// Allow authenticating with GitHub using the same SSH agent
1925
"source=${localEnv:HOME}/.ssh,target=/root/.ssh,type=bind,consistency=cached",
26+
// Allow devcontainer to communicate with the host's Docker daemon
2027
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind",
28+
// Allow devcontainer to use the host's AWS credentials and configuration
2129
"source=${localEnv:HOME}/.aws,target=/root/.aws,type=bind,consistency=cached"
2230
]
2331

24-
// Features to add to the dev container. More info: https://containers.dev/features.
25-
// "features": {},
26-
2732
// Use 'forwardPorts' to make a list of ports inside the container available locally.
2833
// "forwardPorts": [],
2934

.env.example

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
FLYWAY_USER=your_db_user
2-
FLYWAY_PASSWORD=your_db_password
3-
FLYWAY_DB=your_db_name
4-
# Using port 5433 because 5432 is already in use by the local PostgreSQL server
5-
# localhost because an SSH tunnel is used to connect to the RDS instance
6-
FLYWAY_URL=jdbc:postgresql://localhost:5433/your_db_name
71
PORT=3000
82
DATABASE_URL=postgres://your_db_user:your_db_password@localhost:5432/your_db_name

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,4 @@ Thumbs.db
6363

6464
!.env.example
6565

66-
# Flyway
67-
flyway.conf
68-
6966
notes.md

README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
# InterviewPrep
22

3-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 18.2.4.
3+
This project grew out of wanting to prepare for an interview for a job that would have focused on Angular. As I had been using React exclusively for several months, I needed to refresh my Angular skills. Then I began to add more things to make up for my focus on AWS Amplify. The result is in some ways the opposite of what I have in my previous videos:
4+
5+
* Angular instead of React/NextJS
6+
* Angular Material instead of AWS Amplify's UI library. (I do, however, use Tailwind CSS in both.)
7+
* Postgres instead of DynamoDB (Sql vs NoSQL)
8+
* REST (using a NodeJS/Express backend) vs GraphQL (using Amplify and AppSync)
9+
* This project has nothing in regard to authentication (yet), while the Amplify project implemented authentication right at the beginning.
10+
11+
Also, in the Amplify project I haven't yet implemented any automated testing but in this project I have unit and integration tests. I set up a GitHub Workflow with GitHub Actions to run the tests when there is a push to or a pull request on specified branches.
12+
13+
This project is Dockerized, and I developed it in a devcontainer.
14+
15+
You'll see some Terraform files in the project but I'll get to that in the next video.
16+
17+
## Devcontainer
18+
19+
I'm using the [Node.js & TypeScript (typescript-node)](https://github.com/devcontainers/templates/tree/main/src/typescript-node) devcontainer. The features I added are
20+
21+
* [sshpass](https://github.com/hspaans/devcontainer-features/tree/master/src/sshpass): installs the GitHub CLI
22+
* [Angular CLI](https://github.com/devcontainers-extra/features/tree/main/src/angular-cli)
23+
* [docker-outside-of-docker](https://github.com/devcontainers/features/tree/main/src/docker-outside-of-docker): "Re-use the host docker socket, adding the Docker CLI to a container. Feature invokes a script to enable using a forwarded Docker socket within a container to run Docker commands."
24+
* [Prettier](https://github.com/devcontainers-community/npm-features/tree/main/src/prettier)
25+
* [AWS CLI](https://github.com/devcontainers/features/tree/main/src/aws-cli)
26+
* [Terraform](https://github.com/devcontainers/features/tree/main/src/terraform)
27+
* [PostgreSQL Client](https://github.com/robbert229/devcontainer-features/blob/main/src/postgresql-client/README.md)
28+
29+
In devcontainer.json you'll see that I'm specifying a network. That's so the devcontainer and the Docker services can communicate with one another. For instance, from a shell within the devcontainer I can connect to the service running the Postgres test database.
30+
31+
I also have some mounts so that
32+
33+
* git can authenticate with GitHub using the SSH agent on my laptop (the host machine)
34+
* devcontainer can communicate with the host's Docker daemon
35+
* the AWS credentials and configuration are available within the devcontainer.
36+
437

5-
## Development
638

739
### Frontend
840

@@ -50,22 +82,18 @@ To get more help on the Angular CLI use `ng help` or go check out the [Angular C
5082
`ssh -f -i <PATH TO KEY-PAIR PEM> -L 5433:interviewprepdbinstance.c92egeoumrf1.us-east-1.rds.amazonaws.com:5432 ec2-user@107.22.66.121 -N`
5183
- To keep SSH tunnel alive: `ssh -f -i <PATH TO KEY-PAIR PEM> -L 5433:interviewprepdbinstance.c92egeoumrf1.us-east-1.rds.amazonaws.com:5432 ec2-user@107.22.66.121 -N -o ServerAliveInterval=60 -o ServerAliveCountMax=3`
5284

53-
## Flyway and Migrations
85+
## Migrations
5486

5587
DB credentials and URLs are in `.env.local` and `.env.development`. Set the environment to be used: `export NODE_ENV=local` or `export NODE_ENV=production`.
5688

57-
The migrations are run (if necessary) after the db container comes up.
58-
5989
Maybe obsolete:
6090

61-
You need an SSH tunnel to be able to run the Flyway migrations on the db in AWS. Check for a tunnel by using `ps aux | grep ssh`. It will look something like
91+
You need an SSH tunnel to be able to run the migrations on the db in AWS. Check for a tunnel by using `ps aux | grep ssh`. It will look something like
6292

6393
```
6494
davidsilva 69319 0.0 0.0 410379280 1904 ?? Ss 12:34PM 0:00.01 ssh -f -i /Users/davidsilva/Downloads/OnyxKeyPair.pem -L 5433:interviewprepdbinstance.c92egeoumrf1.us-east-1.rds.amazonaws.com:5432 ec2-user@107.22.66.121 -N
6595
```
6696

67-
To run migrations, run `./run-flyway.sh`.
68-
6997
# SSH into Bastion Host
7098

7199
The security group only allows SSH connections from my VPN IP address.
@@ -201,3 +229,14 @@ Could run in the background or in separate shell to have HTML reports loaded (an
201229

202230
Everything except the tests will run because frontend is dependent upon backend, which is dependent upon db.
203231
`docker-compose --env-file .env.local up --build frontend`
232+
233+
# Database
234+
235+
With `interview-prep-db-1` up and running, you can access the db via psql using...
236+
237+
`docker exec -it interview-prep-db-1 psql -U interviewprep_admin -d interviewprepdbinstance`
238+
239+
## Run Migrations
240+
241+
`docker-compose --env-file .env.local up migrate`
242+

backend/src/knexFile.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
import { Knex } from 'knex';
22
import dotenv from 'dotenv';
3+
import path from 'path';
4+
5+
let envFile: string;
6+
7+
switch (process.env['NODE_ENV']) {
8+
case 'production':
9+
envFile = '../.env.production';
10+
break;
11+
case 'development':
12+
envFile = '../.env.development';
13+
break;
14+
case 'test':
15+
envFile = '../.env.test';
16+
break;
17+
default:
18+
envFile = '../.env.local';
19+
}
320

4-
const envFile =
5-
process.env['NODE_ENV'] === 'production'
6-
? '../.env.production'
7-
: process.env['NODE_ENV'] === 'test'
8-
? '../.env.test'
9-
: '../.env.local';
1021
dotenv.config({ path: envFile });
1122

1223
const config: { [key: string]: Knex.Config } = {
24+
local: {
25+
client: 'pg',
26+
connection: process.env['DATABASE_URL'],
27+
migrations: {
28+
directory: path.join(__dirname, '../migrations'),
29+
},
30+
seeds: {
31+
directory: path.join(__dirname, '../seeds'),
32+
},
33+
debug: true,
34+
},
1335
development: {
1436
client: 'pg',
1537
connection: process.env['DATABASE_URL'],
@@ -24,10 +46,10 @@ const config: { [key: string]: Knex.Config } = {
2446
client: 'pg',
2547
connection: process.env['DATABASE_URL'],
2648
migrations: {
27-
directory: './migrations',
49+
directory: path.join(__dirname, '../migrations'),
2850
},
2951
seeds: {
30-
directory: './seeds',
52+
directory: path.join(__dirname, '../seeds'),
3153
},
3254
},
3355
production: {

docker-compose.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
command: npm run start
1414
depends_on:
1515
- backend
16+
networks:
17+
- devnetwork
1618

1719
backend:
1820
build:
@@ -26,6 +28,8 @@ services:
2628
command: npm run dev
2729
depends_on:
2830
- db
31+
networks:
32+
- devnetwork
2933

3034
db:
3135
image: postgres:latest
@@ -53,13 +57,19 @@ services:
5357
networks:
5458
- devnetwork
5559

56-
flyway:
57-
image: flyway/flyway:latest
58-
command: -url=jdbc:postgresql://db:5432/interviewprepdbinstance -user=${POSTGRES_USER} -password=${POSTGRES_PASSWORD} migrate
60+
migrate:
61+
image: node:20
62+
working_dir: /app/backend
5963
volumes:
60-
- ./migrations:/flyway/migrations
64+
- /Users/davidsilva/Dev/interview-prep:/app
65+
environment:
66+
- NODE_ENV=${NODE_ENV}
67+
- DATABASE_URL=${DATABASE_URL}
68+
command: sh -c "npm install && npx knex migrate:latest --knexfile=./src/knexFile.ts --env $NODE_ENV"
6169
depends_on:
6270
- db
71+
networks:
72+
- devnetwork
6373

6474
frontend-tests:
6575
build:

flyway.conf.template

Lines changed: 0 additions & 19 deletions
This file was deleted.

migrations/V1__Create_products_table.sql

Lines changed: 0 additions & 24 deletions
This file was deleted.

migrations/V2__Create_users_table.sql

Lines changed: 0 additions & 24 deletions
This file was deleted.

run-flyway.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)