forked from wm3/poormans-service-manager
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
15 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
package jp.w3ch.psm | ||
|
||
|
||
import java.net.InetSocketAddress | ||
|
||
import org.jboss.netty.handler.codec.http._ | ||
import org.jboss.netty.handler.codec.http.HttpResponseStatus._ | ||
|
||
import com.twitter.finagle.Service | ||
|
||
|
||
object DispatchingServer { | ||
type ProxyHandler = PartialFunction[HostUrl, Service[HttpRequest, HttpResponse]] | ||
} | ||
|
||
class DispatchingServer(proxy: DispatchingServer.ProxyHandler) extends Service[HttpRequest, HttpResponse] { | ||
class DispatchingServer(dispatch: DispatchingServer.ProxyHandler) extends Service[HttpRequest, HttpResponse] { | ||
|
||
val defaultServer = new service.TextResponse("hello") | ||
val getDefaultServer: DispatchingServer.ProxyHandler = { case _ => defaultServer } | ||
val defaultService = new service.TextResponse("hello") | ||
|
||
override def apply(request: HttpRequest) = { | ||
val host = Option(request.getHeader("Host")) | ||
|
||
val server = (proxy orElse getDefaultServer)(HostUrl(host)) | ||
val service = doDispatch(HostUrl(host)) | ||
|
||
server(request) | ||
service(request) | ||
} | ||
|
||
|
||
private[this] val doDispatch: DispatchingServer.ProxyHandler = dispatch orElse { | ||
case _ => defaultService | ||
} | ||
|
||
} | ||
|
||
object DispatchingServer { | ||
type ProxyHandler = PartialFunction[HostUrl, Service[HttpRequest, HttpResponse]] | ||
} | ||
|
||
// vim: set shiftwidth=2 expandtab : |