Skip to content

Commit

Permalink
Use in-server proxy for local development
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 9, 2024
1 parent bcd800d commit 98be593
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"packageManager": "[email protected]",
"scripts": {
"test": "FORCE_COLOR=1 pnpm run -r --include-workspace-root /^test:/",
"start": "FORCE_COLOR=1 pnpm run -F server -F web -F proxy start",
"start": "FORCE_COLOR=1 pnpm run -F server -F web start",
"format": "pnpm run -r --include-workspace-root /^format:/",
"clean": "pnpm run -r --include-workspace-root /^clean:/",
"build": "pnpm run -r build",
Expand Down
3 changes: 1 addition & 2 deletions proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export interface ProxyConfig {
timeout: number
}

export const DEFAULT_PROXY_CONFIG: ProxyConfig = {
allowsFrom: process.env.PROXY_ORIGIN ?? '^http:\\/\\/localhost:5173',
export const DEFAULT_PROXY_CONFIG: Omit<ProxyConfig, 'allowsFrom'> = {
maxSize: 10 * 1024 * 1024,
timeout: 2500
}
Expand Down
15 changes: 9 additions & 6 deletions proxy/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import { createProxy, DEFAULT_PROXY_CONFIG } from './index.ts'

const PORT = process.env.PORT ?? 5284

let allowsFrom: string
let allowsFrom: string | undefined
if (process.env.NODE_ENV !== 'production') {
allowsFrom = '^http:\\/\\/localhost:5173'
} else if (process.env.STAGING) {
allowsFrom =
'^https:\\/\\/dev.slowreader.app' +
'|https:\\/\\/(preview-\\d+---)?staging-web-jfj4bxwwxq-ew.a.run.app'
} else {
allowsFrom = '^https:\\/\\/slowreader.app'
allowsFrom = process.env.PROXY_ORIGIN
}

if (!allowsFrom) {
process.stderr.write(
styleText('red', 'Set PROXY_ORIGIN environment variable\n')
)
process.exit(0)
}

let proxy = createServer(createProxy({ ...DEFAULT_PROXY_CONFIG, allowsFrom }))
Expand Down
6 changes: 6 additions & 0 deletions server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type AssetsPaths =
export type Config = {
db: string
env: 'development' | 'production' | 'test'
proxyOrigin: string | undefined
staging: boolean
} & AssetsPaths

Expand Down Expand Up @@ -33,9 +34,14 @@ export function getConfig(from: Record<string, string | undefined>): Config {
if (env !== 'test' && env !== 'production' && env !== 'development') {
throw new Error('Unknown NODE_ENV')
}
let proxyOrigin = from.PROXY_ORIGIN
if (!proxyOrigin && env === 'development') {
proxyOrigin = '^http:\\/\\/localhost:5173'
}
return {
db: from.DATABASE_URL ?? getDefaultDatabase(env),
env,
proxyOrigin,
staging: !!from.STAGING,
...getPaths(from)
}
Expand Down
11 changes: 9 additions & 2 deletions server/modules/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import {
type ProxyConfig
} from '@slowreader/proxy'

import { config } from '../config.ts'

export default (server: BaseServer, opts: Partial<ProxyConfig> = {}): void => {
if (!process.env.PROXY_ORIGIN) return
let proxy = createProxy({ ...DEFAULT_PROXY_CONFIG, ...opts })
let allowsFrom = config.proxyOrigin ?? opts.allowsFrom
if (!allowsFrom) return
let proxy = createProxy({
...DEFAULT_PROXY_CONFIG,
...opts,
allowsFrom
})
server.http((req, res) => {
if (req.url!.startsWith('/proxy/')) {
proxy(req, res)
Expand Down
21 changes: 21 additions & 0 deletions server/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,39 @@ test('checks environment', () => {
equal(getConfig({ STAGING: '1' }).staging, true)
})

test('sets proxy origin', () => {
equal(
getConfig({ NODE_ENV: 'development' }).proxyOrigin,
'^http:\\/\\/localhost:5173'
)
equal(
getConfig({ DATABASE_URL, NODE_ENV: 'production' }).proxyOrigin,
undefined
)
equal(
getConfig({
DATABASE_URL,
NODE_ENV: 'production',
PROXY_ORIGIN: '^http:\\/\\/slowreader.app'
}).proxyOrigin,
'^http:\\/\\/slowreader.app'
)
})

test('passes keys', () => {
deepStrictEqual(
getConfig({
ASSETS_DIR: './dist/',
DATABASE_URL,
NODE_ENV: 'production',
PROXY_ORIGIN: '^http:\\/\\/slowreader.app',
ROUTES_FILE: './routes.regexp'
}),
{
assets: './dist/',
db: DATABASE_URL,
env: 'production',
proxyOrigin: '^http:\\/\\/slowreader.app',
routes: './routes.regexp',
staging: false
}
Expand Down
2 changes: 1 addition & 1 deletion web/main/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enableWarnings()

let PROXY_URL: string
if (location.hostname === 'localhost') {
PROXY_URL = 'http://localhost:5284/'
PROXY_URL = 'http://localhost:31337/proxy/'
} else if (location.hostname === 'slowreader.app') {
PROXY_URL = 'https://proxy.slowreader.app/'
} else {
Expand Down

0 comments on commit 98be593

Please sign in to comment.