Skip to content

Commit

Permalink
explicit asset.minified and stage.banner configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 1, 2020
1 parent cedb336 commit a16cc84
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 75 deletions.
5 changes: 1 addition & 4 deletions app/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ final class Env(

def net = common.netConfig

val isProd = mode == Mode.Prod && net.isProd
val isDev = mode == Mode.Dev
val isStage = mode == Mode.Prod && !net.isProd
val explorerEndpoint = config.get[String]("explorer.endpoint")
val tablebaseEndpoint = config.get[String]("explorer.tablebase.endpoint")

Expand Down Expand Up @@ -280,7 +277,7 @@ final class EnvBoot(
templating.Environment setEnv env

// free memory for reload workflow
if (env.isDev)
if (mode == Mode.Dev)
Lilakka.shutdown(shutdown, _.PhaseServiceStop, "Freeing dev memory") { () =>
Future {
templating.Environment.destroy()
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/LilaController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ abstract private[controllers] class LilaController(val env: Env)
type RestoredUser = (Option[FingerPrintedUser], Option[UserModel])
private def restoreUser(req: RequestHeader): Fu[RestoredUser] =
env.security.api restoreUser req dmap {
case Some(d) if !env.isProd =>
case Some(d) if !env.net.isProd =>
d.copy(user =
d.user
.addRole(lila.security.Permission.Beta.dbKey)
Expand Down
5 changes: 2 additions & 3 deletions app/http/HttpFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ final class HttpFilter(env: Env)(implicit val mat: Materializer) extends Filter
val reqTime = nowMillis - startTime
val statusCode = result.header.status
val client = HTTPRequest clientName req
if (env.isDev) {
if (logRequests) logger.info(s"$statusCode $client $req ${req.method} $actionName ${reqTime}ms")
} else httpMon.time(actionName, client, req.method, statusCode).record(reqTime)
if (env.net.isProd) httpMon.time(actionName, client, req.method, statusCode).record(reqTime)
else if (logRequests) logger.info(s"$statusCode $client $req ${req.method} $actionName ${reqTime}ms")
}

private def redirectWrongDomain(req: RequestHeader): Option[Result] =
Expand Down
17 changes: 6 additions & 11 deletions app/templating/AssetHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ import lila.common.{ AssetVersion, ContentSecurityPolicy, Nonce }

trait AssetHelper { self: I18nHelper with SecurityHelper =>

def isProd: Boolean
def isStage: Boolean

def minifiedAssets = isProd || isStage

def netDomain: lila.common.config.NetDomain
lazy val assetDomain = env.net.assetDomain
lazy val socketDomains = env.net.socketDomains
lazy val vapidPublicKey = env.push.vapidPublicKey
private lazy val netDomain = env.net.domain
private lazy val assetDomain = env.net.assetDomain
private lazy val assetBaseUrl = env.net.assetBaseUrl
private lazy val socketDomains = env.net.socketDomains
private lazy val minifiedAssets = env.net.minifiedAssets
lazy val vapidPublicKey = env.push.vapidPublicKey

lazy val sameAssetDomain = netDomain.value == assetDomain.value

lazy val assetBaseUrl = env.net.assetBaseUrl

def assetVersion = AssetVersion.current

def assetUrl(path: String): String = s"$assetBaseUrl/assets/_$assetVersion/$path"
Expand Down
15 changes: 5 additions & 10 deletions app/templating/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ object Environment

type FormWithCaptcha = (play.api.data.Form[_], lila.common.Captcha)

def netBaseUrl = env.net.baseUrl.value
def isGloballyCrawlable = env.net.crawlable

lazy val netDomain = env.net.domain
def isProd = env.isProd
def isStage = env.isStage
def netConfig = env.net
def netBaseUrl = env.net.baseUrl.value
def contactEmail = env.net.email.value

def apiVersion = lila.api.Mobile.Api.currentVersion

lazy val explorerEndpoint = env.explorerEndpoint
lazy val tablebaseEndpoint = env.tablebaseEndpoint

def contactEmail = env.net.email.value
def explorerEndpoint = env.explorerEndpoint
def tablebaseEndpoint = env.tablebaseEndpoint

def contactEmailLink = a(href := s"mailto:$contactEmail")(contactEmail)

Expand Down
6 changes: 3 additions & 3 deletions app/views/analyse/embed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ object embed {
),
body(
cls := s"highlight ${config.bg} ${config.board}",
dataDev := (!isProd).option("true"),
dataAssetUrl := assetBaseUrl,
dataDev := netConfig.minifiedAssets.option("true"),
dataAssetUrl := netConfig.assetBaseUrl,
dataAssetVersion := assetVersion.value,
dataTheme := config.bg
)(
Expand All @@ -42,7 +42,7 @@ object embed {
div(cls := "left")(
a(target := "_blank", href := url)(h1(titleGame(pov.game))),
" ",
em("brought to you by ", a(target := "_blank", href := netBaseUrl)(netDomain))
em("brought to you by ", a(target := "_blank", href := netBaseUrl)(netConfig.domain))
),
a(target := "_blank", cls := "open", href := url)("Open")
)
Expand Down
16 changes: 8 additions & 8 deletions app/views/base/layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ object layout {
def apply(
title: String,
fullTitle: Option[String] = None,
robots: Boolean = isGloballyCrawlable,
robots: Boolean = netConfig.crawlable,
moreCss: Frag = emptyFrag,
moreJs: Frag = emptyFrag,
playing: Boolean = false,
Expand All @@ -172,7 +172,7 @@ object layout {
metaThemeColor,
st.headTitle {
if (ctx.blind) "lichess"
else if (isProd) fullTitle | s"$title • lichess.org"
else if (netConfig.isProd) fullTitle | s"$title • lichess.org"
else s"[dev] ${fullTitle | s"$title • lichess.dev"}"
},
cssTag("site"),
Expand Down Expand Up @@ -216,12 +216,12 @@ object layout {
"mobile" -> ctx.isMobileBrowser,
"playing fixed-scroll" -> playing
),
dataDev := (!isProd).option("true"),
dataDev := (!netConfig.minifiedAssets).option("true"),
dataVapid := vapidPublicKey,
dataUser := ctx.userId,
dataSoundSet := ctx.currentSoundSet.toString,
dataSocketDomains := socketDomains.mkString(","),
dataAssetUrl := assetBaseUrl,
dataSocketDomains := netConfig.socketDomains.mkString(","),
dataAssetUrl := netConfig.assetBaseUrl,
dataAssetVersion := assetVersion.value,
dataNonce := ctx.nonce.ifTrue(sameAssetDomain).map(_.value),
dataTheme := ctx.currentBg,
Expand All @@ -231,7 +231,7 @@ object layout {
blindModeForm,
ctx.pageData.inquiry map { views.html.mod.inquiry(_) },
ctx.me ifTrue ctx.userContext.impersonatedBy.isDefined map { views.html.mod.impersonate(_) },
isStage option views.html.base.bits.stage,
netConfig.stageBanner option views.html.base.bits.stage,
lila.security.EmailConfirm.cookie.get(ctx.req).map(views.html.auth.bits.checkYourEmailBanner(_)),
playing option zenToggle,
siteHeader(playing),
Expand All @@ -255,7 +255,7 @@ object layout {
a(id := "reconnecting", cls := "link text", dataIcon := "B")(trans.reconnecting()),
chessground option jsTag("vendor/chessground.min.js"),
ctx.requiresFingerprint option fingerprintTag,
if (minifiedAssets)
if (netConfig.minifiedAssets)
jsModule("site", defer = deferJs)
else
frag(
Expand Down Expand Up @@ -307,7 +307,7 @@ object layout {
else ctx.isBot option botImage,
a(href := "/")(
"lichess",
span(if (isProd) ".org" else ".dev")
span(if (netConfig.isProd) ".org" else ".dev")
)
),
ctx.blind option h2("Navigation"),
Expand Down
2 changes: 1 addition & 1 deletion app/views/lobby/home.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object home {
views.html.base.layout(
title = "",
fullTitle = Some {
s"lichess.${if (isProd && !isStage) "org" else "dev"}${trans.freeOnlineChess.txt()}"
s"lichess.${if (netConfig.isProd) "org" else "dev"}${trans.freeOnlineChess.txt()}"
},
moreJs = frag(
jsModule("lobby", defer = true),
Expand Down
2 changes: 1 addition & 1 deletion app/views/streamer/header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object header {
)(youTube.minUrl)
},
a(cls := "service lichess", href := routes.User.show(s.user.username))(
netDomain,
netConfig.domain,
routes.User.show(s.user.username).url
)
),
Expand Down
6 changes: 3 additions & 3 deletions app/views/streamer/show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ method:'post'
iframe(
st.frameborder := "0",
frame.scrolling := "no",
src := s"https://www.youtube.com/live_chat?v=$videoId&embed_domain=$netDomain"
src := s"https://www.youtube.com/live_chat?v=$videoId&embed_domain=${netConfig.domain}"
)
case _ =>
s.streamer.twitch.map { twitch =>
iframe(
st.frameborder := "0",
frame.scrolling := "yes",
src := s"https://twitch.tv/embed/${twitch.userId}/chat?${(ctx.currentBg != "light") ?? "darkpopout&"}parent=${netDomain}"
src := s"https://twitch.tv/embed/${twitch.userId}/chat?${(ctx.currentBg != "light") ?? "darkpopout&"}parent=${netConfig.domain}"
)
}
}
Expand All @@ -88,7 +88,7 @@ method:'post'
s.streamer.twitch.map { twitch =>
div(cls := "box embed twitch")(
iframe(
src := s"https://player.twitch.tv/?channel=${twitch.userId}&parent=$netDomain",
src := s"https://player.twitch.tv/?channel=${twitch.userId}&parent=${netConfig.domain}",
frame.allowfullscreen
)
)
Expand Down
4 changes: 2 additions & 2 deletions app/views/study/embed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object embed {
),
body(
cls := s"highlight ${config.bg} ${config.board}",
dataDev := (!isProd).option("true"),
dataAssetUrl := assetBaseUrl,
dataDev := netConfig.minifiedAssets.option("true"),
dataAssetUrl := netConfig.assetBaseUrl,
dataAssetVersion := assetVersion.value,
dataTheme := config.bg
)(
Expand Down
2 changes: 1 addition & 1 deletion app/views/team/admin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object admin {
a(
dataIcon := "z",
cls := "text copy-url-button",
data.copyurl := s"${netDomain}${routes.Tournament.show(t.id).url}"
data.copyurl := s"${netConfig.domain}${routes.Tournament.show(t.id).url}"
)
)
}
Expand Down
2 changes: 2 additions & 0 deletions conf/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ net {
socket.domains = [ "localhost:9664" ]
asset.domain = ${net.domain}
asset.base_url = "http://"${net.asset.domain}
asset.minified = false
base_url = "http://"${net.domain}
ip = "127.0.0.1"
email = ""
crawlable = false
ratelimit = true
prodDomain = "lichess.org"
http.log = true
stage.banner = false
}
play {
application.loader = "lila.app.AppLoader"
Expand Down
2 changes: 2 additions & 0 deletions modules/common/src/main/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ object config {
@ConfigName("base_url") baseUrl: BaseUrl,
@ConfigName("asset.domain") assetDomain: AssetDomain,
@ConfigName("asset.base_url") assetBaseUrl: String,
@ConfigName("asset.minified") minifiedAssets: Boolean,
@ConfigName("stage.banner") stageBanner: Boolean,
@ConfigName("socket.domains") socketDomains: List[String],
crawlable: Boolean,
@ConfigName("ratelimit") rateLimit: RateLimit,
Expand Down
10 changes: 5 additions & 5 deletions ui/site/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
booted = true;
var $el = $('#challenge-app').html(lichess.initiatingHtml);
lichess.loadCssPath('challenge');
lichess.loadScript(lichess.compiledScript('challenge')).done(function() {
lichess.loadScript(lichess.jsModule('challenge')).done(function() {
instance = LichessChallenge($el[0], {
data: data,
show: function() {
Expand Down Expand Up @@ -339,7 +339,7 @@
booted = true;
var $el = $('#notify-app').html(initiatingHtml);
lichess.loadCssPath('notify');
lichess.loadScript(lichess.compiledScript('notify')).done(function() {
lichess.loadScript(lichess.jsModule('notify')).done(function() {
instance = LichessNotify($el.empty()[0], {
data: data,
incoming: incoming,
Expand Down Expand Up @@ -390,7 +390,7 @@
const $el = $('#dasher_app').html(initiatingHtml),
playing = $('body').hasClass('playing');
lichess.loadCssPath('dasher');
lichess.loadScript(lichess.compiledScript('dasher')).done(() =>
lichess.loadScript(lichess.jsModule('dasher')).done(() =>
LichessDasher($el.empty()[0], { playing })
);
});
Expand All @@ -405,7 +405,7 @@
const boot = () => {
if (booted) return;
booted = true;
lichess.loadScript(lichess.compiledScript('cli')).done(() =>
lichess.loadScript(lichess.jsModule('cli')).done(() =>
LichessCli.app($wrap, toggle)
);
};
Expand Down Expand Up @@ -964,7 +964,7 @@
////////////////////

if ('serviceWorker' in navigator && 'Notification' in window && 'PushManager' in window) {
const workerUrl = new URL(lichess.assetUrl(lichess.compiledScript('serviceWorker'), {sameDomain: true}), self.location.href);
const workerUrl = new URL(lichess.assetUrl(lichess.jsModule('serviceWorker'), {sameDomain: true}), self.location.href);
workerUrl.searchParams.set('asset-url', document.body.getAttribute('data-asset-url'));
if (document.body.getAttribute('data-dev')) workerUrl.searchParams.set('dev', '1');
const updateViaCache = document.body.getAttribute('data-dev') ? 'none' : 'all';
Expand Down
Loading

0 comments on commit a16cc84

Please sign in to comment.