Skip to content

Instantly share code, notes, and snippets.

@epool
Created January 8, 2018 20:25
Show Gist options
  • Save epool/e3b6b4bc0d3f5d20d3acd4a0ceb64ff7 to your computer and use it in GitHub Desktop.
Save epool/e3b6b4bc0d3f5d20d3acd4a0ceb64ff7 to your computer and use it in GitHub Desktop.
Simplified main method of the Pikmail API
@JvmStatic
fun main(args: Array<String>) {
val port = System.getenv("PORT")?.toInt() ?: 8080
embeddedServer(Netty, port) {
routing {
static {
staticRootFolder = File("./static")
files("./")
default("index.html")
}
get("/{email}") {
// Since the email is part of the endpoint path, it won't be null ever
val email = call.parameters["email"]!!
val size = call.request.queryParameters["size"]
try {
val profilePictureUrl = Pikmail.getProfilePictureUrl(email, size?.toInt()).await()
call.respondRedirect(profilePictureUrl)
} catch (throwable: Throwable) {
if (throwable is ProfileNotFountException) {
with(HttpStatusCode.NotFound) {
call.respondText(
"""{"email": "$email","status":$value,"error":"$description"}""",
ContentType.Application.Json,
this
)
}
}
}
}
}
}.start(wait = true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment