Skip to content

Commit

Permalink
Step 1: Setting up a basic web server
Browse files Browse the repository at this point in the history
  • Loading branch information
baens committed Feb 17, 2018
1 parent 830399a commit cb13731
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repositories {

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

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

// Properties for `./gradlew run`
run {
standardInput = System.in
environment = ["SHUTDOWN_TYPE" : "INPUT"]
}
29 changes: 27 additions & 2 deletions src/Server.kt
Original file line number Diff line number Diff line change
@@ -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.