Skip to content
Draft
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
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@ethersphere/bee-factory": "^0.4.0",
"@fluffy-spoon/substitute": "^1.208.0",
"@jest/types": "^27.5.1",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
Expand All @@ -64,6 +65,7 @@
"dependencies": {
"@ethersphere/bee-js": "^4.0.0",
"@ethersphere/swarm-cid": "^0.1.0",
"cors": "^2.8.5",
"express": "^4.17.3",
"http-proxy-middleware": "^2.0.4",
"prom-client": "^14.0.1",
Expand Down
13 changes: 11 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { CorsOptions } from 'cors'

export interface AppConfig {
beeApiUrl: string
authorization?: string
Expand All @@ -10,6 +12,7 @@ export interface AppConfig {
export interface ServerConfig {
hostname: string
port: number
corsOptions: CorsOptions
}

interface StampsConfigHardcoded {
Expand Down Expand Up @@ -41,6 +44,7 @@ export type EnvironmentVariables = Partial<{
// Server
PORT: string
HOSTNAME: string
CORS_ORIGIN: string

// CID subdomain support
CID_SUBDOMAINS: string
Expand Down Expand Up @@ -70,6 +74,7 @@ export const DEFAULT_POSTAGE_USAGE_THRESHOLD = 0.7
export const DEFAULT_POSTAGE_USAGE_MAX = 0.9
export const DEFAULT_POSTAGE_REFRESH_PERIOD = 60_000
export const DEFAULT_LOG_LEVEL = 'info'
export const DEFAULT_CORS_ORIGIN = true

export const logLevel =
process.env.LOG_LEVEL && SUPPORTED_LEVELS.includes(process.env.LOG_LEVEL as SupportedLevels)
Expand All @@ -94,8 +99,12 @@ export function getAppConfig({
}
}

export function getServerConfig({ PORT, HOSTNAME }: EnvironmentVariables = {}): ServerConfig {
return { hostname: HOSTNAME || DEFAULT_HOSTNAME, port: Number(PORT || DEFAULT_PORT) }
export function getServerConfig({ PORT, HOSTNAME, CORS_ORIGIN }: EnvironmentVariables = {}): ServerConfig {
return {
hostname: HOSTNAME || DEFAULT_HOSTNAME,
port: Number(PORT || DEFAULT_PORT),
corsOptions: { origin: CORS_ORIGIN ?? DEFAULT_CORS_ORIGIN },
}
}

export function getStampsConfig({
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function main() {
// Configuration
const stampConfig = getStampsConfig(process.env as EnvironmentVariables)
const appConfig = getAppConfig(process.env as EnvironmentVariables)
const { hostname, port } = getServerConfig(process.env as EnvironmentVariables)
const { hostname, port, corsOptions } = getServerConfig(process.env as EnvironmentVariables)

logger.debug('proxy config', appConfig)
logger.debug('server config', { hostname: hostname, port })
Expand All @@ -23,10 +23,10 @@ async function main() {
logger.info('starting postage stamp manager')
await stampManager.start(stampConfig)
logger.info('starting the proxy')
app = createApp(appConfig, stampManager)
app = createApp(appConfig, stampManager, corsOptions)
} else {
logger.info('starting the app without postage stamps management')
app = createApp(appConfig)
app = createApp(appConfig, undefined, corsOptions)
}

// Start the Proxy
Expand Down
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { StampsManager } from './stamps'
import { logger } from './logger'
import * as bzzLink from './bzz-link'
import { register } from './metrics'
import cors, { CorsOptions } from 'cors'

const SWARM_STAMP_HEADER = 'swarm-postage-batch-id'

Expand All @@ -20,6 +21,7 @@ export const POST_PROXY_ENDPOINTS = ['/bzz', '/bytes', '/chunks', '/feeds/:owner
export const createApp = (
{ hostname, beeApiUrl, authorization, cidSubdomains, ensSubdomains, removePinHeader }: AppConfig,
stampManager: StampsManager | undefined = undefined,
corsOptions: CorsOptions | undefined,
): Application => {
const commonOptions: Options = {
target: beeApiUrl,
Expand All @@ -30,6 +32,8 @@ export const createApp = (
// Create Express Server
const app = express()

app.use(cors(corsOptions))

if (hostname) {
const subdomainOffset = hostname.split('.').length
app.set('subdomain offset', subdomainOffset)
Expand Down