Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger and prefix env variables #5

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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