Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CD] Dockerize All Services, Improve CI Scripts #31

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions .docker-compose.dev.yml

This file was deleted.

1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
POSTGRES_PASSWORD=example
POSTGRES_USER=postgres
POSTGRES_DB=postgres
DB_PORT=5432
ADMINER_PORT=8080
JWT_SECRET=secret
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun run test --run
bun run test
17 changes: 17 additions & 0 deletions Dockerfile.client
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM oven/bun:alpine AS base

FROM base AS builder
WORKDIR /app
RUN apk add --no-cache git
COPY . .

RUN bun install
RUN bun run client:build

FROM builder AS runner
WORKDIR /app
ENV NODE_ENV=production

USER bun
EXPOSE 5173
CMD ["bun", "run", "client:start"]
16 changes: 16 additions & 0 deletions Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM oven/bun:alpine AS base

FROM base AS builder
WORKDIR /app
RUN apk add --no-cache git
COPY . .

RUN bun install

FROM builder AS runner
WORKDIR /app
ENV NODE_ENV=production

USER bun
EXPOSE 3000
CMD ["bun", "run", "server:start"]
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Budgeteer

## Prerequisites

To get started with Budgeteer, ensure that you have the following tools installed and properly configured on your system:

1. **Docker**: A platform to develop, ship, and run applications using containers.
- Installation guide: [Get Docker](https://docs.docker.com/get-docker/)
2. **Docker Compose**: A tool for defining and running multi-container Docker applications.
- Installation guide: [Install Docker Compose](https://docs.docker.com/compose/install/)

## Usage

Run the following command to initialize the required services using Docker Compose:

```bash
# Executes the Docker Compose configuration for development
npm run all:up
```

## Setting Up Environment Variables

> [!TIP]
> By default, the environment variables are set to placeholder fallbacks to ensure the app works without any configuration.

If you want to setup your own environment variables in the Docker Compose files, make a copy of the `.env.example` first.

```bash
# creates a .env to read from when executing the Docker Compose file
cp .env.example .env

# start editing it
nano .env
```
54 changes: 54 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
services:
client:
depends_on:
- server
build:
context: .
dockerfile: Dockerfile.client
restart: always
ports:
- ${CLIENT_PORT:-5173}:${CLIENT_PORT:-5173}
Luzefiru marked this conversation as resolved.
Show resolved Hide resolved
environment:
- NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL:-http://server:${SERVER_PORT:-3000}/api}

server:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile.server
restart: always
ports:
- ${SERVER_PORT:-3000}:${SERVER_PORT:-3000}
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-example}
- POSTGRES_USER=${POSTGRES_USER:-postgres}
- POSTGRES_DB=${POSTGRES_DB:-postgres}
- POSTGRES_HOST=${POSTGRES_HOST:-db}
- JWT_SECRET=${JWT_SECRET:-secret}

db:
image: postgres
restart: always
ports:
- ${DB_PORT:-5432}:${DB_PORT:-5432}
shm_size: 128mb
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-example}
- POSTGRES_USER=${POSTGRES_USER:-postgres}
- POSTGRES_DB=${POSTGRES_DB:-postgres}
volumes:
- budgeteer-db:/var/lib/postgresql/data
# run this script on first DB initialization
- ./packages/server/src/infrastructure/drizzle-data-service/migrations/0000_furry_sheva_callister.sql:/docker-entrypoint-initdb.d/init.sql
Luzefiru marked this conversation as resolved.
Show resolved Hide resolved

adminer:
depends_on:
- db
image: adminer
restart: always
ports:
- ${ADMINER_PORT:-8080}:${ADMINER_PORT:-8080}

