Skip to content

Commit

Permalink
ops: improve env sample, optimize dockerfile, add couchdb sidecar helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonraimondi committed Mar 20, 2024
1 parent fcb89f0 commit 5a6f0b5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 56 deletions.
43 changes: 31 additions & 12 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
LOG_LEVEL=debug
NODE_ENV=development
PORT=3089

ALLOW_LIST=jasonraimondi.com,github.com
#CACHE_CONTROL="public, max-age=86400, immutable"

STORAGE_PROVIDER=
#BROWSER_TIMEOUT=10000
#BROWSER_WAIT_UNTIL=domcontentloaded

COUCH_DB_PROTOCOL=
COUCH_DB_HOST=
COUCH_DB_PORT=
COUCH_DB_USER=
COUCH_DB_PASS=
#POOL_IDLE_TIMEOUT_MS=15000
#POOL_MAX=10
#POOL_MAX_WAITING_CLIENTS=50
#POOL_MIN=2

AWS_ACCESS_KEY=
AWS_SECRET_KEY=
AWS_REGION=
AWS_BUCKET=
METRICS=true
STORAGE_PROVIDER=stub

BROWSER_TIMEOUT=10000
BROWSER_WAIT_UNTIL=domcontentloaded
#STORAGE_PROVIDER=s3
#AWS_BUCKET=
#AWS_ACCESS_KEY_ID=
#AWS_SECRET_ACCESS_KEY=
#AWS_DEFAULT_REGION=us-east-1
#AWS_ENDPOINT_URL_S3=
#AWS_FORCE_PATH_STYLE=false

#STORAGE_PROVIDER=couchdb
#COUCH_DB_HOST=
#COUCH_DB_PASS=
#COUCH_DB_PORT=
#COUCH_DB_PROTOCOL=
#COUCH_DB_USER=
#COUCHDB_DATABASE=images

#STORAGE_PROVIDER=filesystem
#IMAGE_STORAGE_PATH=

#CRYPTO_KEY=
11 changes: 3 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ RUN npm install -g pnpm \

FROM baserepo as builder
WORKDIR /
RUN apt-get update \
&& apt-get install -y wget
USER pwuser
WORKDIR /app
COPY package.json pnpm-lock.yaml /app
COPY package.json pnpm-lock.yaml /app/
RUN pnpm install --production false
COPY tsconfig.json tsconfig.build.json /app/
COPY src /app/src
Expand All @@ -20,12 +18,9 @@ RUN pnpm build

FROM baserepo
ENV DOCKER=1
RUN apt-get update \
&& apt-get install -y tini
USER pwuser
COPY --from=builder --chown=pwuser:pwuser /app/package.json /app/pnpm-lock.yaml /app/
RUN pnpm install --production && pnpm exec playwright install
RUN pnpm install --production && pnpm exec playwright install chromium
COPY --from=builder --chown=pwuser:pwuser /app/dist /app/dist
EXPOSE 3000
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD node -r dotenv/config dist/main.js
CMD ["node", "-r", "dotenv/config", "dist/main.js"]
53 changes: 35 additions & 18 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,51 @@ services:
- 3089:3089
environment:
METRICS: true
LOG_LEVEL: debug
NODE_ENV: debug
BROWSER_WAIT_UNTIL: networkidle
STORAGE_PROVIDER: s3
AWS_ACCESS_KEY_ID: miniominiominio
AWS_SECRET_ACCESS_KEY: miniominiominio
AWS_DEFAULT_REGION: us-east-1
AWS_ENDPOINT_URL_S3: http://minio:9000
AWS_FORCE_PATH_STYLE: true
AWS_BUCKET: url-to-png-uploads
# STORAGE_PROVIDER: couchdb
# COUCH_DB_PROTOCOL: http
# COUCH_DB_HOST: couchdb
# COUCH_DB_PORT: 5984
# COUCH_DB_USER: admin
# COUCH_DB_PASS: password
# STORAGE_PROVIDER: s3
# AWS_ACCESS_KEY_ID: miniominiominio
# AWS_SECRET_ACCESS_KEY: miniominiominio
# AWS_DEFAULT_REGION: us-east-1
# AWS_ENDPOINT_URL_S3: http://minio:9000
# AWS_FORCE_PATH_STYLE: true
# AWS_BUCKET: url-to-png-uploads
STORAGE_PROVIDER: couchdb
COUCH_DB_PROTOCOL: http
COUCH_DB_HOST: couchdb
COUCH_DB_PORT: 5984
COUCH_DB_USER: admin
COUCH_DB_PASS: password

