Skip to content
Permalink
Browse files

Step 1: Setting up a basic web server

  • Loading branch information...
baens committed Feb 16, 2018
1 parent 830399a commit cb1373152bf52d7cdf5aa08489bafc327cf7912e
Showing with 37 additions and 3 deletions.
  1. +2 −0 Dockerfile
  2. +8 −1 build.gradle
  3. +27 −2 src/Server.kt
@@ -11,4 +11,6 @@ FROM openjdk:${VERSION}-jre
COPY --from=BUILD /src/build/libs/step-by-step-kotlin-hello-world-jaxrs-all.jar /bin/runner/run.jar
WORKDIR /bin/runner

EXPOSE 8080

CMD ["java","-jar","run.jar"]
@@ -21,6 +21,7 @@ repositories {

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8'
compile "org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.26"
}

/**
@@ -41,4 +42,10 @@ jar {
manifest {
attributes 'Main-Class': 'ServerKt'
}
}
}

// Properties for `./gradlew run`
run {
standardInput = System.in
environment = ["SHUTDOWN_TYPE" : "INPUT"]
}
@@ -1,3 +1,28 @@
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory
import javax.ws.rs.core.UriBuilder

fun main(args: Array<String>) {
println("Hello World")
}
val url = UriBuilder.fromUri("http://0.0.0.0/")
.port(8080)
.build()

val httpServer = GrizzlyHttpServerFactory.createHttpServer(
url,
true
)

if (System.getenv()["SHUTDOWN_TYPE"].equals("INPUT")) {
println("Press any key to shutdown")
readLine()
println("Shutting down from input")
httpServer.shutdownNow()
} else {
Runtime.getRuntime().addShutdownHook(Thread {
println("Shutting down from shutdown hook")
httpServer.shutdownNow()
})

println("Press Ctrl+C to shutdown")
Thread.currentThread().join()
}
}

0 comments on commit cb13731

Please sign in to comment.
You can’t perform that action at this time.