0

I am building a simple proxy to point to another server. Everything works but I need to find a way to be able to set the hosts in a ClientBuilder externally most likely using Docker or maybe some sort of configuration file. Here is what I have:

import java.net.InetSocketAddress
import com.twitter.finagle.Service
import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder}
import com.twitter.finagle.http.{Request, Http}
import com.twitter.util.Future
import org.jboss.netty.handler.codec.http._

object Proxy extends App {

  val client: Service[HttpRequest, HttpResponse] = {
  ClientBuilder()
    .codec(Http())
    .hosts("localhost:8888")
    .hostConnectionLimit(1)
    .build()
  }

  val server = {
    ServerBuilder()
      .codec(Http())
      .bindTo(new InetSocketAddress(8080))
      .name("TROGDOR")
      .build(client)
  }
}

If you know of a way to do this or have any ideas about it please let me know!

Ryan Wilson
  • 1,743
  • 3
  • 15
  • 26

2 Answers2

0

You need a file server.properties and put your configuration inside the file:

HOST=host:8888

Now get docker to write your configuration with every startup with a docker-entrypoint bash script. Add this script and define environment variables inside your Dockerfile:

$ ENV HOST=myhost
$ ENV PORT=myport
$ ADD docker-entrypoint.sh /docker-entrypoint.sh
$ ENTRYPOINT ["/docker-entrypoint.sh"]
$ CMD ["proxy"]

Write out your docker-entrypoint.sh:

#!/bin/bash -x

set -o errexit

cat > server.properties << EOF
HOST=${HOST}:${PORT}
EOF

if [ "$1" = 'proxy' ]; then
  launch server
fi

exec "$@"

Launch Docker with your configuration and the command "proxy":

$ docker run -e "HOST=host" -e "PORT=port" image proxy

You can also do linking when your not sure of your server container ip adress:

$ docker run -e "HOST=mylinkhost" -e "PORT=port" --link myservercontainer:mylinkhost image proxy
blacklabelops
  • 4,708
  • 5
  • 25
  • 42
0

if you want running this simple proxy in a docker container and manage the target host ip dynamically, you can try to pass a target host ip through environment variable and change your code like this

import java.net.InetSocketAddress
import com.twitter.finagle.Service
import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder}
import com.twitter.finagle.http.{Request, Http}
import com.twitter.util.Future
import org.jboss.netty.handler.codec.http._

object Proxy extends App {
  val target_host = sys.env.get("TARGET_HOST")

  val client: Service[HttpRequest, HttpResponse] = {
  ClientBuilder()
    .codec(Http())
    .hosts(target_host.getOrElse("127.0.0.1:8888"))
    .hostConnectionLimit(1)
    .build()
  }

  val server = {
    ServerBuilder()
      .codec(Http())
      .bindTo(new InetSocketAddress(8080))
      .name("TROGDOR")
      .build(client)
  }
}

this will let your code read system environment variable TARGET_HOST. when you done this part, you can try to start your docker container by adding the following parameter to your docker run command:

-e "TARGET_HOST=127.0.0.1:8090"

for example docker run -e "TARGET_HOST=127.0.0.1:8090" <docker image> <docker command>

note that you can change 127.0.0.1:8090 to your target host.

Freeznet
  • 537
  • 2
  • 6