Skip to content

Commit

Permalink
feat: merge api
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Jun 3, 2024
1 parent c91d5be commit ab75940
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 157 deletions.
1 change: 0 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:

env:
BEE_API_URL: 'http://127.0.0.1:1633'
BEE_DEBUG_API_URL: 'http://127.0.0.1:1635'

jobs:
nodejs:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ The proxy can manage postage stamps for you in 4 modes of operation:
1. It can just proxy requests without manipulating the request
2. It can add/replace the request postage stamp with one provided through environment variable `POSTAGE_STAMP`
3. It can add/replace the request postage stamp with an auto-bought stamp or existing stamp that fulfils the amount,
depth and is not too full or about to expire. To enable this, provide at minimum `POSTAGE_DEPTH`, `POSTAGE_AMOUNT`
and `BEE_DEBUG_API_URL`.
depth and is not too full or about to expire. To enable this, provide at minimum `POSTAGE_DEPTH` and
`POSTAGE_AMOUNT`.
4. It can extend the TTL of a stamp that is about to expire. To enable this, set `POSTAGE_EXTENDSTTL=true`, provide
`POSTAGE_AMOUNT`, `POSTAGE_DEPTH` with the desired amount to use and `POSTAGE_TTL_MIN` above with a number above or
equal to 60.
Expand Down Expand Up @@ -132,7 +132,6 @@ npm run start
| Name | Default Value | Description |
| ----------------------- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| BEE_API_URL | http://localhost:1633 | URL of the Bee node API |
| BEE_DEBUG_API_URL | http://localhost:1635 | URL of the Bee node Debug API. Required for postage stamps autobuy and hashed identity header. |
| AUTH_SECRET | undefined | Authentication secret, disabled if not set (this secret is checked in the request header `authorization`). |
| HOSTNAME | localhost | Hostname of the proxy. Required for Bzz.link support. |
| PORT | 3000 | Port of the proxy. |
Expand Down
5 changes: 2 additions & 3 deletions dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ networks:
external: true # To be used with bee factory, it connects to its network, disable when using with bee dev
driver: bridge
volumes:
prometheus_data: {}
grafana_data: {}
prometheus_data: {}
grafana_data: {}
services:
prometheus:
image: prom/prometheus:v2.20.1
Expand Down Expand Up @@ -46,7 +46,6 @@ services:
environment:
- PORT=4000
- BEE_API_URL=http://swarm-test-queen:1633 # http://bee:1633 for bee dev
- BEE_DEBUG_API_URL=http://swarm-test-queen:1635 # http://bee:1635 for bee dev
- POSTAGE_DEPTH=19
- POSTAGE_AMOUNT=100000
- POSTAGE_USAGE_THRESHOLD=0.49
Expand Down
90 changes: 35 additions & 55 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"typescript": "^4.8.3"
},
"dependencies": {
"@ethersphere/bee-js": "^6.8.1",
"@ethersphere/bee-js": "^7.0.0",
"@ethersphere/swarm-cid": "^0.1.0",
"axios": "^1.5.1",
"body-parser": "^1.20.2",
Expand Down
17 changes: 6 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface AppConfig {
beeApiUrl: string
beeDebugApiUrl: string
authorization?: string
allowlist?: string[]
hostname?: string
Expand All @@ -27,7 +26,7 @@ export interface StampsConfigExtends {
depth: number
amount: string
refreshPeriod: number
beeDebugApiUrl: string
beeApiUrl: string
}

export interface ContentConfigReupload {
Expand All @@ -39,7 +38,7 @@ export interface StampsConfigAutobuy {
mode: 'autobuy'
depth: number
amount: string
beeDebugApiUrl: string
beeApiUrl: string
usageThreshold: number
usageMax: number
ttlMin: number
Expand Down Expand Up @@ -77,7 +76,6 @@ export type EnvironmentVariables = Partial<{
REMOVE_PIN_HEADER: string

// Stamps
BEE_DEBUG_API_URL: string
POSTAGE_STAMP: string
POSTAGE_DEPTH: string
POSTAGE_AMOUNT: string
Expand All @@ -96,7 +94,6 @@ export const SUPPORTED_LEVELS = ['critical', 'error', 'warn', 'info', 'verbose',
export type SupportedLevels = typeof SUPPORTED_LEVELS[number]

export const DEFAULT_BEE_API_URL = 'http://localhost:1633'
export const DEFAULT_BEE_DEBUG_API_URL = 'http://localhost:1635'
export const DEFAULT_HOSTNAME = 'localhost'
export const DEFAULT_PORT = 3000
export const DEFAULT_POSTAGE_USAGE_THRESHOLD = 0.7
Expand All @@ -114,7 +111,6 @@ export const logLevel =

export function getAppConfig({
BEE_API_URL,
BEE_DEBUG_API_URL,
AUTH_SECRET,
ALLOWLIST,
CID_SUBDOMAINS,
Expand All @@ -128,7 +124,6 @@ export function getAppConfig({
return {
hostname: HOSTNAME || DEFAULT_HOSTNAME,
beeApiUrl: BEE_API_URL || DEFAULT_BEE_API_URL,
beeDebugApiUrl: BEE_DEBUG_API_URL || DEFAULT_BEE_DEBUG_API_URL,
authorization: AUTH_SECRET,
allowlist: ALLOWLIST ? ALLOWLIST.split(',') : undefined,
cidSubdomains: CID_SUBDOMAINS === 'true',
Expand All @@ -145,7 +140,7 @@ export function getServerConfig({ PORT, HOSTNAME }: EnvironmentVariables = {}):
}

export function getStampsConfig({
BEE_DEBUG_API_URL,
BEE_API_URL,
POSTAGE_STAMP,
POSTAGE_DEPTH,
POSTAGE_AMOUNT,
Expand All @@ -156,7 +151,7 @@ export function getStampsConfig({
POSTAGE_EXTENDSTTL,
}: EnvironmentVariables = {}): StampsConfig | undefined {
const refreshPeriod = Number(POSTAGE_REFRESH_PERIOD || DEFAULT_POSTAGE_REFRESH_PERIOD)
const beeDebugApiUrl = BEE_DEBUG_API_URL || DEFAULT_BEE_DEBUG_API_URL
const beeApiUrl = BEE_API_URL || DEFAULT_BEE_API_URL

// Start in hardcoded mode
if (POSTAGE_STAMP) return { mode: 'hardcoded', stamp: POSTAGE_STAMP }
Expand All @@ -170,7 +165,7 @@ export function getStampsConfig({
usageMax: Number(POSTAGE_USAGE_MAX || DEFAULT_POSTAGE_USAGE_MAX),
ttlMin: Number(POSTAGE_TTL_MIN || (refreshPeriod / 1000) * 5),
refreshPeriod,
beeDebugApiUrl,
beeApiUrl,
}
} else if (
POSTAGE_EXTENDSTTL === 'true' &&
Expand All @@ -184,7 +179,7 @@ export function getStampsConfig({
ttlMin: Number(POSTAGE_TTL_MIN),
amount: POSTAGE_AMOUNT,
refreshPeriod,
beeDebugApiUrl,
beeApiUrl,
}
}
// Missing one of the variables needed for the autobuy or extends TTL
Expand Down
12 changes: 6 additions & 6 deletions src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BeeDebug } from '@ethersphere/bee-js'
import { Bee } from '@ethersphere/bee-js'
import { createHash } from 'crypto'
import { logger } from './logger'

Expand All @@ -15,17 +15,17 @@ export function getHashedIdentity(): string {
return identity
}

export async function fetchBeeIdentity(beeDebug: BeeDebug, frequencyMs = 15_000) {
export async function fetchBeeIdentity(bee: Bee, frequencyMs = 15_000) {
logger.info(`fetching bee identity with frequency ${frequencyMs}ms`)

if (!(await attemptFetchingBeeIdentity(beeDebug))) {
interval = setInterval(async () => attemptFetchingBeeIdentity(beeDebug), frequencyMs)
if (!(await attemptFetchingBeeIdentity(bee))) {
interval = setInterval(async () => attemptFetchingBeeIdentity(bee), frequencyMs)
}
}

async function attemptFetchingBeeIdentity(beeDebug: BeeDebug) {
async function attemptFetchingBeeIdentity(bee: Bee) {
try {
const { overlay } = await beeDebug.getNodeAddresses()
const { overlay } = await bee.getNodeAddresses()
identity = mapAddress(overlay)
logger.info('bee debug overlay', { overlay, identity })
clearInterval(interval as NodeJS.Timer)
Expand Down
Loading

0 comments on commit ab75940

Please sign in to comment.