Skip to content

Commit

Permalink
Add logger and prefix env variables (#5)
Browse files Browse the repository at this point in the history
* Add logger and prefix env variables

* Fix tests

---------

Co-authored-by: YummYume <[email protected]>
  • Loading branch information
YummYume and YummYume authored Nov 5, 2024
1 parent a9a7b3b commit 18e9b41
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions docker/prod/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 18e9b41

Please sign in to comment.