Skip to content

Commit

Permalink
build: migrate to devcontainer for development environment
Browse files Browse the repository at this point in the history
Signed-off-by: Husen <[email protected]>
  • Loading branch information
heyhusen committed Mar 19, 2024
1 parent bd25994 commit 8be5d33
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 296 deletions.
8 changes: 8 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/devcontainers/base:ubuntu-22.04

ENV HOME=/home/vscode

# Install dependencies
RUN sudo apt-get update
RUN curl -sS https://starship.rs/install.sh | sh -s -- --yes
RUN echo 'eval "$(starship init bash)"' >> $HOME/.bashrc
21 changes: 21 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Playground",
"dockerComposeFile": "./docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"overrideCommand": true,
"shutdownAction": "stopCompose",
"features": {
"ghcr.io/devcontainers/features/go:1": {
"version": "1.22"
},
"ghcr.io/devcontainers/features/node:1": {
"version": "20"
}
},
"customizations": {
"vscode": {
"extensions": ["EditorConfig.EditorConfig", "dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "svelte.svelte-vscode", "golang.go", "Grafana.vscode-jsonnet", "redhat.vscode-yaml", "ionutvmi.path-autocomplete", "GitHub.vscode-github-actions"]
}
}
}
210 changes: 210 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- ../:/workspaces:cached
command: sleep infinity
networks:
- playground

