/naive-http-server

A really simple http server library implemented in scala with no dependencies

Primary LanguageScala

naive-http-server Build Status Download

A really simple http server library implemented in scala with no dependencies

Requirements

Usage

Add the following lines to your build.sbt

resolvers += "Tim Tennant's repo" at "http://dl.bintray.com/timt/repo/"

libraryDenpendencies += "io.shaka" %% "naive-http-server" % "28"

Starting a server

import io.shaka.http.HttpServer
import io.shaka.http.Response.respond
val httpServer = HttpServer(request => respond("Hello World!")).start()
...
val httpServer = HttpServer(8080).handler(request => respond("Hello World!")).start()

Handling requests

import io.shaka.http.RequestMatching._
httpServer.handler{
    case GET("/hello") => respond("Hello world")
    case GET(echoUrl) => respond(echoUrl)
    case request@POST("/some/restful/thing") => respond(...)
    case GET(_) && Accept(APPLICATION_JSON) => respond("""{"hello":"world"}""").contentType(APPLICATION_JSON)
    case GET(url"/tickets/$ticketId?messageContains=$messageContains") => respond(s"Ticket $ticketId, messageContains $messageContains").contentType(TEXT_PLAIN)
    case _ => respond("doh!").status(NOT_FOUND)
}

Serving static content

import io.shaka.http.StaticResponse.static
httpServer.handler{
    case GET(path) => static("/home/timt/docRoot", path)
    case GET(path) => static(classpathDocRoot("web"), path)
}

Stopping the server

httpServer.stop()

For more examples see

Code license

Apache License 2.0