couchdb:
image: couchdb
container_name: couchdb
restart: always
ports:
- "5984:5984"
- 5984:5984
environment:
- COUCHDB_USER=admin
- COUCHDB_PASSWORD=password
volumes:
- couchdb-data:/opt/couchdb/data

couchdb-sidecar:
image: curlimages/curl
depends_on:
- couchdb
command: |
sh -c "
until curl -f http://admin:password@couchdb:5984/_up; do
echo 'Waiting for CouchDB to be ready...';
sleep 1;
done;
echo 'CouchDB is ready, creating databases...';
curl -X PUT http://admin:password@couchdb:5984/_users;
curl -X PUT http://admin:password@couchdb:5984/_replicator;
curl -X PUT http://admin:password@couchdb:5984/_global_changes;
curl -X PUT http://admin:password@couchdb:5984/images;
echo 'Databases created successfully.';
"
minio:
image: minio/minio
command: ["server", "/data", "--console-address", ":9001"]
Expand All @@ -45,8 +62,8 @@ services:
- minio-config:/root/.minio
- minio-data:/data
environment:
MINIO_ACCESS_KEY: miniominiominio
MINIO_SECRET_KEY: miniominiominio
MINIO_ROOT_USER: miniominiominio
MINIO_ROOT_PASSWORD: miniominiominio
MINIO_HTTP_TRACE: /dev/stdout

minio-mc:
Expand Down
4 changes: 4 additions & 0 deletions src/lib/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export function createImageRenderService(browserPool: BrowserPool) {
case "networkidle":
navigationOptions.waitUntil = process.env.BROWSER_WAIT_UNTIL;
break;
default:
navigationOptions.waitUntil = "domcontentloaded";
break;
}

return new ImageRenderService(browserPool, navigationOptions);
Expand Down Expand Up @@ -59,6 +62,7 @@ export function createImageStorageService(): ImageStorage {
const filePath = process.env.IMAGE_STORAGE_PATH!;
imageStorage = new FileSystemStorageProvider(filePath);
break;
case "stub":
default:
imageStorage = new StubStorageProvider();
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/storage/couch-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ export class CouchDbStorageProvider implements ImageStorage {
constructor(private readonly couchDB: couchDBNano.ServerScope) {}

get images() {
return this.couchDB.use("images");
return this.couchDB.use(process.env.COUCHDB_DATABASE ?? "images");
}

public async fetchImage(imageId: string): Promise<null | Buffer> {
imageId = md5(imageId);
const imageMd5 = md5(imageId);
try {
return await this.images.attachment.get(imageId, "urlto.png");
return await this.images.attachment.get(imageMd5, `${imageId}.png`);
} catch (err) {
return null;
}
}

public async storeImage(imageId: string, image: Buffer): Promise<boolean> {
const images = this.images;
imageId = md5(imageId);
const imageMd5 = md5(imageId);
try {
await images.attachment.get(imageId, "urlto.png");
await images.attachment.get(imageMd5, `${imageId}.png`);
return true;
} catch (err) {
await images.attachment.insert(imageId, "urlto.png", image, "image/png");
await images.attachment.insert(imageMd5, `${imageId}.png`, image, "image/png");
}

return true;
Expand Down
12 changes: 0 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ import {
} from "./lib/factory.js";
import { logger } from "./lib/logger.js";

if (process.env.AWS_ACCESS_KEY) {
logger.warn("AWS_ACCESS_KEY is deprecated, please use AWS_ACCESS_KEY_ID");
}

if (process.env.AWS_SECRET_KEY) {
logger.warn("AWS_SECRET_KEY is deprecated, please use AWS_SECRET_ACCESS_KEY");
}

if (process.env.AWS_REGION) {
logger.warn("AWS_REGION is deprecated, please use AWS_DEFAULT_REGION");
}

let server: ReturnType<typeof serve>;

async function main() {
Expand Down

0 comments on commit 5a6f0b5

Please sign in to comment.