Skip to content

Commit

Permalink
compact virtual hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Guarraci committed Dec 29, 2012
1 parent 4d0dc33 commit b2fb262
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
11 changes: 6 additions & 5 deletions core/src/main/scala/io/viper/common/NestServer.scala
Expand Up @@ -136,11 +136,11 @@ class MultiHostServer(port: Int = 80) extends DelayedInit {
body
}

def route(hostname: String, resourcePath: String) {
_server.putRoute(hostname, port, new ViperServer(resourcePath))
def route(hostname: String, resourcePath: String): VirtualServer = {
route(new VirtualServer(hostname, resourcePath))
}

def route(hostname: String) {
def route(hostname: String): VirtualServer = {
route(hostname, "res:///%s".format(hostname))
}

Expand All @@ -158,8 +158,9 @@ class MultiHostServer(port: Int = 80) extends DelayedInit {
route(hostname, "res:///%s".format(hostname), f)
}

def route(server: VirtualServer) {
_server.putRoute(server.hostname, port, server)
def route(virtualServer: VirtualServer): VirtualServer = {
server.putRoute(virtualServer.hostname, port, virtualServer)
virtualServer
}

def main(args: Array[String]) {
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/io/viper/common/ViperServer.scala
Expand Up @@ -3,6 +3,9 @@ package io.viper.common

import io.viper.core.server.file.StaticFileServerHandler
import org.jboss.netty.channel.{ChannelPipeline, ChannelPipelineFactory}
import java.util
import io.viper.core.server.router.RouteResponse
import collection.mutable.ListBuffer


class ViperServer(resourcePath: String) extends ChannelPipelineFactory with RestServer
Expand Down Expand Up @@ -33,4 +36,17 @@ class VirtualServer(val hostname: String, resourcePath: String) extends ViperSer
def this(hostname: String) {
this(hostname, "res:///%s".format(hostname))
}

private val initCode = new ListBuffer[() => Unit]

override def addRoutes {
for (proc <- initCode) proc()
}

def get(route: String, f:(util.Map[String, String]) => RouteResponse): VirtualServer = {
initCode.append(() => {
super.get(route)(f)
})
this
}
}
9 changes: 4 additions & 5 deletions examples/src/main/scala/io/viper/examples/VirtualHosts.scala
Expand Up @@ -14,12 +14,11 @@ object VirtualHosts extends MultiHostServer(8080) {
route(VirtualServer("bar.com", "res:///foo.com"))

// Serve REST handlers
route("rest.com", { server =>
server.get("/hello") { args => Response("world") }
server.get("/echo/$something") { args =>
route("rest.com")
.get("/hello", { args => Response("world") })
.get("/echo/$something", { args =>
val json = new JSONObject()
json.put("response", args.get("something"))
Response(json)
}
})
})
}

0 comments on commit b2fb262

Please sign in to comment.