Skip to content

Commit

Permalink
serve V1 bundles from the application rather than S3
Browse files Browse the repository at this point in the history
  • Loading branch information
twrichards committed Jun 8, 2023
1 parent 7db3052 commit d0c7e25
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 95 deletions.
5 changes: 2 additions & 3 deletions app/Components.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class AppComponents(context: Context, val config: ApplicationConfiguration)
val updateManager = new UpdateManager(updateActions, configAgent, s3FrontsApi)
val cloudwatch = new CloudWatch(config, awsEndpoints)
val press = new Press(faciaPress)
val assetsManager = new AssetsManager(config, isDev)
override lazy val httpErrorHandler = new LoggingHttpErrorHandler(environment, configuration, sourceMapper, Some(router))

// Controllers
Expand All @@ -99,10 +98,10 @@ class AppComponents(context: Context, val config: ApplicationConfiguration)
val storiesVisible = new StoriesVisibleController(containerService, this)
val thumbnail = new ThumbnailController(containerThumbnails, this)
val troubleshoot = new TroubleshootController(this)
val uncachedAssets = new UncachedAssets(assets, this)
val uncachedAssets = new V1Assets(assets, this)
val v2Assets = new V2Assets(assets)
val vanityRedirects = new VanityRedirects(acl, this)
val views = new ViewsController(acl, assetsManager, isDev, this)
val views = new ViewsController(acl, isDev, this)
val pressController = new PressController(dynamo, this)
val v2App = new V2App(isDev, acl, dynamo, this)
val faciaToolV2 = new FaciaToolV2Controller(acl, structuredLogger, faciaPress, updateActions, configAgent, collectionService, faciaApiIO, this)
Expand Down
9 changes: 0 additions & 9 deletions app/controllers/UncachedAssets.scala

This file was deleted.

12 changes: 12 additions & 0 deletions app/controllers/V1Assets.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package controllers

import scala.concurrent.ExecutionContext.Implicits.global

class V1Assets(val assets: Assets, deps: BaseFaciaControllerComponents) extends BaseFaciaController(deps) {

def aggressivelyCached(file: String) =
assets.at("/public/dist", file, aggressiveCaching = true)
def notCached(file: String) = model.NoCache {
assets.at("/public/src", file)
}
}
41 changes: 33 additions & 8 deletions app/controllers/ViewsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ import model.Cached
import org.joda.time.DateTime
import permissions.ConfigPermissionCheck
import play.api.mvc._
import services.AssetsManager
import util.Acl

class ViewsController(val acl: Acl, assetsManager: AssetsManager, isDev: Boolean,
val deps: BaseFaciaControllerComponents)(implicit ec: ExecutionContext) extends BaseFaciaController(deps) {
import java.io.File

class ViewsController(
val acl: Acl,
isDev: Boolean,
val deps: BaseFaciaControllerComponents
)(implicit ec: ExecutionContext) extends BaseFaciaController(deps) {

private def getV1DistFilename(prefix: String) =
new File("/public/src/").listFiles().find(_.getName.startsWith(prefix)).get.getName
private val v1ConfigBundleHashedName = getV1DistFilename("config.")
private val v1CollectionsBundleHashedName = getV1DistFilename("collections.")

private def shouldRedirectToV2(request: UserRequest[AnyContent], priority: Option[String] = None): Boolean = {
val isBreakingNews = priority.getOrElse("") == "breaking-news" || request.queryString.getOrElse("layout", Seq("")).exists(_.contains("breaking-news"))
Expand All @@ -27,7 +36,12 @@ class ViewsController(val acl: Acl, assetsManager: AssetsManager, isDev: Boolean
} else {
val identity = request.user
Cached(60) {
Ok(views.html.priority(Option(identity), config.facia.stage, isDev, true))
Ok(views.html.priority(
identity = Option(identity),
stage = config.facia.stage,
isDev = isDev,
displayV2Message = true
))
}
}
}
Expand All @@ -38,17 +52,28 @@ class ViewsController(val acl: Acl, assetsManager: AssetsManager, isDev: Boolean
} else {
val identity = request.user
Cached(60) {
Ok(views.html.admin_main(Option(identity), config.facia.stage, overrideIsDev(request, isDev),
assetsManager.pathForCollections, priority != "email", priority))
Ok(views.html.admin_main(
identity = Option(identity),
stage = config.facia.stage,
isDev = overrideIsDev(request, isDev),
bundleFilename = v1ConfigBundleHashedName,
displayV2Message = priority != "email",
priority = priority
))
}
}
}

def configEditor() = (AccessAuthAction andThen new ConfigPermissionCheck(acl)) { request =>
val identity = request.user
Cached(60) {
Ok(views.html.admin_main(Option(identity), config.facia.stage, overrideIsDev(request, isDev),
assetsManager.pathForConfig, false))
Ok(views.html.admin_main(
identity = Option(identity),
stage = config.facia.stage,
isDev = overrideIsDev(request, isDev),
bundleFilename = v1CollectionsBundleHashedName,
displayV2Message = false
))
}
}

Expand Down
40 changes: 0 additions & 40 deletions app/services/AssetsManager.scala

This file was deleted.

11 changes: 2 additions & 9 deletions app/views/admin_main.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(identity: Option[com.gu.pandomainauth.model.User] = None, stage: String, isDev: Boolean, bundlePath: String, displayV2Message: Boolean = false, priority: String = "")
@(identity: Option[com.gu.pandomainauth.model.User] = None, stage: String, isDev: Boolean, bundleFilename: String, displayV2Message: Boolean = false, priority: String = "")

@import switchboard.SwitchManager

Expand All @@ -9,14 +9,7 @@
<title>Fronts Editor</title>
@templates.meta()

<script src="/assets/jspm_packages/system.js"></script>
<script src="/assets/js/jspm-config.js"></script>

@if(!isDev) {
<script src="@{bundlePath}"></script>
}

<script>System.import('setup');</script>
<script src="/assets/dist/@{bundleFilename}"></script>
</head>
<body class="mainFlexContainer is-authed" data-bind="css: {pending: pending}">

Expand Down
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ riffRaffManifestProjectName := riffRaffPackageName.value
riffRaffUploadArtifactBucket := Option("riffraff-artifact")
riffRaffUploadManifestBucket := Option("riffraff-builds")
riffRaffArtifactResources := {
val jsBundlesDir = baseDirectory.value / "tmp" / "bundles"
Seq(
(Debian / packageBin).value -> s"${name.value}/${name.value}_1.0_all.deb",
baseDirectory.value / "riff-raff.yaml" -> "riff-raff.yaml",
baseDirectory.value / "fluentbit/td-agent-bit.conf" -> "facia-tool/fluentbit/td-agent-bit.conf",
baseDirectory.value / "fluentbit/parsers.conf" -> "facia-tool/fluentbit/parsers.conf"
) ++ ((jsBundlesDir * "*") pair rebase(jsBundlesDir, "static-facia-tool"))
)
}

