Align Scala sample with the rest of our samples (#3020)

* Correct differences between scala and the rest

* Update dependencies and scala version

* Some cleanups
This commit is contained in:
Markus Thömmes 2020-11-11 16:02:59 +01:00 committed by GitHub
parent 3d61d6dff8
commit f8c3655862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 19 deletions

View File

@ -1,5 +1,5 @@
# Use an SBT image matching the Scala and JDK version. # Use an SBT image matching the Scala and JDK version.
FROM hseeberger/scala-sbt:8u265_1.4.2_2.12.12 as builder FROM hseeberger/scala-sbt:8u265_1.4.2_2.13.3 as builder
# Copy local code to the container image. # Copy local code to the container image.
WORKDIR /app WORKDIR /app
@ -16,7 +16,7 @@ RUN sbt assembly
FROM openjdk:8-jre-alpine FROM openjdk:8-jre-alpine
# Copy the jar to the production image from the builder stage. # Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/scala-2.12/helloworld-*.jar /helloworld.jar COPY --from=builder /app/target/scala-2.13/helloworld-*.jar /helloworld.jar
# Run the web service on container startup. # Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/helloworld.jar"] CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/helloworld.jar"]

View File

@ -27,7 +27,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-scala
## Configuring the Service descriptor ## Configuring the Service descriptor
Importantly, in [helloworld-scala.yaml](./helloworld-scala.yaml) **change the Importantly, in [service.yaml](./service.yaml) **change the
image reference to match up with your designated repository**, i.e. replace image reference to match up with your designated repository**, i.e. replace
`{username}` with your Dockerhub username in the example below. `{username}` with your Dockerhub username in the example below.

View File

@ -4,14 +4,14 @@ name := "helloworld-scala"
version := "0.0.1" version := "0.0.1"
scalaVersion := "2.12.8" scalaVersion := "2.13.3"
mainClass in Compile := Some("klang.HelloWorldScala") mainClass in Compile := Some("example.HelloWorldScala")
scalacOptions ++= Seq("-encoding", "UTF-8") scalacOptions ++= Seq("-encoding", "UTF-8")
lazy val akkaVersion = "2.5.16" lazy val akkaVersion = "2.6.10"
lazy val akkaHttpVersion = "10.1.5" lazy val akkaHttpVersion = "10.2.1"
libraryDependencies ++= Seq( libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion, "com.typesafe.akka" %% "akka-actor" % akkaVersion,

View File

@ -9,7 +9,5 @@ spec:
containers: containers:
- image: docker.io/{username}/helloworld-scala - image: docker.io/{username}/helloworld-scala
env: env:
- name: MESSAGE - name: TARGET
value: "Scala & Akka on Knative says hello!" value: "Scala Sample v1"
- name: HOST
value: "localhost"

View File

@ -3,6 +3,6 @@ helloworld {
host = ${?HOST} host = ${?HOST}
port = 8080 port = 8080
port = ${?PORT} port = ${?PORT}
message = "Hello World!" target = "World"
message = ${?MESSAGE} target = ${?TARGET}
} }

View File

@ -1,4 +1,4 @@
package klang package example
import akka.actor.ActorSystem import akka.actor.ActorSystem
import akka.event.LoggingAdapter import akka.event.LoggingAdapter
@ -15,8 +15,6 @@ object HelloWorldScala {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
// Creates and initializes an Akka Actor System // Creates and initializes an Akka Actor System
implicit val system: ActorSystem = ActorSystem("HelloWorldScala") implicit val system: ActorSystem = ActorSystem("HelloWorldScala")
// Creates and initializes a Materializer to be used for the Akka HTTP Server
implicit val mat: Materializer = ActorMaterializer()
// Specifies where any Futures in this code will execute // Specifies where any Futures in this code will execute
implicit val ec: ExecutionContext = system.dispatcher implicit val ec: ExecutionContext = system.dispatcher
// Obtains a logger to be used for the sample // Obtains a logger to be used for the sample
@ -25,7 +23,7 @@ object HelloWorldScala {
val config = system.settings.config val config = system.settings.config
// These are read from the application.conf file under `resources` // These are read from the application.conf file under `resources`
val message = config.getString("helloworld.message") val target = config.getString("helloworld.target")
val host = config.getString("helloworld.host") val host = config.getString("helloworld.host")
val port = config.getInt("helloworld.port") val port = config.getInt("helloworld.port")
@ -34,13 +32,13 @@ object HelloWorldScala {
path("") { path("") {
get { get {
log.info("Received request to HelloWorldScala") log.info("Received request to HelloWorldScala")
complete(HttpEntity(`text/html(UTF-8)`, message)) complete(HttpEntity(`text/html(UTF-8)`, s"Hello $target!"))
} }
} }
// Here we create the Http server, and bind it to the host and the port, // Here we create the Http server, and bind it to the host and the port,
// so we can serve requests using the route(s) we defined previously. // so we can serve requests using the route(s) we defined previously.
val binding = Http().bindAndHandle(serviceRoute, host, port) andThen { val binding = Http().newServerAt(host, port).bind(serviceRoute) andThen {
case Success(sb) => case Success(sb) =>
log.info("Bound: {}", sb) log.info("Bound: {}", sb)
case Failure(t) => case Failure(t) =>