volumes:
budgeteer-db:
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"private": true,
"scripts": {
"prepare": "husky",
"test": "vitest",
"test": "docker-compose -f docker-compose.yml up -d db server && vitest --run && docker-compose -f docker-compose.yml down db server",
Luzefiru marked this conversation as resolved.
Show resolved Hide resolved
"server:test": "bun run --cwd=\"packages/server\" test",
"server:lint": "bun run --cwd=\"packages/server\" lint",
"server:format": "bun run --cwd=\"packages/server\" format",
Expand All @@ -36,16 +36,19 @@
"types:format": "bun run --cwd=\"packages/types\" format",
"workspace:lint": "bun run server:lint && bun run client:lint && bun run types:lint",
"workspace:format": "bun run server:format && bun run client:format && bun run types:format",
"db:up": "docker-compose -f .docker-compose.dev.yml up -d",
"db:down": "docker-compose -f .docker-compose.dev.yml down",
"db:up": "docker-compose -f docker-compose.yml up -d db",
"db:down": "docker-compose -f docker-compose.yml down db",
Luzefiru marked this conversation as resolved.
Show resolved Hide resolved
"db:migration-generate": "bun run --cwd=\"packages/server\" drizzle:generate",
"db:migration-migrate": "bun run --cwd=\"packages/server\" drizzle:migrate",
"db:migration-push": "bun run --cwd=\"packages/server\" drizzle:push",
"db:seed": "bun run --cwd=\"packages/server\" drizzle:seed",
"client:dev": "bun run --cwd=\"packages/client\" dev",
"client:build": "bun run --cwd=\"packages/client\" build",
"client:start": "bun run --cwd=\"packages/client\" start",
"server:dev": "bun run --cwd=\"packages/server\" dev",
"server:start": "bun run --cwd=\"packages/server\" start"
"server:start": "bun run --cwd=\"packages/server\" start",
"all:up": "docker-compose -f docker-compose.yml up",
"all:down": "docker-compose -f docker-compose.yml down"
},
"workspaces": [
"packages/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"usehooks-ts": "^3.1.0",
"zod": "^3.23.7"
"zod": "3.23.7"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/features/dashboard/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TransactionTypeEnum } from "@budgeteer/types"
import { convertToTitleCase } from "~/lib/convertToTitleCase"

interface TransactionProps {
type: TransactionTypeEnum
type: string
description: string
}

Expand Down
1 change: 1 addition & 0 deletions packages/client/src/hooks/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ToasterToast = ToastProps & {
action?: ToastActionElement
}

// eslint-disable-next-line
const actionTypes = {
ADD_TOAST: "ADD_TOAST",
UPDATE_TOAST: "UPDATE_TOAST",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"drizzle-orm": "^0.36.0",
"hono": "^4.6.9",
"pg": "^8.13.1",
"zod": "^3.23.7"
"zod": "3.23.7"
},
"type": "module"
}
2 changes: 2 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ app.use(logger())
app.use(cors())

app.onError((err, c) => {
console.error(err)

const response: ResponseDto<null> = {
status: HttpStatusEnum.INTERNAL_SERVER_ERROR,
data: null,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/services/config-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function loadEnvConfig() {
DB_USER: process.env["POSTGRES_USER"] ?? "postgres",
DB_DB: process.env["POSTGRES_DB"] ?? "postgres",
DB_HOST: process.env["POSTGRES_HOST"] ?? "localhost",
DB_PORT: Number(process.env["DB_PORT"]) ?? 5432,
DB_PORT: process.env["DB_PORT"] ?? 5432,
JWT_SECRET: process.env["JWT_SECRET"] ?? "secret",
}

Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/use-cases/auth/auth.use-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const AuthUseCases: IAuthUseCases = {
return response
} catch (e) {
if (e instanceof Error) {
console.log(e)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? Don't think our other use cases are logging errors since it's handled by Hono's global error handler

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah whoops, this was for debugging. I'll remove it.

throw new HTTPException(HttpStatusEnum.INTERNAL_SERVER_ERROR, { message: e.message })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserCreateDto } from "../../entities/users/user-create.dto"
import type { UserUpdateDto } from "../../entities/users/user-update.dto"
import type { UserUpdateProfilePictureDto } from "src/entities/users/user-update-profile-picture.dto"
import type { UserUpdateProfilePictureDto } from "../../entities/users/user-update-profile-picture.dto"
Luzefiru marked this conversation as resolved.
Show resolved Hide resolved
import type { UserDto, UserPublicDto } from "../../entities/users/user.dto"

export type IUserRepository = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { ResponseDto } from "../../entities/response/response.dto"
import type { UserCreateDto } from "../../entities/users/user-create.dto"

export type IAuthUseCases = {
register: (dto: UserCreateDto) => Promise<ResponseDto<string>>
login: (dto: UserCreateDto) => Promise<ResponseDto<string>>
register: (dto: UserCreateDto) => Promise<ResponseDto<string | null>>
login: (dto: UserCreateDto) => Promise<ResponseDto<string | null>>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ResponseDto } from "../../entities/response/response.dto"
import type { UserCreateDto } from "../../entities/users/user-create.dto"
import type { UserUpdateProfilePictureDto } from "src/entities/users/user-update-profile-picture.dto"
import type { UserUpdateDto } from "src/entities/users/user-update.dto"
import type { UserUpdateProfilePictureDto } from "../../entities/users/user-update-profile-picture.dto"
import type { UserUpdateDto } from "../../entities/users/user-update.dto"
import type { UserDto, UserPublicDto } from "../../entities/users/user.dto"

export type IUserUseCases = {
Expand Down