Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to directly configure Akka HTTP settings (#8392)
* Allow users to directly configure Akka HTTP settings Fixes #8368. Refactor the AkkaHttpServer class so that more of its internal behavior is accessible to advanced users. Users can now extend this class and override behavior through protected methods. The custom class can be hooked into Play through the existing play.server.provider setting. By allowing users to change the Akka HTTP server behavior, particularly the server settings, we reduce the pressure on Play to provide a perfect and universal configuration out of the box. We can now provide a configuration that is right for most users and allow advanced users to reconfigure their settings as needed. For example, it is now possible to modify the Akka HTTP server's configuration to extend the list of HTTP methods that is supported. This example is shown in an integration test. * Reduce API surface; use context * Documentation * Fix compile error * Add documentation source files * Fix doc tests; minor doc tweak * Copyright to 2018 * Fix broken doc links * Added blank line after header
- Loading branch information
1 parent
6768cb9
commit ba0ce3e
Showing
12 changed files
with
338 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
documentation/manual/working/commonGuide/configuration/code/CustomAkkaHttpServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
//#custom-akka-http-server | ||
//###replace: package server | ||
package detailedtopics.configuration.customakkaserver | ||
|
||
import java.util.Random | ||
import play.core.server.{AkkaHttpServer, AkkaHttpServerProvider, ServerProvider} | ||
import akka.http.scaladsl.ConnectionContext | ||
import akka.http.scaladsl.model.HttpMethod | ||
import akka.http.scaladsl.settings.{ParserSettings, ServerSettings} | ||
|
||
/** A custom Akka HTTP server with advanced configuration. */ | ||
class CustomAkkaHttpServer(context: AkkaHttpServer.Context) extends AkkaHttpServer(context) { | ||
override protected def createParserSettings(): ParserSettings = { | ||
val defaultSettings: ParserSettings = | ||
super.createParserSettings() | ||
defaultSettings.withCustomMethods(HttpMethod.custom("TICKLE")) | ||
} | ||
override protected def createServerSettings(port: Int, connectionContext: ConnectionContext, secure: Boolean): ServerSettings = { | ||
val defaultSettings: ServerSettings = | ||
super.createServerSettings(port, connectionContext, secure) | ||
defaultSettings.withWebsocketRandomFactory(() => new Random()) | ||
} | ||
} | ||
|
||
/** A factory that instantiates a CustomAkkaHttpServer. */ | ||
class CustomAkkaHttpServerProvider extends ServerProvider { | ||
def createServer(context: ServerProvider.Context) = { | ||
val serverContext = AkkaHttpServer.Context.fromServerProviderContext(context) | ||
new CustomAkkaHttpServer(serverContext) | ||
} | ||
} | ||
//#custom-akka-http-server |
4 changes: 4 additions & 0 deletions
4
documentation/manual/working/commonGuide/configuration/code/application.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//#custom-akka-http-server-provider | ||
//###replace: play.server.provider = server.CustomAkkaHttpServerProvider | ||
#play.server.provider = server.CustomAkkaHttpServerProvider | ||
//#custom-akka-http-server-provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.