Skip to content

Commit

Permalink
Support providing an SDC URL via the env (#560)
Browse files Browse the repository at this point in the history
This is useful when live previewing banners with a local version of SDC.

Run the server with e.g.:
$ SDC_URL=http://localhost:8082 sbt run
  • Loading branch information
tjmw authored Mar 1, 2024
1 parent 526b229 commit b856019
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import play.api.mvc._

class Application(authAction: AuthAction[AnyContent],
components: ControllerComponents,
stage: String)
stage: String,
sdcUrlOverride: Option[String])
extends AbstractController(components) {
def healthcheck = Action {
Ok("healthy")
}

def index = authAction {
Ok(views.html.index(stage)).withHeaders(CACHE_CONTROL -> "no-cache")
Ok(views.html.index(stage, sdcUrlOverride))
.withHeaders(CACHE_CONTROL -> "no-cache")
}

// Handler for endpoints with a resource name in the path. The client takes care of using the name
Expand Down
8 changes: 6 additions & 2 deletions app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(stage: String)
@(stage: String, sdcUrlOverride: Option[String])
<!DOCTYPE html>
<html>
<head>
Expand All @@ -13,7 +13,11 @@
<body>
<div id="root"></div>
<script>
window.guardian = { stage: "@stage"};
window.guardian = {};
window.guardian.stage = "@stage";
@if(sdcUrlOverride.isDefined) {
window.guardian.sdcUrlOverride = "@sdcUrlOverride.get";
}
</script>
<script src="@routes.Assets.at("build/app.bundle.js")"></script>
</body>
Expand Down
4 changes: 3 additions & 1 deletion app/wiring/AppComponents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ class AppComponents(context: Context, stage: String) extends BuiltInComponentsFr
val dynamoBannerDesigns = new DynamoBannerDesigns(stage, dynamoClient)
val dynamoArchivedBannerDesigns = new DynamoArchivedBannerDesigns(stage, dynamoClient)

val sdcUrlOverride: Option[String] = sys.env.get("SDC_URL")

override lazy val router: Router = new Routes(
httpErrorHandler,
new Application(authAction, controllerComponents, stage),
new Application(authAction, controllerComponents, stage, sdcUrlOverride),
new Login(authConfig, wsClient, requiredGoogleGroups, googleGroupChecker, controllerComponents),
new SwitchesController(authAction, controllerComponents, stage, runtime),
new AmountsController(authAction, controllerComponents, stage, runtime),
Expand Down
20 changes: 14 additions & 6 deletions public/src/hooks/useModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import { withPreviewStyles } from '../components/channelManagement/previewContai

const moduleVersion = 3;

const getSdcBaseUrl = (): string => {
// If override explicitly provided, use it
if (window.guardian.sdcUrlOverride) {
return `${window.guardian.sdcUrlOverride}/modules/v${moduleVersion}`;
}

// Otherwise, use the stage to determine the base URL
const stage = getStage();
return stage === 'PROD'
? `https://contributions.guardianapis.com/modules/v${moduleVersion}`
: `https://contributions.code.dev-guardianapis.com/modules/v${moduleVersion}`;
};

/**
* Hook for importing a remote module, for live previews.
*/
Expand All @@ -20,12 +33,7 @@ export const useModule = <T>(path: string, name: string): React.FC<T> | undefine
emotionReactJsxRuntime,
};

const stage = getStage();

const baseUrl =
stage === 'PROD'
? `https://contributions.guardianapis.com/modules/v${moduleVersion}`
: `https://contributions.code.dev-guardianapis.com/modules/v${moduleVersion}`;
const baseUrl = getSdcBaseUrl();

window.remoteImport(`${baseUrl}/${path}`).then(bannerModule => {
setModule(() => withPreviewStyles(bannerModule[name]));
Expand Down
1 change: 1 addition & 0 deletions public/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ declare global {
emotionReact: any;
emotionReactJsxRuntime: any;
};
sdcUrlOverride: string | undefined;
};
}
/* eslint-enable @typescript-eslint/no-explicit-any */
Expand Down

0 comments on commit b856019

Please sign in to comment.