Created
July 13, 2015 03:31
-
-
Save cacoco/4abd49ac71e7a2bd3b23 to your computer and use it in GitHub Desktop.
SmallyController
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
class SmallyController @Inject()( | |
@Flag("secure") secure: Boolean, | |
urlShortenerService: RedisUrlShortenerService, | |
response: ResponseBuilder) | |
extends Controller | |
with Logging { | |
post("/url") { request: PostUrlRequest => | |
val url = new URL(request.url) | |
val path = urlShortenerService.create(url) | |
// return the url in the location header | |
val protocol = if (secure) "https" else "http" | |
val base = request.request.host getOrElse "localhost" | |
response.created(PostUrlResponse(s"$protocol://$base/$path")) | |
} | |
get("/:id") { request: SmallyUrlRedirect => | |
urlShortenerService.get(request.id) match { | |
case Some(url) => | |
info(s"Redirecting to resolved URL for id: ${request.id} -> $url") | |
response.movedPermanently.location(url) | |
case _ => response.notFound | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment