|
1 | 1 | package org.elasticmq.rest.sqs
|
2 | 2 |
|
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 |
8 | 5 | import java.security.MessageDigest
|
| 6 | +import java.util.concurrent.TimeUnit |
9 | 7 |
|
10 |
| -import org.elasticmq.util.Logging |
11 |
| - |
12 |
| -import collection.mutable.ArrayBuffer |
13 | 8 | 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 |
14 | 12 | 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._ |
22 | 13 | import com.typesafe.config.ConfigFactory
|
| 14 | +import org.elasticmq._ |
23 | 15 | 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} |
29 | 19 |
|
30 | 20 | 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, _} |
34 | 26 |
|
35 | 27 | /**
|
36 | 28 | * By default:
|
@@ -72,6 +64,11 @@ case class TheSQSRestServerBuilder(providedActorSystem: Option[ActorSystem],
|
72 | 64 | */
|
73 | 65 | def withPort(_port: Int) = this.copy(port = _port)
|
74 | 66 |
|
| 67 | + /** |
| 68 | + * Will use port zero for automatic assignment. |
| 69 | + */ |
| 70 | + def withDynamicPort() = withPort(0) |
| 71 | + |
75 | 72 | /**
|
76 | 73 | * @param _serverAddress Address which will be returned as the queue address. Requests to this address
|
77 | 74 | * should be routed to this server.
|
@@ -113,10 +110,10 @@ case class TheSQSRestServerBuilder(providedActorSystem: Option[ActorSystem],
|
113 | 110 | with PurgeQueueDirectives
|
114 | 111 | with AttributesModule {
|
115 | 112 |
|
| 113 | + var serverAddress = theServerAddress |
116 | 114 | lazy val actorSystem = theActorSystem
|
117 | 115 | lazy val materializer = implicitMaterializer
|
118 | 116 | lazy val queueManagerActor = theQueueManagerActor
|
119 |
| - lazy val serverAddress = theServerAddress |
120 | 117 | lazy val sqsLimits = theLimits
|
121 | 118 | lazy val timeout = Timeout(21, TimeUnit.SECONDS) // see application.conf
|
122 | 119 | }
|
@@ -160,8 +157,18 @@ case class TheSQSRestServerBuilder(providedActorSystem: Option[ActorSystem],
|
160 | 157 |
|
161 | 158 | val appStartFuture = Http().bindAndHandle(routes, interface, port)
|
162 | 159 |
|
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 | + } |
165 | 172 |
|
166 | 173 | SQSRestServer(appStartFuture, () => {
|
167 | 174 | appStartFuture.flatMap { sb =>
|
|
0 commit comments