Skip to content

Commit

Permalink
feat: add homepage env
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Nov 23, 2023
1 parent 95b34a5 commit 651f6e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface AppConfig {
removePinHeader?: boolean
exposeHashedIdentity?: boolean
readinessCheck?: boolean
homepage?: string
}

export interface ServerConfig {
Expand Down Expand Up @@ -86,6 +87,9 @@ export type EnvironmentVariables = Partial<{
POSTAGE_REFRESH_PERIOD: string
POSTAGE_EXTENDSTTL: string
REUPLOAD_PERIOD: string

// Homepage
HOMEPAGE: string
}>

export const SUPPORTED_LEVELS = ['critical', 'error', 'warn', 'info', 'verbose', 'debug'] as const
Expand Down Expand Up @@ -119,6 +123,7 @@ export function getAppConfig({
REMOVE_PIN_HEADER,
EXPOSE_HASHED_IDENTITY,
READINESS_CHECK,
HOMEPAGE,
}: EnvironmentVariables = {}): AppConfig {
return {
hostname: HOSTNAME || DEFAULT_HOSTNAME,
Expand All @@ -131,6 +136,7 @@ export function getAppConfig({
removePinHeader: REMOVE_PIN_HEADER ? REMOVE_PIN_HEADER === 'true' : true,
exposeHashedIdentity: EXPOSE_HASHED_IDENTITY === 'true',
readinessCheck: READINESS_CHECK === 'true',
homepage: HOMEPAGE,
}
}

Expand Down
29 changes: 28 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bee, BeeDebug } from '@ethersphere/bee-js'
import axios from 'axios'
import bodyParser from 'body-parser'
import { Arrays } from 'cafe-utility'
import { Arrays, Strings } from 'cafe-utility'
import express, { Application } from 'express'
import { AppConfig, DEFAULT_HOSTNAME } from './config'
import { HASHED_IDENTITY_HEADER, fetchBeeIdentity, getHashedIdentity } from './identity'
Expand All @@ -22,6 +23,7 @@ export const createApp = (
removePinHeader,
exposeHashedIdentity,
readinessCheck,
homepage,
}: AppConfig,
stampManager?: StampsManager,
): Application => {
Expand Down Expand Up @@ -119,6 +121,31 @@ export const createApp = (
),
})

if (homepage) {
app.use(async (req, res, next) => {
try {
const url = Strings.joinUrl(beeApiUrl, 'bzz', homepage, req.url)
logger.info('attempting to fetch homepage', { url })

// attempt to fetch homepage
const response = await axios.get(url, {
responseType: 'arraybuffer',
})

if (response.status !== 200) {
throw Error('Homepage not available')
}
const contentType = response.headers['content-type']
res.set('content-type', contentType || 'application/octet-stream')
res.send(await response.data)

return
} catch (error) {
next()
}
})
}

app.use(express.static('public'))

app.use((_req, res) => res.sendStatus(404))
Expand Down

0 comments on commit 651f6e7

Please sign in to comment.