Created
November 20, 2014 09:31
-
-
Save sshastry/874fa6ce2431e3725e9f to your computer and use it in GitHub Desktop.
http4s example
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
package org.app | |
import scalaz._ | |
import Scalaz._ | |
import scala.concurrent.{ExecutionContext, Future} | |
import org.http4s._ // v0.4.0 | |
import org.http4s.dsl._ | |
import org.http4s.server._ | |
import org.http4s.server.blaze.BlazeBuilder | |
import org.slf4j.{Logger,LoggerFactory} | |
import scala.util.Properties.envOrNone | |
trait Logging { protected lazy val log = LoggerFactory.getLogger(this.getClass) } | |
object X extends ParamMatcher("x") | |
object Y extends ParamMatcher("y") | |
class App(hostname: String = "localhost", port: Int = 9999)(implicit ec: ExecutionContext = ExecutionContext.global) extends Logging { | |
def run = BlazeBuilder.bindHttp(port, hostname).mountService(service, "/").run.awaitShutdown() | |
def logreq(r: Request): Unit = log.info(s"${r.remoteAddr.getOrElse("null")} -> ${r.method}: ${r.uri.path} ${r.uri.query}") | |
lazy val service = HttpService { | |
case req @ GET -> Root / "blah" :? X(x0) +& Y(y0) => | |
logreq(req) | |
log.info(s"x0=${x0}, y0=${y0}") | |
Ok("x = " + x0 + "\n" + "y = " + y0) | |
case req => | |
logreq(req) | |
NotFound("404: not found") | |
} | |
} | |
object App extends Logging { | |
val hostname = envOrNone("HOSTNAME").getOrElse("localhost") | |
val port = envOrNone("HTTP_PORT").map(_.toInt).getOrElse(8080) | |
def main(args: Array[String]): Unit = { | |
log.info(s"starting http4s-blaze example on '$hostname:$port'") | |
new App(hostname,port).run | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment