Skip to content

Commit

Permalink
added docker file and necessary utils for production environment
Browse files Browse the repository at this point in the history
  • Loading branch information
aruokhai committed Nov 5, 2024
1 parent 4572869 commit a7a4fb4
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ lerna-debug.log*

#e2e
/e2e/.logs

#dev
/config/dev.config.yaml
19 changes: 19 additions & 0 deletions config/config.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
db:
path: ${SQLITE_PATH}
synchronize: false
app:
port: ${PORT}
network: ${NETWORK}
requestRetry:
delay: ${DELAY} # delay in Milliseconds
count: ${COUNT}
providerType: ${PROVIDER_TYPE}
esplora:
url: ${HOST}
batchSize: ${BATCH_SIZE}
bitcoinCore:
protocol: ${PROTOCOL} # http | https
rpcHost: ${RPC_HOST}
rpcPass: ${RPC_PASS}
rpcUser: ${RPC_USER}
rpcPort: ${RPC_PORT}
20 changes: 0 additions & 20 deletions config/config.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions config/dev.config.yaml

This file was deleted.

44 changes: 44 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Stage 1: Build the NestJS application
FROM node:18-alpine AS builder

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json for efficient layer caching
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the application
RUN npm run build

# Stage 2: Production image
FROM node:18-alpine AS production

#Set the working directory in the container
WORKDIR /app

# Generate config.yml by replacing placeholders in config.yml.template
RUN apk add --no-cache gettext

#Copy the compiled application from the build stage
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/migrations ./migrations
COPY --from=builder /app/migrate.js ./migrate.js

#Copy package.json and install only production dependencies
COPY package.json ./
RUN npm install --only=production

# Set environment variables for SQLite path and app port
ENV SQLITE_PATH="/app/.silent-pay-indexer/db/database.sqlite"
ENV APP_PORT="3000"

EXPOSE $APP_PORT

# Set up entrypoint to handle database migration and config generation
ENTRYPOINT ["sh", "-c", "node migrate.js && envsubst < dist/config/config.template.yaml > dist/config/config.yaml && node dist/main.js"]
47 changes: 47 additions & 0 deletions migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs');
const path = require('path');

// Set the database directory and file path
const dbPath = process.env.SQLITE_PATH;
const dbDir = path.dirname(dbPath);

// Ensure the database directory exists, creating it if necessary
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}

const db = new sqlite3.Database(dbPath);

// Function to run migrations
async function runMigrations() {
const migrationsDir = path.join(__dirname, 'migrations');

// Read migration files in sorted order
const files = fs.readdirSync(migrationsDir).sort();

for (const file of files) {
const filePath = path.join(migrationsDir, file);
const sql = fs.readFileSync(filePath, 'utf-8');
console.log(`Running migration: ${file}`);

// Execute each SQL file
await new Promise((resolve, reject) => {
db.exec(sql, (err) => {
if (err) reject(err);
else resolve();
});
});
}
console.log('All migrations applied.');
}

// Run migrations, then close the database connection
runMigrations()
.then(() => db.close())
.catch((err) => {
console.error('Migration failed:', err);
db.close();
});
8 changes: 8 additions & 0 deletions migrations/001_create_transaction_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS "transaction" (
id TEXT PRIMARY KEY,
blockHeight INTEGER NOT NULL,
blockHash TEXT NOT NULL,
scanTweak TEXT NOT NULL,
outputs TEXT NOT NULL,
isSpent BOOLEAN NOT NULL
);
4 changes: 4 additions & 0 deletions migrations/002_create_operation_state_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS "operation_state" (
id TEXT PRIMARY KEY,
state TEXT
);

0 comments on commit a7a4fb4

Please sign in to comment.