Skip to content

Instantly share code, notes, and snippets.

@dalexandrov
Last active March 16, 2021 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalexandrov/d9cd272ff9a81332ab38a620301ab257 to your computer and use it in GitHub Desktop.
Save dalexandrov/d9cd272ff9a81332ab38a620301ab257 to your computer and use it in GitHub Desktop.
package io.helidon.kotlin.examples.quickstart.se
import healthSupport
import io.helidon.common.LogConfig
import io.helidon.config.Config
import io.helidon.health.checks.HealthChecks
import io.helidon.media.jsonp.JsonpSupport
import io.helidon.metrics.MetricsSupport
import io.helidon.webserver.Routing
import io.helidon.webserver.WebServer
import routing
import webserver
/**
* Application main entry point.
* @param args command line arguments.
*/
fun main(args: Array<String>) {
startServer()
}
/**
* Start the server.
* @return the created [WebServer] instance
*/
fun startServer(): WebServer {
// load logging configuration
LogConfig.configureRuntime()
// By default this will pick up application.yaml from the classpath
val config = Config.create()
val server = webServer {
routing(createRouting(config)))
config(config["server"])
addMediaSupport(jsonpSupport{})
}
// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
server.start()
.thenAccept { ws: WebServer ->
println(
"WEB server is up! http://localhost:" + ws.port() + "/greet"
)
ws.whenShutdown().thenRun { println("WEB server is DOWN. Good bye!") }
}
.exceptionally { t: Throwable ->
System.err.println("Startup failed: " + t.message)
t.printStackTrace(System.err)
null
}
// Server threads are not daemon. No need to block. Just react.
return server
}
/**
* Creates new [Routing].
*
* @return routing configured with JSON support, a health check, and a service
* @param config configuration of this server
*/
private fun createRouting(config: Config): Routing {
val metrics = MetricsSupport.create()
val greetService = GreetService(config)
val health = healthSupport {
addLiveness(*HealthChecks.healthChecks())
}
return routing {
register(health) // Health at "/health"
register(metrics) // Metrics at "/metrics"
register("/greet", greetService)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment