Skip to content

Commit

Permalink
Merge pull request #668 from mkurz/fix-play-scala-websocket-example_a…
Browse files Browse the repository at this point in the history
…lternative

Fix `play-scala-websocket-example` (missing csp nonce)
  • Loading branch information
mkurz authored Jun 20, 2024
2 parents 088b7ac + 3d4711d commit 357010f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ handleFlip = (container) ->
detailsHolder.append($("<h2>").text("Error: " + JSON.parse(jqXHR.responseText).error))
# display loading info
detailsHolder = container.find(".details-holder")
detailsHolder.append($("<h4>").text("Determing whether you should buy or sell based on the sentiment of recent tweets..."))
detailsHolder.append($("<h4>").text("Determining whether you should buy or sell based on the sentiment of recent tweets..."))
detailsHolder.append($("<div>").addClass("progress progress-striped active").append($("<div>").addClass("bar").css("width", "100%")))
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ body {
.transform(180deg);
text-align: center;
z-index: 1;
transform-style: preserve-3d;
& h4 {
padding: 20px;
}
Expand All @@ -121,4 +122,4 @@ body {
margin-bottom: 10px;
margin-left: 10px;
margin-right: 10px;
}
}
8 changes: 4 additions & 4 deletions play-scala-websocket-example/app/views/index.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<head>
<title>Reactive Stock News Dashboard</title>
<link rel='stylesheet' href='@routes.Assets.at("lib/bootstrap/css/bootstrap.min.css")'>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.min.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script type='text/javascript' src='@routes.Assets.at("lib/jquery/jquery.min.js")'></script>
<script type='text/javascript' src='@routes.Assets.at("lib/flot/jquery.flot.js")'></script>
<script type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("lib/jquery/jquery.min.js")'></script>
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("lib/flot/jquery.flot.js")'></script>
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>
</head>
<body data-ws-url="@routes.HomeController.ws.webSocketURL()">
<div class="navbar navbar-inverse navbar-fixed-top">
Expand Down
6 changes: 4 additions & 2 deletions play-scala-websocket-example/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import play.core.PlayVersion.pekkoVersion

lazy val root = (project in file("."))
.enablePlugins(PlayScala)
//.enablePlugins(PlayNettyServer).disablePlugins(PlayPekkoHttpServer) // uncomment to use the Netty backend
Expand All @@ -16,6 +14,10 @@ lazy val root = (project in file("."))
"org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test,
"org.awaitility" % "awaitility" % "4.2.1" % Test,
),
TwirlKeys.templateImports ++= Seq(
"views.html.helper.CSPNonce"
),
LessKeys.compress := true,
(Test / javaOptions) += "-Dtestserver.port=19001",
scalacOptions ++= Seq(
"-feature",
Expand Down
22 changes: 12 additions & 10 deletions play-scala-websocket-example/conf/application.conf
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# This is the main configuration file for the application.
# ~~~~~

# Uncomment this for the most verbose Pekko debugging:
pekko {
loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
loglevel = "INFO"
logging-filter = "org.apache.pekko.event.slf4j.Slf4jLoggingFilter"

actor {
debug {
#receive = on
#autoreceive = on
#lifecycle = on
}
}
#actor {
# debug {
# receive = on
# autoreceive = on
# lifecycle = on
# }
#}
}

play.filters.enabled += play.filters.csp.CSPFilter

play.filters.csp.directives {
connect-src = "'self' ws://localhost:9000"
connect-src = "'self'"
default-src = "'self'"
}

Expand Down
5 changes: 3 additions & 2 deletions play-scala-websocket-example/conf/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
<logger name="play" level="INFO"/>

<!-- actors logging -->
<logger name="org.apache.pekko" level="DEBUG"/>
<logger name="org.apache.pekko" level="INFO"/>
<logger name="org.apache.pekko.stream.Log" level="INFO"/>
<logger name="actors" level="DEBUG"/>

<!-- controllers -->
<logger name="controllers" level="DEBUG"/>
<logger name="controllers" level="INFO"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
Expand Down
1 change: 0 additions & 1 deletion play-scala-websocket-example/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Use the Play sbt plugin for Play projects
addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.4")

addSbtPlugin("com.github.sbt" % "sbt-less" % "2.0.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import play.shaded.ahc.org.asynchttpclient.ws.WebSocketListener;

import play.shaded.ahc.org.asynchttpclient.ws.WebSocketUpgradeHandler;
import org.slf4j.Logger;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
Expand All @@ -29,7 +30,7 @@ public CompletableFuture<NettyWebSocket> call(String url, String origin, WebSock
final BoundRequestBuilder requestBuilder = client.prepareGet(url).addHeader("Origin", origin);

final WebSocketUpgradeHandler handler = new WebSocketUpgradeHandler.Builder().addWebSocketListener(listener).build();
ListenableFuture<NettyWebSocket> future = requestBuilder.execute(handler);
final ListenableFuture<NettyWebSocket> future = requestBuilder.execute(handler);
return future.toCompletableFuture();
}

Expand All @@ -40,6 +41,8 @@ public LoggingListener(Consumer<String> onMessageCallback) {
this.onMessageCallback = onMessageCallback;
}

private Logger logger = org.slf4j.LoggerFactory.getLogger(LoggingListener.class);

private Throwable throwableFound = null;

public Throwable getThrowable() {
Expand All @@ -62,6 +65,7 @@ public void onError(Throwable t) {

@Override
public void onTextFrame(String payload, boolean finalFragment, int rsv) {
//logger.info("onMessage: s = " + s);
onMessageCallback.accept(payload);
}
}
Expand Down

0 comments on commit 357010f

Please sign in to comment.