postgres:
image: docker.io/library/postgres:16-alpine
ports:
- '${DB_PORT:-5432}:5432'
volumes:
- 'postgres-data:/var/lib/postgresql/data'
environment:
POSTGRES_USER: ${DB_USERNAME:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
POSTGRES_DB: ${DB_DATABASE:-postgres}
healthcheck:
test:
[
'CMD',
'pg_isready',
'-q',
'-d',
'${DB_USERNAME:-postgres}',
'-U',
'${DB_PASSWORD:-postgres}',
]
retries: 3
timeout: 5s
networks:
- playground

redis:
image: docker.io/library/redis:7-alpine
ports:
- '${REDIS_PORT:-6379}:6379'
volumes:
- 'redis-data:/data'
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
retries: 3
timeout: 5s
networks:
- playground

minio:
image: docker.io/minio/minio
ports:
- '${S3_PORT:-9000}:9000'
- '${S3_CONSOLE_PORT:-8900}:8900'
volumes:
- 'minio-data:/data/minio'
environment:
MINIO_ROOT_USER: ${S3_ACCESS_KEY_ID:-miniosudo}
MINIO_ROOT_PASSWORD: ${S3_SECRET_ACCESS_KEY:-miniosudo}
command: minio server /data/minio --console-address ":8900"
healthcheck:
test: ['CMD', 'curl', '-f', 'http://127.0.0.1:9000/minio/health/live']
retries: 3
timeout: 5s
networks:
- playground

minio-client:
image: docker.io/minio/mc
depends_on:
- minio
entrypoint: >
/bin/sh -c "
/usr/bin/mc config host add myminio http://minio:9000 ${S3_ACCESS_KEY_ID:-miniosudo} ${S3_SECRET_ACCESS_KEY:-miniosudo};
/usr/bin/mc rm -r --force myminio/local;
/usr/bin/mc mb myminio/local;
/usr/bin/mc policy set download myminio/local;
exit 0;
"
networks:
- playground

# mailhog:
# image: docker.io/mailhog/mailhog:v1.0.0
# ports:
# - '${MAILHOG_PORT:-1025}:1025'
# - '${MAILHOG_DASHBOARD_PORT:-8025}:8025'
# volumes:
# - ../config:/.config
# environment:
# - MH_AUTH_FILE="/.config/mailhog/auth"
# networks:
# - playground

mailslurper:
image: docker.io/oryd/mailslurper:latest-smtps
ports:
- '4436:4436'
- '4437:4437'
networks:
- playground

postgres-kratos:
image: docker.io/library/postgres:16-alpine
ports:
- '${DB_PORT:-5431}:5432'
volumes:
- 'kratos-postgres:/var/lib/postgresql/data'
environment:
POSTGRES_USER: ${DB_USERNAME:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
POSTGRES_DB: ${DB_DATABASE:-postgres}
healthcheck:
test:
[
'CMD',
'pg_isready',
'-q',
'-d',
'${DB_USERNAME:-postgres}',
'-U',
'${DB_PASSWORD:-postgres}',
]
retries: 3
timeout: 5s
networks:
- playground

kratos-migrate:
image: docker.io/oryd/kratos:v1.0.0
depends_on:
- postgres-kratos
environment:
- DSN=postgres://postgres:postgres@postgres-kratos:5432/postgres?sslmode=disable&max_conns=20&max_idle_conns=4
volumes:
- type: bind
source: ../config/kratos/email-password
target: /etc/config/kratos
command: -c /etc/config/kratos/kratos.yml migrate sql -e --yes
restart: on-failure
networks:
- playground

kratos:
image: docker.io/oryd/kratos:v1.0.0
depends_on:
- kratos-migrate
ports:
- '4433:4433' # public
- '4434:4434' # admin
restart: unless-stopped
environment:
- DSN=postgres://postgres:postgres@postgres-kratos:5432/postgres?sslmode=disable&max_conns=20&max_idle_conns=4
- LOG_LEVEL=trace
- SERVE_PUBLIC_BASE_URL=http://127.0.0.1:4455/.ory/kratos/public/
command: serve -c /etc/config/kratos/kratos.yml --dev --watch-courier
volumes:
- type: bind
source: ../config/kratos/email-password
target: /etc/config/kratos
networks:
- playground

kratos-selfservice-ui-node:
image: docker.io/oryd/kratos-selfservice-ui-node:v0.13.0-20
depends_on:
- kratos
restart: on-failure
ports:
- 4435:4435
environment:
- PORT=4435
- KRATOS_BROWSER_URL=http://127.0.0.1:4455/.ory/kratos/public
- JWKS_URL=http://oathkeeper:4456/.well-known/jwks.json
- SECURITY_MODE=jwks
- COOKIE_SECRET=playground12345
- CSRF_COOKIE_NAME=__locahost-example.com-x-csrf-token
- CSRF_COOKIE_SECRET=playground12345
- KRATOS_PUBLIC_URL=http://kratos:4433/
networks:
- playground

oathkeeper:
image: docker.io/oryd/oathkeeper:v0.40.6
depends_on:
- kratos
ports:
- 4455:4455
- 4456:4456
command:
serve proxy -c "/etc/config/oathkeeper/oathkeeper.yml"
environment:
- LOG_LEVEL=debug
restart: on-failure
networks:
- playground
volumes:
- ../config/oathkeeper:/etc/config/oathkeeper

volumes:
postgres-data:
redis-data:
minio-data:
kratos-postgres:
kratos-sqlite:
networks:
playground:
19 changes: 15 additions & 4 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ jobs:
build:
name: Build (Node v${{ matrix.node }})
runs-on: ubuntu-latest
services:
postgres:
image: docker.io/library/postgres:14-alpine
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
strategy:
matrix:
node: ['20', '21']
Expand Down Expand Up @@ -71,11 +85,8 @@ jobs:
- name: Run tests
run: |
cd ${{ env.FOLDER }}
cp .env.example .env
docker compose up -d
sleep 10s
cp .env.testing .env
pnpm migrate:up
pnpm seed:run
pnpm test
docker compose down -v
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

# Dependency directories
node_modules

*.pnpm-store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# templates

- JWT authentification (with refresh token)
- JWT authentification (with refresh token).
2 changes: 1 addition & 1 deletion backend/node/.env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ AWS_DEFAULT_REGION=ap-southeast-1
AWS_ACCESS_KEY_ID=miniosudo
AWS_SECRET_ACCESS_KEY=miniosudo

REDIS_HOST=redis
REDIS_HOST=localhost
REDIS_PORT=6379

JWT_ACCESS_SECRET=jwtaccesssecret
Expand Down
1 change: 1 addition & 0 deletions backend/node/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const config = {
collectCoverage: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
setupFiles: ['dotenv/config'],
// globalTeardown: './__tests__/teardown.ts',
testMatch: ['**/src/**/*.spec.ts', '**/__tests__/**/*.test.ts'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion backend/node/src/infrastructure/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface AppConfig {
}

export const appConfig: AppConfig = {
name: process.env.APP_NAME || 'express',
name: process.env.APP_NAME || 'playground',
host: process.env.APP_HOST || 'localhost',
port: parseInt(String(process.env.APP_PORT), 10) || 3000,
url: process.env.APP_URL || 'http://localhost:3000',
Expand Down
2 changes: 1 addition & 1 deletion backend/node/src/infrastructure/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Knex } from 'knex';
export const databaseConfig: Knex.Config = {
client: 'pg',
connection: {
host: process.env.DB_HOST || '127.0.0.1',
host: process.env.DB_HOST || 'postgres',
port: parseInt(String(process.env.DB_PORT), 10) || 5432,
user: process.env.DB_USERNAME || 'postgres',
password: process.env.DB_PASSWORD || 'postgres',
Expand Down
10 changes: 10 additions & 0 deletions config/kratos/email-password/hooks/after-profile-setting.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function(ctx) {
data: {
type: "users",
attributes: {
first_name: ctx.identity.traits.name.first,
last_name: ctx.identity.traits.name.last,
email: ctx.identity.traits.email,
}
}
}
16 changes: 14 additions & 2 deletions config/kratos/email-password/kratos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ serve:
base_url: http://kratos:4434/

selfservice:
default_browser_return_url: http://127.0.0.1:4455/
default_browser_return_url: http://127.0.0.1:4455/welcome
allowed_return_urls:
- http://127.0.0.1:4455
- http://localhost:19006/Callback
Expand Down Expand Up @@ -39,6 +39,18 @@ selfservice:
ui_url: http://127.0.0.1:4455/settings
privileged_session_max_age: 15m
required_aal: highest_available
# after:
# profile:
# hooks:
# - hook: web_hook
# config:
# url: |
# http://backend:3000/users/{ identityId }
# method: PATCH
# body: file:///etc/config/kratos/hooks/after-profile-setting.jsonnet
# response:
# ignore: true
# parse: false

recovery:
enabled: true
Expand All @@ -50,7 +62,7 @@ selfservice:
ui_url: http://127.0.0.1:4455/verification
use: code
after:
default_browser_return_url: http://127.0.0.1:4455/
default_browser_return_url: http://127.0.0.1:4455/welcome

logout:
after:
Expand Down
Loading

0 comments on commit 8be5d33

Please sign in to comment.