Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added feature flag #121

Merged
merged 2 commits into from
Dec 18, 2024
Merged
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
2 changes: 2 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ AWS_SECRET_ACCESS_KEY=
AWS_S3_BUCKET=
AWS_S3_REGION=
AWS_S3_ENDPOINT=
FF_URL=https://feature-flags.decentraland.org
ATLAS_SERVER_URL=
31 changes: 25 additions & 6 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-s3": "^3.0.0",
"@dcl/schemas": "^9.8.0",
"@well-known-components/env-config-provider": "^1.2.0",
"@well-known-components/features-component": "^2.0.0",
"@well-known-components/http-requests-logger-component": "^2.1.0",
"@well-known-components/http-server": "^1.1.6",
"@well-known-components/http-tracer-component": "^1.1.0",
Expand All @@ -33,8 +35,7 @@
"ethers": "^5.7.2",
"fp-future": "^1.0.1",
"node-fetch": "^2.6.1",
"p-limit": "^3.1.0",
"@aws-sdk/client-s3": "^3.0.0"
"p-limit": "^3.1.0"
},
"devDependencies": {
"@types/cli-progress": "^3.9.1",
Expand All @@ -45,4 +46,4 @@
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
}
}
14 changes: 13 additions & 1 deletion src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createStatusCheckComponent,
IFetchComponent,
} from '@well-known-components/http-server'
import { createFeaturesComponent } from '@well-known-components/features-component'
import { createSubgraphComponent } from '@well-known-components/thegraph-component'
import { createLogComponent } from '@well-known-components/logger'
import { createTracerComponent } from '@well-known-components/tracer-component'
Expand Down Expand Up @@ -113,7 +114,17 @@ export async function initComponents(): Promise<AppComponents> {
const statusChecks = await createStatusCheckComponent({ server, config })
const renderMiniMap = await createMiniMapRendererComponent({ map })
const renderEstateMiniMap = await createEstatesRendererComponent({ map })

const features = await createFeaturesComponent(
{
config,
logs,
fetch,
},
(await config.requireString('ATLAS_SERVER_URL')) ||
`http://${await config.getString(
'HTTP_SERVER_HOST'
)}:${await config.getString('HTTP_SERVER_PORT')}`
)
return {
config,
api,
Expand All @@ -131,5 +142,6 @@ export async function initComponents(): Promise<AppComponents> {
rentals,
renderMiniMap,
renderEstateMiniMap,
features,
}
}
75 changes: 45 additions & 30 deletions src/controllers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,86 @@ import { cacheWrapper } from '../logic/cache-wrapper'
import { extractParams, getFilterFromUrl } from '../logic/filter-params'
import { isErrorWithMessage } from '../logic/error'
import { AppComponents, Context } from '../types'
import { ApplicationName, Feature } from '../modules/features/types'

export const createTilesRequestHandler = (
components: Pick<AppComponents, 'map'>
components: Pick<AppComponents, 'map' | 'features'>
) => {
const { map } = components
const { map, features } = components
return cacheWrapper(
async (context: { url: URL }) => {
if (!map.isReady()) {
return { status: 503, body: 'Not ready' }
}

const lastUploadedUrls = map.getLastUploadedTilesUrl()
if (!lastUploadedUrls.v2) {
const tiles = await map.getTiles()
const data = getFilterFromUrl(context.url, tiles)
return {
status: 200,
headers: {
'content-type': 'application/json',
} as Record<string, string>,
body: JSON.stringify({ ok: true, data }),
if (
await features.getIsFeatureEnabled(
ApplicationName.DAPPS,
Feature.ATLAS_REDIRECT_TO_S3
)
) {
const lastUploadedUrls = map.getLastUploadedTilesUrl()
if (lastUploadedUrls.v2) {
return {
status: 301,
headers: {
location: lastUploadedUrls.v2,
'cache-control': 'public, max-age=60',
} as Record<string, string>,
}
}
}

const tiles = await map.getTiles()
const data = getFilterFromUrl(context.url, tiles)
return {
status: 301,
status: 200,
headers: {
location: lastUploadedUrls.v2,
'cache-control': 'public, max-age=60',
'content-type': 'application/json',
} as Record<string, string>,
body: JSON.stringify({ ok: true, data }),
}
},
[map.getLastUpdatedAt]
)
}

export const createLegacyTilesRequestHandler = (
components: Pick<AppComponents, 'map'>
components: Pick<AppComponents, 'map' | 'features'>
) => {
const { map } = components
const { map, features } = components
return cacheWrapper(
async (context: { url: URL }) => {
if (!map.isReady()) {
return { status: 503, body: 'Not ready' }
}

const lastUploadedUrls = map.getLastUploadedTilesUrl()
if (!lastUploadedUrls.v1) {
const tiles = await map.getTiles()
const data = toLegacyTiles(getFilterFromUrl(context.url, tiles))
return {
status: 200,
headers: {
'content-type': 'application/json',
} as Record<string, string>,
body: JSON.stringify({ ok: true, data }),
if (
await features.getIsFeatureEnabled(
ApplicationName.DAPPS,
Feature.ATLAS_REDIRECT_TO_S3
)
) {
const lastUploadedUrls = map.getLastUploadedTilesUrl()
if (lastUploadedUrls.v1) {
return {
status: 301,
headers: {
location: lastUploadedUrls.v1,
'cache-control': 'public, max-age=60',
} as Record<string, string>,
}
}
}

const tiles = await map.getTiles()
const data = toLegacyTiles(getFilterFromUrl(context.url, tiles))
return {
status: 301,
status: 200,
headers: {
location: lastUploadedUrls.v1,
'cache-control': 'public, max-age=60',
'content-type': 'application/json',
} as Record<string, string>,
body: JSON.stringify({ ok: true, data }),
}
},
[map.getLastUpdatedAt]
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Router } from '@well-known-components/http-server'

export type RoutesComponents = Pick<
AppComponents,
'server' | 'map' | 'image' | 'config' | 'api' | 'district'
'server' | 'map' | 'image' | 'config' | 'api' | 'district' | 'features'
>

// We return the entire router because it will be easier to test than a whole server
Expand Down
15 changes: 15 additions & 0 deletions src/modules/features/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export enum Feature {
ATLAS_REDIRECT_TO_S3 = 'atlas-redirect-to-s3',
}

export enum ApplicationName {
EXPLORER = 'explorer',
BUILDER = 'builder',
MARKETPLACE = 'marketplace',
ACCOUNT = 'account',
DAO = 'dao',
DAPPS = 'dapps',
EVENTS = 'events',
LANDING = 'landing',
TEST = 'test',
}
13 changes: 7 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { IApiComponent } from './modules/api/types'
import { IMapComponent } from './modules/map/types'
import { IDistrictComponent } from './modules/district/types'
import { IImageComponent } from './modules/image/types'
import { IS3Component } from './modules/s3/component'
import {
IConfigComponent,
ILoggerComponent,
Expand All @@ -13,9 +8,14 @@ import {
} from '@well-known-components/interfaces'
import { IRentalsComponent } from './modules/rentals/types'
import { ISubgraphComponent } from '@well-known-components/thegraph-component'
import { IFeaturesComponent } from '@well-known-components/features-component'
import { IApiComponent } from './modules/api/types'
import { IMapComponent } from './modules/map/types'
import { IDistrictComponent } from './modules/district/types'
import { IImageComponent } from './modules/image/types'
import { IS3Component } from './modules/s3/component'
import { Metrics } from './metrics'
import { MiniMapRendererComponent } from './adapters/mini-map-renderer'

export type GlobalContext = {
components: AppComponents
}
Expand All @@ -37,6 +37,7 @@ export type AppComponents = {
statusChecks: IBaseComponent
renderMiniMap: MiniMapRendererComponent
renderEstateMiniMap: MiniMapRendererComponent
features: IFeaturesComponent
}

export type TestComponents = AppComponents
Expand Down
Loading