Skip to content

Commit

Permalink
load config
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Jan 16, 2025
1 parent 5a2a0df commit 34d2e05
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 53 deletions.
59 changes: 37 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
FROM oven/bun as deps

## NOTE
# This Dockerfile builds the frontend and backend separately,
# frontend uses npm and backend requires bun.
# This separation is a temporary solution for a Bun issue with rsbuild,
# see: https://github.com/oven-sh/bun/issues/11628

# Frontend deps & build stage
FROM node:20 as frontend-builder
WORKDIR /app

# Copy package files for all workspaces
COPY package.json bun.lockb turbo.json ./
# Copy frontend package files
COPY frontend/package.json ./frontend/
COPY backend/package.json ./backend/

# Install dependencies
RUN bun install
# Install frontend dependencies
RUN cd frontend && npm install

# Copy frontend source code
COPY frontend ./frontend

# Build frontend
RUN cd frontend && npm run build

# Build stage
FROM oven/bun as builder
# Backend deps & build stage
FROM oven/bun as backend-builder
WORKDIR /app

# Set NODE_ENV for build process
ENV NODE_ENV="production"
# Copy backend package files
COPY package.json ./
COPY backend/package.json ./backend/

# Copy all files from deps stage including node_modules
COPY --from=deps /app ./
# Install backend dependencies
RUN cd backend && bun install

# Copy source code
COPY . .
# Copy backend source code
COPY backend ./backend

# Build both frontend and backend
RUN bun run build
# Build backend
RUN cd backend && bun run build

# Production stage
FROM oven/bun as production
Expand All @@ -40,11 +51,12 @@ COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs
RUN mkdir -p /litefs /var/lib/litefs /public && \
chown -R bun:bun /litefs /var/lib/litefs /public

# Copy only necessary files from builder
COPY --from=builder --chown=bun:bun /app/package.json /app/bun.lockb /app/turbo.json ./
COPY --from=builder --chown=bun:bun /app/node_modules ./node_modules
COPY --from=builder --chown=bun:bun /app/frontend/dist ./frontend/dist
COPY --from=builder --chown=bun:bun /app/backend/dist ./backend/dist
# Copy only necessary files from builders
COPY --from=backend-builder --chown=bun:bun /app/package.json ./
COPY --chown=bun:bun curate.config.json ./

COPY --from=frontend-builder --chown=bun:bun /app/frontend/dist ./frontend/dist
COPY --from=backend-builder --chown=bun:bun /app/backend/dist ./backend/dist

# Set environment variables
ENV DATABASE_URL="file:/litefs/db"
Expand All @@ -54,5 +66,8 @@ ENV NODE_ENV="production"
# Expose the port
EXPOSE 3000

# Copy LiteFS configuration
COPY --chown=bun:bun litefs.yml /etc/litefs.yml

# Start LiteFS (runs app with distributed file system for SQLite)
ENTRYPOINT ["litefs", "mount"]
4 changes: 2 additions & 2 deletions backend/src/services/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class ConfigService {
private configPath: string;

private constructor() {
// Default to local config file path
this.configPath = path.resolve(process.cwd(), "../curate.config.json");
// Always look for config relative to the distribution directory
this.configPath = path.resolve(__dirname, "../../../../curate.config.json");
}

public static getInstance(): ConfigService {
Expand Down
34 changes: 11 additions & 23 deletions docs/docs/developers/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ fly auth login
bun run deploy:init
```

2. Create the LiteFS volume (see [LiteFS Spreedrun](https://fly.io/docs/litefs/speedrun/) for more information):
This will create the Fly App, the LiteFS volume (see [LiteFS Spreedrun](https://fly.io/docs/litefs/speedrun/) for more information), and attach Consul for LiteFS cluster management (this sets the FLY_CONSUL_URL secret for the app, which is required for LitefS leases)

2. The app will crash the first time, because your Fly App needs environment variables set (sorry, you have to do this manually):

```bash
bun run deploy:volumes
fly secrets set TWITTER_USERNAME=your_twitter_username
fly secrets set TWITTER_PASSWORD=your_twitter_password
fly secrets set TWITTER_EMAIL=your_twitter_email
```

3. Attach Consul for LiteFS cluster management (this sets the FLY_CONSUL_URL secret for the app, which is required for LitefS leases):
For distribution services, these will hydrate the curate.config.json:

```bash
bun run deploy:consul
# Telegram
fly secrets set TELEGRAM_BOT_TOKEN=your_bot_token
fly secrets set TELEGRAM_CHANNEL_ID=your_channel_id
```

4. Deploy the application:
2. Then redeploy the application. Use this command for any future deployments:

```bash
bun run deploy
Expand Down Expand Up @@ -85,24 +91,6 @@ The deployment includes:
- `litefs.yml`: LiteFS configuration
- `Dockerfile`: Container and LiteFS setup

### Environment Variables

Make sure to configure your environment variables in Fly.io:

```bash
fly secrets set TWITTER_USERNAME=your_twitter_username
fly secrets set TWITTER_PASSWORD=your_twitter_password
fly secrets set TWITTER_EMAIL=your_twitter_email
```

For distribution services, these will hydrate the curate.config.json:

```bash
# Telegram
fly secrets set TELEGRAM_BOT_TOKEN=your_bot_token
fly secrets set TELEGRAM_CHANNEL_ID=your_channel_id
```

### Monitoring

Monitor your deployment:
Expand Down
4 changes: 0 additions & 4 deletions litefs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
# so that those transactions can be saved and shipped to replicas.
fuse:
dir: "/litefs"
mount-timeout: "5s"
allow-other: true
directories:
- path: "/public"
read-only: false

# The data section describes settings for the internal LiteFS storage. We'll
# mount a volume to the data directory so it can be persisted across restarts.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"build": "bunx turbo run build",
"start": "NODE_ENV=production bun run backend/dist/index.js",
"lint": "bunx turbo run lint",
"deploy:init": "fly launch",
"deploy:consul": "fly consul attach",
"deploy:init": "fly launch && fly consul attach",
"deploy": "fly deploy",
"fmt": "prettier --write '**/*.{js,jsx,ts,tsx,json}'",
"fmt:check": "prettier --check '**/*.{js,jsx,ts,tsx,json}'"
Expand Down

0 comments on commit 34d2e05

Please sign in to comment.