Skip to content

Commit

Permalink
add transparent proxy support
Browse files Browse the repository at this point in the history
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
arnelandwehr committed Mar 1, 2016
0 parents commit 4be549f
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
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
8 changes: 8 additions & 0 deletions LICENSE
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.
26 changes: 26 additions & 0 deletions README
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
54 changes: 54 additions & 0 deletions app/controllers/ProxyController.scala
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)
}
}
}
22 changes: 22 additions & 0 deletions build.sbt
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
47 changes: 47 additions & 0 deletions conf/application.conf
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}
22 changes: 22 additions & 0 deletions conf/logback.xml
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>
19 changes: 19 additions & 0 deletions conf/routes
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)
4 changes: 4 additions & 0 deletions project/build.properties
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
16 changes: 16 additions & 0 deletions project/plugins.sbt
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")
Binary file added public/images/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/javascripts/hello.js
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 added public/stylesheets/main.css
Empty file.
Loading

0 comments on commit 4be549f

Please sign in to comment.