diff --git a/.env b/.env index ef70586..a46ac59 100644 --- a/.env +++ b/.env @@ -11,3 +11,5 @@ POSTGRES_DB=affine WORKERS_NODE_ENV=development WORKERS_ELYSIA_PORT=3000 WORKERS_ALLOWED_ORIGINS=http://localhost +WORKERS_ELYSIA_PREFIX= +WORKERS_ELYSIA_LOGGER_LEVEL=debug diff --git a/Caddyfile b/Caddyfile index 512bdb7..98337cd 100644 --- a/Caddyfile +++ b/Caddyfile @@ -3,7 +3,9 @@ } http://localhost { + handle_path /api/worker/* { + reverse_proxy workers:3000 + } + reverse_proxy * affine:3010 - reverse_proxy /api/worker/link-preview workers:3000 - reverse_proxy /api/worker/image-proxy workers:3000 } diff --git a/bun.lockb b/bun.lockb index 046c464..6e242f3 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/compose.yml b/compose.yml index 201aab3..52a7b5a 100644 --- a/compose.yml +++ b/compose.yml @@ -22,6 +22,8 @@ services: NODE_ENV: '${WORKERS_NODE_ENV}' ELYSIA_PORT: '${WORKERS_ELYSIA_PORT}' ALLOWED_ORIGINS: '${WORKERS_ALLOWED_ORIGINS}' + ELYSIA_PREFIX: '${WORKERS_ELYSIA_PREFIX}' + ELYSIA_LOGGER_LEVEL: '${WORKERS_ELYSIA_LOGGER_LEVEL}' # Uncomment the following to test the production image of the workers # workers: @@ -32,6 +34,7 @@ services: # container_name: affine_workers # environment: # ALLOWED_ORIGINS: '${WORKERS_ALLOWED_ORIGINS}' + # ELYSIA_PREFIX: '${WORKERS_ELYSIA_PREFIX}' affine: image: 'ghcr.io/toeverything/affine-graphql:stable' diff --git a/docker/prod/Dockerfile b/docker/prod/Dockerfile index 0e7acd4..f67e902 100644 --- a/docker/prod/Dockerfile +++ b/docker/prod/Dockerfile @@ -21,6 +21,8 @@ COPY --from=builder --chown=bun:bun /home/bun/affine-workers/node_modules ./node ENV NODE_ENV=production ENV ELYSIA_PORT=3000 ENV ALLOWED_ORIGINS=localhost +ENV ELYSIA_PREFIX=/api/workers +ENV ELYSIA_LOGGER_LEVEL=debug USER bun diff --git a/src/index.test.ts b/src/index.test.ts index 27c6eb4..9a796c3 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -5,7 +5,7 @@ import { app } from '.'; describe('/link-preview', () => { it('fails with a wrong URL', async () => { const response = await app.handle( - new Request('http://localhost/api/worker/link-preview', { + new Request('http://localhost/link-preview', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -25,7 +25,7 @@ describe('/link-preview', () => { it('fetches metadata for a valid URL', async () => { const response = await app.handle( - new Request('http://localhost/api/worker/link-preview', { + new Request('http://localhost/link-preview', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -52,7 +52,7 @@ describe('/link-preview', () => { describe('/image-proxy', () => { it('fails with a wrong URL', async () => { const response = await app.handle( - new Request('http://localhost/api/worker/image-proxy', { + new Request('http://localhost/image-proxy', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -72,7 +72,7 @@ describe('/image-proxy', () => { it('proxies an image in webp', async () => { const response = await app.handle( - new Request('http://localhost/api/worker/image-proxy', { + new Request('http://localhost/image-proxy', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -89,7 +89,7 @@ describe('/image-proxy', () => { it('proxies an image in avif', async () => { const response = await app.handle( - new Request('http://localhost/api/worker/image-proxy', { + new Request('http://localhost/image-proxy', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/index.ts b/src/index.ts index 6f583a5..62ff22b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,10 +9,10 @@ import { getLinkPreviewMetadata } from './utils/link-preview'; export const ALLOWED_ORIGINS = Bun.env.ALLOWED_ORIGINS?.split(',') ?? []; export const ELYSIA_PORT = Number.parseInt(Bun.env.ELYSIA_PORT ?? '3000'); -export const app = new Elysia({ prefix: '/api/worker' }) +export const app = new Elysia({ prefix: Bun.env.ELYSIA_PREFIX }) .use( logger({ - level: 'debug', + level: Bun.env.ELYSIA_LOGGER_LEVEL || 'debug', }), ) .use(