Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

Commit

Permalink
feat: Add support for private and public urls (#189)
Browse files Browse the repository at this point in the history
* Add support for private and public urls

* Use healthcheck urls as optional

* remove bc from vars

* remove healthcheck vars from env

* undo
  • Loading branch information
agusaldasoro authored Nov 2, 2022
1 parent 75e44c3 commit fbb3606
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ HTTP_SERVER_HOST=0.0.0.0
LAMBDAS_URL=https://peer.decentraland.org/lambdas
LIGHTHOUSE_URL=https://peer.decentraland.org/comms
CONTENT_URL=https://peer.decentraland.org/content

REALM_NAMES=

# archipelago,fixed-adapter,lighthouse
Expand Down
2 changes: 1 addition & 1 deletion .env.org
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
COMMS_MODE=lighthouse
CONTENT_URL=https://peer.decentraland.org/content
LAMBDAS_URL=https://peer.decentraland.org/lambdas
LIGHTHOUSE_URL=https://peer.decentraland.org/comms
LIGHTHOUSE_URL=https://peer.decentraland.org/comms
2 changes: 1 addition & 1 deletion .env.zone
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
COMMS_MODE=lighthouse
CONTENT_URL=https://peer.decentraland.zone/content
LAMBDAS_URL=https://peer.decentraland.zone/lambdas
LIGHTHOUSE_URL=https://peer.decentraland.zone/comms
LIGHTHOUSE_URL=https://peer.decentraland.zone/comms
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"**/CVS": true,
"**/.DS_Store": true,
"**/*.ts": { "when": "$(basename).proto" }
}
},
"cSpell.words": [
"healthcheck"
]
}
42 changes: 31 additions & 11 deletions src/adapters/status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBaseComponent } from '@well-known-components/interfaces'
import { IBaseComponent, IConfigComponent } from '@well-known-components/interfaces'
import { BaseComponents } from '../types'

export type HealthStatus = {
Expand Down Expand Up @@ -34,9 +34,7 @@ export async function createStatusComponent(
const { fetch, logs, config } = components

const logger = logs.getLogger('status-component')
const lambdasUrl = new URL(await config.requireString('LAMBDAS_URL'))
const contentUrl = new URL(await config.requireString('CONTENT_URL'))
const lighthouseUrl = new URL(await config.requireString('LIGHTHOUSE_URL'))
const { lambdasUrl, contentUrl, lighthouseUrl } = await loadServicesURLs(config)

const fetchJson = async (baseURL: URL, path: string) => {
let url = baseURL.toString()
Expand All @@ -60,7 +58,7 @@ export async function createStatusComponent(
comms: false
}
try {
const data = await fetchJson(lambdasUrl, 'health')
const data = await fetchJson(lambdasUrl.healthcheck, 'health')

health.content = data['content'] === 'Healthy'
health.lambdas = data['lambda'] === 'Healthy'
Expand All @@ -74,7 +72,7 @@ export async function createStatusComponent(

const lastLambdasStatus: ServiceStatus = {
time: 0,
publicUrl: lambdasUrl.toString()
publicUrl: lambdasUrl.public.toString()
}

async function getLambdasStatus() {
Expand All @@ -84,7 +82,7 @@ export async function createStatusComponent(

lastLambdasStatus.time = Date.now()
try {
const data = await fetchJson(lambdasUrl, 'status')
const data = await fetchJson(lambdasUrl.healthcheck, 'status')
lastLambdasStatus.version = data.catalystVersion
lastLambdasStatus.commitHash = data.commitHash
} catch (err: any) {
Expand All @@ -96,7 +94,7 @@ export async function createStatusComponent(

const lastContentStatus: ServiceStatus = {
time: 0,
publicUrl: contentUrl.toString()
publicUrl: contentUrl.public.toString()
}
async function getContentStatus() {
if (Date.now() - lastContentStatus.time < STATUS_EXPIRATION_TIME_MS) {
Expand All @@ -105,7 +103,7 @@ export async function createStatusComponent(

lastContentStatus.time = Date.now()
try {
const data = await fetchJson(contentUrl, 'status')
const data = await fetchJson(contentUrl.healthcheck, 'status')
lastContentStatus.version = data.catalystVersion
lastContentStatus.commitHash = data.commitHash
} catch (err: any) {
Expand All @@ -118,7 +116,7 @@ export async function createStatusComponent(
const lastLighthouseStatus: LighthouseStatus = {
time: 0,
usersCount: 0,
publicUrl: lighthouseUrl.toString()
publicUrl: lighthouseUrl.public.toString()
}

async function getLighthouseStatus() {
Expand All @@ -128,7 +126,7 @@ export async function createStatusComponent(

lastLighthouseStatus.time = Date.now()
try {
const data = await fetchJson(lighthouseUrl, 'status')
const data = await fetchJson(lighthouseUrl.healthcheck, 'status')

lastLighthouseStatus.realmName = data.name
lastLighthouseStatus.version = data.version
Expand All @@ -148,3 +146,25 @@ export async function createStatusComponent(
getLighthouseStatus
}
}

async function loadServicesURLs(config: IConfigComponent) {
const publicLambdasUrl = new URL(await config.requireString('LAMBDAS_URL'))
const healthCheckLambdasUrl = await config.getString('HEALTHCHECK_LAMBDAS_URL')
const lambdasUrl = {
public: publicLambdasUrl,
healthcheck: healthCheckLambdasUrl ? new URL(healthCheckLambdasUrl) : publicLambdasUrl
}
const publicContentUrl = new URL(await config.requireString('CONTENT_URL'))
const healthCheckContentUrl = await config.getString('HEALTHCHECK_CONTENT_URL')
const contentUrl = {
public: publicContentUrl,
healthcheck: healthCheckContentUrl ? new URL(healthCheckContentUrl) : publicContentUrl
}
const publicLighthouseUrl = new URL(await config.requireString('LIGHTHOUSE_URL'))
const healthCheckLighthouseUrl = await config.getString('HEALTHCHECK_LIGHTHOUSE_URL')
const lighthouseUrl = {
public: publicLighthouseUrl,
healthcheck: healthCheckLighthouseUrl ? new URL(healthCheckLighthouseUrl) : publicLighthouseUrl
}
return { lambdasUrl, contentUrl, lighthouseUrl }
}
3 changes: 2 additions & 1 deletion test/unit/about-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ test('lighthouse adapter about response', ({ beforeStart, components, spyCompone
beforeStart(() => {
Object.assign(process.env, {
COMMS_MODE: 'lighthouse',
LIGHTHOUSE_URL: 'http://0.0.0.0:3000'
LIGHTHOUSE_URL: 'http://0.0.0.0:3000',
HEALTHCHECK_LIGHTHOUSE_URL: 'http://0.0.0.0:3000'
})
})

Expand Down

0 comments on commit fbb3606

Please sign in to comment.