--- a/src/Pure/General/http_server.scala Wed Mar 01 17:09:54 2017 +0100
+++ b/src/Pure/General/http_server.scala Thu Mar 02 12:09:50 2017 +0100
@@ -13,19 +13,32 @@
object HTTP_Server
{
- def apply(handler: HttpExchange => Unit): HTTP_Server =
+ class Handler private[HTTP_Server](val path: String, val handler: HttpHandler)
+ {
+ override def toString: String = path
+ }
+
+ def handler(path: String)(body: HttpExchange => Unit): Handler =
+ new Handler(path, new HttpHandler { def handle(x: HttpExchange) { body(x) } })
+
+ def apply(handlers: Handler*): HTTP_Server =
{
val localhost = InetAddress.getByName("127.0.0.1")
val server = HttpServer.create(new InetSocketAddress(localhost, 0), 0)
- server.createContext("/", new HttpHandler { def handle(x: HttpExchange) { handler(x) } })
server.setExecutor(null)
- new HTTP_Server(server)
+
+ val http_server = new HTTP_Server(server)
+ for (handler <- handlers) http_server += handler
+ http_server
}
}
class HTTP_Server private(val server: HttpServer)
{
+ def += (handler: HTTP_Server.Handler) { server.createContext(handler.path, handler.handler) }
+ def -= (handler: HTTP_Server.Handler) { server.removeContext(handler.path) }
+
def start: Unit = server.start
def stop: Unit = server.stop(0)