Skip to content

Commit

Permalink
Provide simplest startServer overload akka#873
Browse files Browse the repository at this point in the history
Issue: akka#873
Provide start server with only host and port
Update docu
  • Loading branch information
jlprat committed Feb 18, 2017
1 parent e20245c commit dc84b28
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ class HttpAppSpec extends AkkaSpec with RequestBuilding with Eventually {

"HttpApp Java" should {

"start only with host and port" in withMinimal { (minimal, host, port)

val server = Future {
minimal.startServer(host, port)
}

minimal.bindingPromise.get(5, TimeUnit.SECONDS)

// Requesting the server to shutdown
callAndVerify(host, port, "shutdown")

Await.ready(server, Duration(1, TimeUnit.SECONDS))
server.isCompleted should ===(true)

}

"start without ActorSystem" in withMinimal { (minimal, host, port)

val server = Future {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ class HttpAppSpec extends AkkaSpec with Directives with RequestBuilding with Eve

"HttpApp" should {

"start only with host and port" in withMinimal { (minimal, host, port)
val server = Future {
minimal.startServer(host, port)
}

Await.result(minimal.bindingPromise.future, Duration(5, TimeUnit.SECONDS))

// Requesting the server to shutdown
callAndVerify(host, port, "shutdown")
Await.ready(server, Duration(1, TimeUnit.SECONDS))
server.isCompleted should ===(true)
}

"start without ActorSystem" in withMinimal { (minimal, host, port)

val server = Future {
Expand Down
9 changes: 9 additions & 0 deletions akka-http/src/main/java/akka/http/javadsl/server/HttpApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.settings.ServerSettings;
import akka.stream.ActorMaterializer;
import com.typesafe.config.ConfigFactory;

import java.io.IOException;
import java.util.Optional;
Expand All @@ -33,6 +34,14 @@ public abstract class HttpApp extends AllDirectives {
*/
protected AtomicReference<ActorSystem> systemReference = new AtomicReference<>();

/**
* Start a server on the specified host and port.
* Note that this method is blocking.
*/
public void startServer(String host, int port) throws ExecutionException, InterruptedException {
startServer(host, port, ServerSettings.create(ConfigFactory.load()));
}

/**
* Start a server on the specified host and port, using provided settings.
* Note that this method is blocking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.stream.ActorMaterializer
import akka.http.scaladsl.settings.ServerSettings
import com.typesafe.config.ConfigFactory

import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, ExecutionContext, Future }
Expand All @@ -33,6 +34,14 @@ trait HttpApp {
*/
protected val systemReference = new AtomicReference[ActorSystem]()

/**
* Start a server on the specified host and port.
* Note that this method is blocking
*/
def startServer(host: String, port: Int): Unit = {
startServer(host, port, ServerSettings(ConfigFactory.load))
}

/**
* Start a server on the specified host and port, using provided settings.
* Note that this method is blocking.
Expand Down
12 changes: 10 additions & 2 deletions docs/src/main/paradox/java/http/routing-dsl/HttpApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ If desired, `HttpApp` provides different hook methods that can be overridden to

The following example shows how to start a server:

@@snip [HttpAppExampleTest.java](../../../../../test/java/docs/http/javadsl/server/HttpAppExampleTest.java) { #imports #minimal-routing-example }
@@snip [HttpAppExampleTest.java](../../../../../test/java/docs/http/javadsl/server/HttpAppExampleTest.java) { #minimal-imports #minimal-routing-example }

Firstly we define a `class` that extends `HttpApp` and we just implement the routes this server will handle.
After that, we can start a server just by providing a `host`, `port` and `ServerProperties`. Calling `startServer` blocks the current thread until the server is signaled for termination.
After that, we can start a server just by providing a `host` and a `port`. Calling `startServer` blocks the current thread until the server is signaled for termination.
The default behavior of `HttpApp` is to start a server, and shut it down after `ENTER` is pressed. When the call to `startServer` returns the server is properly shut down.

## Reacting to Bind Failures
Expand All @@ -36,6 +36,14 @@ Here you can see an example server that overrides the `postHttpBindingFailure` h

So if the port `80` would be already taken by another app, the call to `startServer` returns immediately and the `postHttpBindingFailure` hook will be called.

## Providing your own Server Settings

`HttpApp` reads the default `ServerSettings` when one is not provided.
In case you want to provide different settings, you can simply pass it to `startServer` as illustrated in the following example:

@@snip [HttpAppExampleTest.java](../../../../../test/java/docs/http/javadsl/server/HttpAppExampleTest.java) { #imports #with-settings-routing-example }


## Providing your own Actor System

`HttpApp` creates its own `ActorSystem` instance when one is not provided.
Expand Down
10 changes: 9 additions & 1 deletion docs/src/main/paradox/scala/http/routing-dsl/HttpApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The following example shows how to start a server:
@@snip [HttpAppExampleSpec.scala](../../../../../test/scala/docs/http/scaladsl/HttpAppExampleSpec.scala) { #minimal-routing-example }

Firstly we define an `object` (it can also be a `class`) that extends `HttpApp` and we just implement the routes this server will handle.
After that, we can start a server just by providing a `host`, `port` and `ServerProperties`. Calling `startServer` blocks the current thread until the server is signaled for termination.
After that, we can start a server just by providing a `host` and a `port`. Calling `startServer` blocks the current thread until the server is signaled for termination.
The default behavior of `HttpApp` is to start a server, and shut it down after `ENTER` is pressed. When the call to `startServer` returns the server is properly shut down.

## Reacting to Bind Failures
Expand All @@ -36,6 +36,14 @@ Here you can see an example server that overrides the `postHttpBindingFailure` h

So if the port `80` would be already taken by another app, the call to `startServer` returns immediately and the `postHttpBindingFailure` hook will be called.

## Providing your own Server Settings

`HttpApp` reads the default `ServerSettings` when one is not provided.
In case you want to provide different settings, you can simply pass it to `startServer` as illustrated in the following example:

@@snip [HttpAppExampleSpec.scala](../../../../../test/scala/docs/http/scaladsl/HttpAppExampleSpec.scala) { #with-settings-routing-example }


## Providing your own Actor System

`HttpApp` creates its own `ActorSystem` instance when one is not provided.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package docs.http.javadsl.server;

//#imports
//#minimal-imports
import akka.Done;
import akka.actor.ActorSystem;
import akka.http.javadsl.server.HttpApp;
import akka.http.javadsl.server.Route;
//#minimal-imports
import akka.http.javadsl.settings.ServerSettings;
import com.typesafe.config.ConfigFactory;
//#imports
Expand All @@ -16,8 +18,10 @@
import scala.runtime.BoxedUnit;
//#selfClosing
//#imports
//#minimal-imports
import java.util.Optional;
import java.util.concurrent.*;
//#minimal-imports
//#imports
//#selfClosing

Expand Down Expand Up @@ -55,11 +59,19 @@ void minimalServer() throws ExecutionException, InterruptedException {
//#minimal-routing-example
// Starting the server
final MinimalHttpApp myServer = new MinimalHttpApp();
myServer.startServer("localhost", 8080, ServerSettings.create(ConfigFactory.load()));
myServer.startServer("localhost", 8080);
//#minimal-routing-example
}


void withSettingsServer() throws ExecutionException, InterruptedException {
//#with-settings-routing-example
// Starting the server
final MinimalHttpApp myServer = new MinimalHttpApp();
myServer.startServer("localhost", 8080, ServerSettings.create(ConfigFactory.load()));
//#with-settings-routing-example
}

static
//#serverTerminationSignal

Expand Down
24 changes: 23 additions & 1 deletion docs/src/test/scala/docs/http/scaladsl/HttpAppExampleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ class HttpAppExampleSpec extends WordSpec with Matchers
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.HttpApp
import akka.http.scaladsl.server.Route

// Server definition
object WebServer extends HttpApp {
def route: Route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
}

// Starting the server
WebServer.startServer("localhost", 8080)
//#minimal-routing-example
}

"with-settings-routing-example" in compileOnlySpec {
//#with-settings-routing-example
import akka.http.scaladsl.model.{ ContentTypes, HttpEntity }
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.HttpApp
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.settings.ServerSettings
import com.typesafe.config.ConfigFactory

Expand All @@ -30,7 +52,7 @@ class HttpAppExampleSpec extends WordSpec with Matchers

// Starting the server
WebServer.startServer("localhost", 8080, ServerSettings(ConfigFactory.load))
//#minimal-routing-example
//#with-settings-routing-example
}

"failed-binding" in compileOnlySpec {
Expand Down

0 comments on commit dc84b28

Please sign in to comment.