ThisBuild / javacOptions := Seq("-g","-encoding", "utf8")
Expand Down
5 changes: 3 additions & 2 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ GET /logout controllers.PandaAuthContro
GET /login/status controllers.PandaAuthController.status

# static files
GET /assets/fronts-client/*file controllers.V2Assets.at(path="", file)
GET /assets/*file controllers.UncachedAssets.at(path="", file)
GET /assets/fronts-client/*file controllers.V2Assets.at(path="", file)
GET /assets/dist/*file controllers.V1Assets.aggressivelyCached(file)
GET /assets/*file controllers.V1Assets.notCached(file)

##################### NOTE ############################
#all endpoints below this line should be authenticated#
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"description": "Guardian front pages editor",
"scripts": {
"bundleV1config": "esbuild public/src/js/config.js public/src/js/collections.js --bundle --minify --sourcemap --outdir=public/dist ",
"bundleV1": "esbuild public/src/js/config.js public/src/js/collections.js --bundle --minify --sourcemap --outdir=public/dist ",
"watchV1": "esbuild public/src/js/config.js public/src/js/collections.js --bundle --sourcemap --outdir=public/dist --watch",
"test": "grunt test",
"test:browser": "grunt test --no-single-run"
Expand Down
2 changes: 2 additions & 0 deletions public/src/js/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import oauthSession from './utils/oauth-session';
import Router from './modules/router';
import handlers from './modules/route-handlers';

//FIXME reinstate import 'font-awesome/css/font-awesome.min.css!'; here or in the scala templates

var router, bootstrap;

function terminate (error) {
Expand Down
15 changes: 0 additions & 15 deletions riff-raff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,3 @@ deployments:
cloudFormationStackName: facia
cloudFormationStackByTags: false
prependStackToCloudFormationStackName: false
static-facia-tool:
type: aws-s3
parameters:
bucket: cms-fronts-static-assets
cacheControl:
- pattern: .*\\.js$
value: public, max-age=315360000
- pattern: .*
value: public, max-age=60
mimeTypes:
js: application/javascript
map: text/plain
json: application/json
prefixStack: false
publicReadAcl: true
8 changes: 2 additions & 6 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ globalJsDependencies() {

javascriptV1() {
npm install
npm run jspm config registries.github.auth ${JSPM_GITHUB_AUTH_SECRET}
npm run jspm registry export github
npm run jspm install
grunt --stack validate
# grunt --stack test
grunt --stack bundle
# grunt --stack test #FIXME reinstate tests
npm run bundleV1
}

javascriptV2() {
Expand Down

0 comments on commit d0c7e25

Please sign in to comment.