-
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.
application could act as a transparent proxy, every request is just forwarded to the specified server. The server base uri must be therefor configured.
- Loading branch information
0 parents
commit 4be549f
Showing
14 changed files
with
436 additions
and
0 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,22 @@ | ||
logs | ||
target | ||
/.idea | ||
/.idea_modules | ||
/.classpath | ||
/.project | ||
/.settings | ||
/RUNNING_PID | ||
bin/ | ||
/db | ||
.eclipse | ||
/lib/ | ||
/logs/ | ||
/modules | ||
/project/target | ||
/target | ||
tmp/ | ||
test-result | ||
server.pid | ||
*.eml | ||
/dist/ | ||
.cache |
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,8 @@ | ||
This software is licensed under the Apache 2 license, quoted below. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with | ||
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
language governing permissions and limitations under the License. |
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,26 @@ | ||
# Docster | ||
|
||
|
||
Transforming proxy that creates an html represenation of your json hypermedia (e.g. HAL) api on the fly. | ||
|
||
## Start | ||
|
||
```shell | ||
SERVER_URI="my-server-base-uri" sbt run | ||
``` | ||
|
||
or | ||
```shell | ||
sbt run -Dserver.uri="my-server-base-uri" | ||
``` | ||
|
||
## Execute tests | ||
|
||
```shell | ||
sbt test | ||
``` | ||
|
||
|
||
## Current Features | ||
|
||
- transparent proxy |
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,54 @@ | ||
package controllers | ||
|
||
import javax.inject.Inject | ||
|
||
import play.api.Play.current | ||
import play.api._ | ||
import play.api.libs.ws.{ InMemoryBody, WSClient, WSRequest, WSResponseHeaders } | ||
import play.api.mvc._ | ||
|
||
import scala.concurrent.Future | ||
|
||
class ProxyController @Inject() (ws: WSClient) (configuration: Configuration) extends Controller { | ||
|
||
implicit val context = play.api.libs.concurrent.Execution.Implicits.defaultContext | ||
|
||
def proxy(requestPath: String) = Action.async(parse.tolerantText) { originalRequest => | ||
calculateServerUri(requestPath) match { | ||
case Some(serverUri) => | ||
forwardRequestToServer(originalRequest, serverUri ) | ||
case None => Future(InternalServerError) | ||
} | ||
} | ||
|
||
private def calculateServerUri(path: String): Option[String] = { | ||
configuration.getString("server.uri") match { | ||
case Some("unset") => None | ||
case Some(basePath) => Some(basePath + "/" + path) | ||
case None => None | ||
} | ||
} | ||
|
||
private def forwardRequestToServer(originalRequest: Request[String], serverUri: String): Future[Result] = { | ||
val serverWsRequest = toServerRequest(originalRequest, serverUri) | ||
serverWsRequest.stream().map { | ||
case (response, body) => | ||
Result(ResponseHeader(response.status, toSimpleHeaderMap(response)), body) | ||
case noResponse => InternalServerError | ||
} | ||
} | ||
|
||
private def toServerRequest(request: Request[String], uri: String): WSRequest = { | ||
val result = ws.url(uri) | ||
.withMethod(request.method) | ||
.withHeaders(request.headers.toSimpleMap.toList: _*) | ||
.withBody(InMemoryBody(request.body.getBytes)) | ||
result | ||
} | ||
|
||
private def toSimpleHeaderMap(response: WSResponseHeaders): Map[String, String] = { | ||
response.headers.map { | ||
case (k, v) => (k, v.head) | ||
} | ||
} | ||
} |
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,22 @@ | ||
name := """docster""" | ||
|
||
version := "1.0-SNAPSHOT" | ||
|
||
lazy val root = (project in file(".")).enablePlugins(PlayScala) | ||
|
||
scalaVersion := "2.11.6" | ||
|
||
libraryDependencies ++= Seq( | ||
jdbc, | ||
cache, | ||
ws, | ||
specs2 % Test, | ||
"com.github.tomakehurst" % "wiremock" % "1.58", | ||
"org.scalatest" %% "scalatest" % "2.2.6" % "test" | ||
) | ||
|
||
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases" | ||
|
||
// Play provides two styles of routers, one expects its actions to be injected, the | ||
// other, legacy style, accesses its actions statically. | ||
routesGenerator := InjectedRoutesGenerator |
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,47 @@ | ||
# This is the main configuration file for the application. | ||
# ~~~~~ | ||
|
||
# Secret key | ||
# ~~~~~ | ||
# The secret key is used to secure cryptographics functions. | ||
# | ||
# This must be changed for production, but we recommend not changing it in this file. | ||
# | ||
# See http://www.playframework.com/documentation/latest/ApplicationSecret for more details. | ||
play.crypto.secret = "changeme" | ||
|
||
# The application languages | ||
# ~~~~~ | ||
play.i18n.langs = [ "en" ] | ||
|
||
# Router | ||
# ~~~~~ | ||
# Define the Router object to use for this application. | ||
# This router will be looked up first when the application is starting up, | ||
# so make sure this is the entry point. | ||
# Furthermore, it's assumed your route file is named properly. | ||
# So for an application router like `my.application.Router`, | ||
# you may need to define a router file `conf/my.application.routes`. | ||
# Default to Routes in the root package (and conf/routes) | ||
# play.http.router = my.application.Routes | ||
|
||
# Database configuration | ||
# ~~~~~ | ||
# You can declare as many datasources as you want. | ||
# By convention, the default datasource is named `default` | ||
# | ||
# db.default.driver=org.h2.Driver | ||
# db.default.url="jdbc:h2:mem:play" | ||
# db.default.username=sa | ||
# db.default.password="" | ||
|
||
# Evolutions | ||
# ~~~~~ | ||
# You can disable evolutions if needed | ||
# play.evolutions.enabled=false | ||
|
||
# You can disable evolutions for a specific datasource if necessary | ||
# play.evolutions.db.default.enabled=false | ||
|
||
server.uri="unset" | ||
server.uri=${?SERVER_URI} |
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,22 @@ | ||
<configuration> | ||
|
||
<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" /> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%coloredLevel - %logger - %message%n%xException</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<!-- | ||
The logger name is typically the Java/Scala package name. | ||
This configures the log level to log at for a package and its children packages. | ||
--> | ||
<logger name="play" level="INFO" /> | ||
<logger name="application" level="DEBUG" /> | ||
|
||
<root level="ERROR"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
|
||
</configuration> |
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,19 @@ | ||
# Routes | ||
# This file defines all ProxyController routes (Higher priority routes first) | ||
# ~~~~ | ||
|
||
GET / controllers.ProxyController.proxy(path = "") | ||
GET /$path<.*> controllers.ProxyController.proxy(path: String) | ||
POST / controllers.ProxyController.proxy(path = "") | ||
POST /$path<.*> controllers.ProxyController.proxy(path: String) | ||
PUT / controllers.ProxyController.proxy(path = "") | ||
PUT /$path<.*> controllers.ProxyController.proxy(path: String) | ||
DELETE / controllers.ProxyController.proxy(path = "") | ||
DELETE /$path<.*> controllers.ProxyController.proxy(path: String) | ||
HEAD / controllers.ProxyController.proxy(path = "") | ||
HEAD /$path<.*> controllers.ProxyController.proxy(path: String) | ||
OPTIONS / controllers.ProxyController.proxy(path = "") | ||
OPTIONS /$path<.*> controllers.ProxyController.proxy(path: String) | ||
|
||
# Map static resources from the /public folder to the /assets URL path | ||
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) |
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,4 @@ | ||
#Activator-generated Properties | ||
#Fri Feb 19 17:13:32 CET 2016 | ||
template.uuid=37a0ffc3-739a-47a4-95a1-2100413d0180 | ||
sbt.version=0.13.8 |
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,16 @@ | ||
// The Play plugin | ||
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.6") | ||
|
||
// web plugins | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,3 @@ | ||
if (window.console) { | ||
console.log("Welcome to your Play application's JavaScript!"); | ||
} |
Empty file.
Oops, something went wrong.