-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added proxying support to the server.
- Loading branch information
1 parent
680a1a8
commit 46d5d4b
Showing
3 changed files
with
87 additions
and
55 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.hyperscala | ||
|
||
import io.undertow.server.{HttpHandler, HttpServerExchange} | ||
|
||
trait Handler extends Ordered[Handler] { | ||
/** | ||
* Returns true if this URL should be handled by this Handler. The handleRequest method will be invoked following | ||
* a true response. | ||
* | ||
* @param url the current URL being handled | ||
* @return true if this handler is equipped to handle it | ||
*/ | ||
def isURLMatch(url: URL): Boolean | ||
|
||
/** | ||
* Handles the exchange. | ||
* | ||
* @param url the current URL | ||
* @param exchange the HTTP request | ||
*/ | ||
def handleRequest(url: URL, exchange: HttpServerExchange): Unit | ||
|
||
/** | ||
* The priority of this handler. A higher priority will be considered before lower priority. | ||
*/ | ||
def priority: Priority | ||
|
||
override def compare(that: Handler): Int = priority.compare(that.priority) | ||
} | ||
|
||
object Handler { | ||
def path(paths: Set[String], handler: HttpHandler, priority: Priority = Priority.Normal): Handler = { | ||
val p = priority | ||
new Handler { | ||
def isURLMatch(url: URL): Boolean = paths.contains(url.path) | ||
|
||
override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = handler.handleRequest(exchange) | ||
|
||
override def priority: Priority = p | ||
} | ||
} | ||
|
||
def apply(handler: HttpHandler, priority: Priority = Priority.Normal): Handler = { | ||
val p = priority | ||
new Handler { | ||
def isURLMatch(url: URL): Boolean = true | ||
|
||
override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = handler.handleRequest(exchange) | ||
|
||
override def priority: Priority = p | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hyperscala | ||
import java.net.URI | ||
|
||
import io.undertow.Handlers | ||
import io.undertow.server.HttpServerExchange | ||
import io.undertow.server.handlers.proxy.SimpleProxyClientProvider | ||
|
||
trait ProxyHandler extends Handler { | ||
def uri: URI | ||
|
||
private lazy val proxyClient = new SimpleProxyClientProvider(uri) | ||
private lazy val proxyHandler = Handlers.proxyHandler(proxyClient) | ||
|
||
override def handleRequest(url: URL, exchange: HttpServerExchange): Unit = { | ||
proxyHandler.handleRequest(exchange) | ||
} | ||
} | ||
|
||
object ProxyHandler { | ||
def apply(matcher: URL => Boolean, uri: String, priority: Priority = Priority.Normal): ProxyHandler = { | ||
val u = new URI(uri) | ||
val p = priority | ||
new ProxyHandler { | ||
override def uri: URI = u | ||
|
||
override def isURLMatch(url: URL): Boolean = matcher(url) | ||
|
||
override def priority: Priority = p | ||
} | ||
} | ||
} |
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