Skip to content

Instantly share code, notes, and snippets.

@pinge
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save pinge/d03f051b6d493ea98ef6 to your computer and use it in GitHub Desktop.

Select an option

Save pinge/d03f051b6d493ea98ef6 to your computer and use it in GitHub Desktop.
custom directives for extracting info from the request and logging
trait ApiWithLoggingDirective extends HttpService {
def clientIPOrLoopbackAddress: Directive1[RemoteAddress] = Directives.clientIP | provide(RemoteAddress("127.0.0.1"))
def clientIPAndUri: Directive[RemoteAddress :: Uri :: HNil] = clientIPOrLoopbackAddress & requestUri
def detachAndLogRequest: Directive0 = (detach(()) & clientIPAndUri).hflatMap {
case (ip :: uri :: HNil) => RequestLog.requestLoggingDirective(ip, uri)
}
val playerTypes = (get & path("api" / "playerTypes")) {
detachAndLogRequest() {
complete {
PlayerTypes.findAll
}
}
}
val article = (get & path("api" / "news" / LongNumber)) { articleId =>
detachAndLogRequest(Map("articleId" -> articleId.toString)) {
complete {
Article(articleId)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment