-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy2.scala
68 lines (57 loc) · 2.07 KB
/
proxy2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package steps
import java.util.concurrent.atomic.AtomicInteger
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.Uri.Authority
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.{Host, RawHeader}
import akka.stream.ActorMaterializer
import io.circe._
import models._
import scala.concurrent.Future
// Round robin load balancing
class ReverseProxyV2 {
implicit val system = ActorSystem()
implicit val executor = system.dispatcher
implicit val materializer = ActorMaterializer()
implicit val http = Http(system)
val counter = new AtomicInteger(0)
def NotFound(path: String) = HttpResponse(
404,
entity = HttpEntity(ContentTypes.`application/json`, Json.obj("error" -> Json.fromString(s"$path not found")).noSpaces)
)
val services: Map[String, Seq[Target]] = Map(
"test.foo.bar" -> Seq(
Target("http://127.0.0.1:8081"),
Target("http://127.0.0.1:8082"),
Target("http://127.0.0.1:8083")
)
)
def extractHost(request: HttpRequest): String = request.header[Host].map(_.host.address()).getOrElse("--")
def handler(request: HttpRequest): Future[HttpResponse] = {
val host = extractHost(request)
services.get(host) match {
case Some(seq) => {
val index = counter.incrementAndGet() % (if (seq.nonEmpty) seq.size else 1)
val target = seq.apply(index)
val headersIn: Seq[HttpHeader] =
request.headers.filterNot(t => t.name() == "Host") :+
Host(target.host) :+
RawHeader("X-Fowarded-Host", host) :+
RawHeader("X-Fowarded-Scheme", request.uri.scheme)
val proxyRequest = request.copy(
uri = request.uri.copy(
scheme = target.scheme,
authority = Authority(host = Uri.NamedHost(target.host), port = target.port)
),
headers = headersIn.toList
)
http.singleRequest(proxyRequest)
}
case None => Future.successful(NotFound(host))
}
}
def start(host: String, port: Int) {
http.bindAndHandleAsync(handler, host, port)
}
}