Created
January 8, 2018 20:25
-
-
Save epool/e3b6b4bc0d3f5d20d3acd4a0ceb64ff7 to your computer and use it in GitHub Desktop.
Simplified main method of the Pikmail API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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