Skip to content

Instantly share code, notes, and snippets.

@cacoco
Created July 13, 2015 03:31
Show Gist options
  • Save cacoco/4abd49ac71e7a2bd3b23 to your computer and use it in GitHub Desktop.
Save cacoco/4abd49ac71e7a2bd3b23 to your computer and use it in GitHub Desktop.
SmallyController
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