Skip to content

Commit a2d4686

Browse files
authored
Merge pull request #89 from pjungermann/87-88_dynamic-port-support
dynamic port support (issue #88) + consistent port (issue #87)
2 parents b935687 + a9c2481 commit a2d4686

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/SQSRestServerBuilder.scala

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
11
package org.elasticmq.rest.sqs
22

3-
import akka.http.scaladsl.Http
4-
import akka.stream.ActorMaterializer
5-
6-
import scala.util.control.Exception._
7-
import xml._
3+
import java.io.ByteArrayOutputStream
4+
import java.nio.ByteBuffer
85
import java.security.MessageDigest
6+
import java.util.concurrent.TimeUnit
97

10-
import org.elasticmq.util.Logging
11-
12-
import collection.mutable.ArrayBuffer
138
import akka.actor.{ActorRef, ActorSystem, Props}
9+
import akka.http.scaladsl.Http
10+
import akka.http.scaladsl.server.{Directive1, Directives}
11+
import akka.stream.ActorMaterializer
1412
import akka.util.Timeout
15-
16-
import scala.concurrent.{Await, Future}
17-
import org.elasticmq.rest.sqs.directives.ElasticMQDirectives
18-
import org.elasticmq.rest.sqs.Constants._
19-
20-
import scala.xml.EntityRef
21-
import org.elasticmq._
2213
import com.typesafe.config.ConfigFactory
14+
import org.elasticmq._
2315
import org.elasticmq.actor.QueueManagerActor
24-
import org.elasticmq.util.NowProvider
25-
26-
import scala.concurrent.duration._
27-
import java.nio.ByteBuffer
28-
import java.io.ByteArrayOutputStream
16+
import org.elasticmq.rest.sqs.Constants._
17+
import org.elasticmq.rest.sqs.directives.ElasticMQDirectives
18+
import org.elasticmq.util.{Logging, NowProvider}
2919

3020
import scala.collection.immutable.TreeMap
31-
import java.util.concurrent.TimeUnit
32-
33-
import akka.http.scaladsl.server.{Directive1, Directives}
21+
import scala.collection.mutable.ArrayBuffer
22+
import scala.concurrent.duration._
23+
import scala.concurrent.{Await, Future}
24+
import scala.util.control.Exception._
25+
import scala.xml.{EntityRef, _}
3426

3527
/**
3628
* By default:
@@ -72,6 +64,11 @@ case class TheSQSRestServerBuilder(providedActorSystem: Option[ActorSystem],
7264
*/
7365
def withPort(_port: Int) = this.copy(port = _port)
7466

67+
/**
68+
* Will use port zero for automatic assignment.
69+
*/
70+
def withDynamicPort() = withPort(0)
71+
7572
/**
7673
* @param _serverAddress Address which will be returned as the queue address. Requests to this address
7774
* should be routed to this server.
@@ -113,10 +110,10 @@ case class TheSQSRestServerBuilder(providedActorSystem: Option[ActorSystem],
113110
with PurgeQueueDirectives
114111
with AttributesModule {
115112

113+
var serverAddress = theServerAddress
116114
lazy val actorSystem = theActorSystem
117115
lazy val materializer = implicitMaterializer
118116
lazy val queueManagerActor = theQueueManagerActor
119-
lazy val serverAddress = theServerAddress
120117
lazy val sqsLimits = theLimits
121118
lazy val timeout = Timeout(21, TimeUnit.SECONDS) // see application.conf
122119
}
@@ -160,8 +157,18 @@ case class TheSQSRestServerBuilder(providedActorSystem: Option[ActorSystem],
160157

161158
val appStartFuture = Http().bindAndHandle(routes, interface, port)
162159

163-
TheSQSRestServerBuilder.this.logger.info("Started SQS rest server, bind address %s:%d, visible server address %s"
164-
.format(interface, port, if (theServerAddress.isWildcard) "* (depends on incoming request path) "else theServerAddress.fullAddress))
160+
appStartFuture.onSuccess {
161+
case sb: Http.ServerBinding =>
162+
env.serverAddress = NodeAddress(
163+
env.serverAddress.protocol,
164+
env.serverAddress.host,
165+
sb.localAddress.getPort,
166+
env.serverAddress.contextPath
167+
)
168+
169+
TheSQSRestServerBuilder.this.logger.info("Started SQS rest server, bind address %s:%d, visible server address %s"
170+
.format(interface, sb.localAddress.getPort, if (env.serverAddress.isWildcard) "* (depends on incoming request path) " else env.serverAddress.fullAddress))
171+
}
165172

166173
SQSRestServer(appStartFuture, () => {
167174
appStartFuture.flatMap { sb =>

0 commit comments

Comments
 (0)