public interface ServerConfigBuilder extends ConfigDataBuilder
ServerConfig
.DEFAULT_ENV_PREFIX, DEFAULT_PROP_PREFIX
Modifier and Type | Method and Description |
---|---|
ServerConfigBuilder |
add(ConfigSource configSource)
Adds a configuration source.
|
ServerConfigBuilder |
address(java.net.InetAddress address)
Sets the address to bind to.
|
ServerConfigBuilder |
args(java.lang.String[] args)
Adds the given args as a config source.
|
ServerConfigBuilder |
args(java.lang.String separator,
java.lang.String[] args)
Invokes
args(String, String, String[]) , with no prefix. |
ServerConfigBuilder |
args(java.lang.String prefix,
java.lang.String separator,
java.lang.String[] args)
Adds a configuration source for the given string args.
|
default ServerConfigBuilder |
baseDir(java.io.File file)
|
ServerConfigBuilder |
baseDir(java.nio.file.Path baseDir)
Sets the root of the filesystem for the application.
|
ServerConfig |
build()
Builds the server config.
|
ServerConfigBuilder |
configureObjectMapper(Action<ObjectMapper> action)
Configures the object mapper used for binding configuration data to arbitrary objects.
|
ServerConfigBuilder |
connectQueueSize(int connectQueueSize)
The maximum amount of connections that may be waiting to be accepted at any time.
|
ServerConfigBuilder |
connectTimeoutMillis(int connectTimeoutMillis)
The connect timeout of the channel.
|
ServerConfigBuilder |
development(boolean development)
Whether or not the application is "development".
|
ServerConfigBuilder |
env()
Adds a configuration source for environment variables starting with the prefix "RATPACK_".
|
ServerConfigBuilder |
env(EnvironmentParser environmentParser)
Adds a configuration source for environment variables using custom parsing logic.
|
ServerConfigBuilder |
env(java.lang.String prefix)
Adds a configuration source for environment variables starting with the specified prefix.
|
ServerConfigBuilder |
env(java.lang.String prefix,
Function<java.lang.String,java.lang.String> mapFunc)
Adds a configuration source for environment variables starting with the specified prefix.
|
default ServerConfigBuilder |
findBaseDir()
Sets the base dir using
BaseDir.find() . |
default ServerConfigBuilder |
findBaseDir(java.lang.String markerFilePath)
Sets the base dir using
BaseDir.find(String) . |
ServerConfigBuilder |
idleTimeout(java.time.Duration idleTimeout)
The default read timeout of the channel.
|
ServerConfigBuilder |
json(com.google.common.io.ByteSource byteSource)
Adds a configuration source for a JSON file.
|
ServerConfigBuilder |
json(java.nio.file.Path path)
Adds a configuration source for a JSON file.
|
ServerConfigBuilder |
json(java.lang.String path)
Adds the JSON file at the given path as a configuration source.
|
ServerConfigBuilder |
json(java.net.URL url)
Adds a configuration source for a JSON file.
|
ServerConfigBuilder |
maxChunkSize(int maxChunkSize)
The maximum size of read chunks of request/response bodies.
|
ServerConfigBuilder |
maxContentLength(int maxContentLength)
The max number of bytes a request body can be.
|
ServerConfigBuilder |
maxHeaderSize(int maxHeaderSize)
The maximum size of all headers allowed for reading http requests.
|
ServerConfigBuilder |
maxInitialLineLength(int maxInitialLineLength)
The maximum initial line length allowed for reading http requests.
|
ServerConfigBuilder |
maxMessagesPerRead(int maxMessagesPerRead)
The maximum number of messages to read per read loop.
|
ServerConfigBuilder |
object(java.lang.String path,
java.lang.Object object)
Adds the object's fields at the given path as a configuration source.
|
ServerConfigBuilder |
onError(Action<? super java.lang.Throwable> errorHandler)
Sets the error all that will be used for added configuration sources.
|
ServerConfigBuilder |
port(int port)
Sets the port to listen for requests on.
|
ServerConfigBuilder |
portFile(java.nio.file.Path portFile)
The path where to store the bind port
|
ServerConfigBuilder |
props(com.google.common.io.ByteSource byteSource)
Adds a configuration source for a properties file.
|
ServerConfigBuilder |
props(java.util.Map<java.lang.String,java.lang.String> map)
Adds a configuration source for a Map (flat key-value pairs).
|
ServerConfigBuilder |
props(java.nio.file.Path path)
Adds a configuration source for a properties file.
|
ServerConfigBuilder |
props(java.util.Properties properties)
Adds a configuration source for a properties object.
|
ServerConfigBuilder |
props(java.lang.String path)
Adds the properties file at the given path as a configuration source.
|
ServerConfigBuilder |
props(java.net.URL url)
Adds a configuration source for a properties file.
|
ServerConfigBuilder |
publicAddress(java.net.URI publicAddress)
The public address of the application.
|
ServerConfigBuilder |
receiveBufferSize(int receiveBufferSize)
The StandardSocketOptions.SO_RCVBUF option.
|
ServerConfigBuilder |
registerShutdownHook(boolean registerShutdownHook)
Whether or not to register a JVM shutdown hook to gracefully stop the server.
|
default ServerConfigBuilder |
require(java.lang.String pointer,
java.lang.Class<?> type)
Declares that it is required that the server config provide an object of the given type at the given path.
|
default ServerConfigBuilder |
require(java.lang.String pointer,
java.lang.reflect.Type type)
Declares that it is required that the server config provide an object of the given type at the given path.
|
ServerConfigBuilder |
require(java.lang.String pointer,
com.google.common.reflect.TypeToken<?> type)
Declares that it is required that the server config provide an object of the given type at the given path.
|
ServerConfigBuilder |
requireClientSslAuth(boolean requireClientSslAuth)
Deprecated.
since 1.5, prefer
ssl(SslContext) |
ServerConfigBuilder |
ssl(io.netty.handler.ssl.SslContext sslContext)
The SSL context to use if the application serves content over HTTPS.
|
ServerConfigBuilder |
ssl(javax.net.ssl.SSLContext sslContext)
Deprecated.
since 1.5, replaced by
ssl(SslContext) |
ServerConfigBuilder |
sysProps()
Adds a configuration source for system properties starting with the prefix "ratpack.".
|
ServerConfigBuilder |
sysProps(java.lang.String prefix)
Adds a configuration source for system properties starting with the specified prefix.
|
ServerConfigBuilder |
threads(int threads)
The number of threads to use.
|
ServerConfigBuilder |
writeSpinCount(int writeSpinCount)
The maximum loop count for a write operation until WritableByteChannel.write(ByteBuffer) returns a non-zero value.
|
ServerConfigBuilder |
yaml(com.google.common.io.ByteSource byteSource)
Adds a configuration source for a YAML file.
|
ServerConfigBuilder |
yaml(java.nio.file.Path path)
Adds a configuration source for a YAML file.
|
ServerConfigBuilder |
yaml(java.lang.String path)
Adds the YAML file at the given path as a configuration source.
|
ServerConfigBuilder |
yaml(java.net.URL url)
Adds a configuration source for a YAML file.
|
getConfigSources, getObjectMapper, jacksonModules
ServerConfigBuilder baseDir(java.nio.file.Path baseDir)
The base dir
acts as the portable file system for the application.
All paths within the application, resolved by Ratpack API are resolved from this point.
For example, Chain.files(Action)
allows serving static files within the base dir.
The base dir is also used to resolve paths to file system locations when using json(String)
, yaml(String)
and props(String)
.
This allows config files to travel with the application within the base dir.
It is generally desirable to use the BaseDir.find()
to dynamically find the base dir at runtime.
baseDir
- the base dirthis
default ServerConfigBuilder baseDir(java.io.File file)
file
- the base dirthis
default ServerConfigBuilder findBaseDir()
BaseDir.find()
.this
default ServerConfigBuilder findBaseDir(java.lang.String markerFilePath)
BaseDir.find(String)
.markerFilePath
- the path to the marker file on the classpaththis
ServerConfigBuilder port(int port)
Defaults to 5050.
port
- the port to listen for requests onthis
ServerConfig.getPort()
ServerConfigBuilder address(java.net.InetAddress address)
Default value is null
.
address
- The address to bind tothis
ServerConfig.getAddress()
ServerConfigBuilder development(boolean development)
Default value is false
.
development
- Whether or not the application is "development".this
ServerConfig.isDevelopment()
ServerConfigBuilder threads(int threads)
Defaults to ServerConfig.DEFAULT_THREADS
threads
- the size of the event loop thread poolthis
ServerConfig.getThreads()
ServerConfigBuilder registerShutdownHook(boolean registerShutdownHook)
Default value is true
.
registerShutdownHook
- whether to register or notthis
ServerConfig.isRegisterShutdownHook()
ServerConfigBuilder publicAddress(java.net.URI publicAddress)
Default value is null
.
publicAddress
- the public address of the applicationthis
ServerConfig.getPublicAddress()
ServerConfigBuilder maxContentLength(int maxContentLength)
1048576
(1 megabyte).maxContentLength
- the max content length to acceptthis
ServerConfig.getMaxContentLength()
ServerConfigBuilder maxChunkSize(int maxChunkSize)
ServerConfig.DEFAULT_MAX_CHUNK_SIZE
.maxChunkSize
- the maximum size of read chunks of request/response bodiesthis
ServerConfig.getMaxChunkSize()
ServerConfigBuilder maxInitialLineLength(int maxInitialLineLength)
ServerConfig.DEFAULT_MAX_INITIAL_LINE_LENGTH
.maxInitialLineLength
- the maximum length of the initial line of the request.this
ServerConfig.getMaxInitialLineLength()
ServerConfigBuilder maxHeaderSize(int maxHeaderSize)
ServerConfig.DEFAULT_MAX_HEADER_SIZE
.maxHeaderSize
- the maximum size of the sum of the length of all headers.this
ServerConfig.getMaxHeaderSize()
ServerConfigBuilder connectTimeoutMillis(int connectTimeoutMillis)
connectTimeoutMillis
- the connect timeout in millisecondsthis
ServerConfig.getConnectTimeoutMillis()
ServerConfigBuilder idleTimeout(java.time.Duration idleTimeout)
idleTimeout
- the idleTimeout (Duration.ZERO
= no timeout, must not be negative, must not be null)this
ServerConfig.getIdleTimeout()
ServerConfigBuilder maxMessagesPerRead(int maxMessagesPerRead)
maxMessagesPerRead
- the max messages per readthis
ServerConfig.getMaxMessagesPerRead()
ServerConfigBuilder receiveBufferSize(int receiveBufferSize)
receiveBufferSize
- the recieve buffer sizethis
ServerConfig.getReceiveBufferSize()
ServerConfigBuilder connectQueueSize(int connectQueueSize)
This is effectively the SO_BACKLOG
standard socket parameter.
If the queue is full (i.e. there are too many pending connections), connection attempts will be rejected.
Established connections are not part of this queue so do not contribute towards the limit.
The default value is platform specific, but usually either 200 or 128. Most application do not need to change this default.
connectQueueSize
- connection queue sizeServerConfigBuilder writeSpinCount(int writeSpinCount)
writeSpinCount
- the write spin countthis
ServerConfig.getWriteSpinCount()
ServerConfigBuilder portFile(java.nio.file.Path portFile)
portFile
- the path filethis
ServerConfig.getPortFile()
@Deprecated ServerConfigBuilder ssl(javax.net.ssl.SSLContext sslContext)
ssl(SslContext)
sslContext
- the SSL contextthis
SSLContexts
,
ServerConfig.getSslContext()
@Deprecated ServerConfigBuilder requireClientSslAuth(boolean requireClientSslAuth)
ssl(SslContext)
requireClientSslAuth
- whether or not server needs client SSL authenticationthis
ServerConfigBuilder ssl(io.netty.handler.ssl.SslContext sslContext)
sslContext
- the SSL contextthis
SslContextBuilder
ServerConfigBuilder env()
The prefix will be removed before loading the data. The environment variable name is split into per-object segments using double underscore as an object boundary. Segments are transformed into camel-case field names using a single underscore as a word boundary.
env
in interface ConfigDataBuilder
ServerConfigBuilder env(java.lang.String prefix)
env
in interface ConfigDataBuilder
prefix
- the prefix which should be used to identify relevant environment variablesServerConfigBuilder args(java.lang.String[] args)
This method is designed to be used with the args var of the static main(String... args)
application entry point.
This allows configuration parameters to be passed to the application on the command line.
Each arg should be of the format «key»=«value»
.
For the following example, the application has been started with the argument thing.name=foo
.
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.assertEquals;
public class Example {
static class Thing {
public String name;
}
public static void main(String... args) throws Exception {
EmbeddedApp.of(a -> a
.serverConfig(s -> s
.args(args)
.require("/thing", Thing.class)
)
.handlers(c -> c
.get(ctx -> ctx.render(ctx.get(Thing.class).name))
)
).test(httpClient ->
assertEquals(httpClient.getText(), "foo")
);
}
}
args
in interface ConfigDataBuilder
args
- the argument valuesthis
ServerConfigBuilder args(java.lang.String separator, java.lang.String[] args)
args(String, String, String[])
, with no prefix.args
in interface ConfigDataBuilder
separator
- the separator of the key and value in each argargs
- the argument valuesthis
args(String[])
ServerConfigBuilder args(java.lang.String prefix, java.lang.String separator, java.lang.String[] args)
Args that do not start with the given prefix
are ignored.
The remaining are each split using the given separator
(as a literal string, not as a regex),
then trimmed of the prefix.
args
in interface ConfigDataBuilder
prefix
- the prefix that each arg must have to be considered (use null
or ""
for no prefix)separator
- the separator between the key and the valueargs
- the argument valuesthis
args(String[])
ServerConfigBuilder props(com.google.common.io.ByteSource byteSource)
props
in interface ConfigDataBuilder
byteSource
- the source of the properties datathis
ServerConfigBuilder props(java.lang.String path)
If a base dir is set, the path will be resolved relative to it. Otherwise, it will be resolved relative to the file system root.
props
in interface ConfigDataBuilder
path
- the path to the filethis
ServerConfigBuilder props(java.nio.file.Path path)
props
in interface ConfigDataBuilder
path
- the source of the properties datathis
ServerConfigBuilder props(java.util.Properties properties)
props
in interface ConfigDataBuilder
properties
- the properties objectthis
ServerConfigBuilder object(java.lang.String path, java.lang.Object object)
The path is a period separated key string. The given object is subject to value merging and overrides as per other config sources.
import ratpack.config.ConfigData;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
public class Example {
static class Thing {
public String f1;
public String f2;
public String f3;
}
public static void main(String... args) throws Exception {
Thing input = new Thing();
input.f1 = "1";
input.f2 = "2";
ConfigData configData = ConfigData.of(c -> c
.object("thing", input)
.props(Collections.singletonMap("thing.f1", "changed"))
.object("thing.f3", "changed")
);
Thing thing = configData.get("/thing", Thing.class);
assertEquals("changed", thing.f1);
assertEquals("2", thing.f2);
assertEquals("changed", thing.f3);
}
}
object
in interface ConfigDataBuilder
path
- the configuration path the object's fields should be mapped on toobject
- the object from which to derive the configuration fieldsthis
ServerConfigBuilder props(java.util.Map<java.lang.String,java.lang.String> map)
import com.google.common.collect.ImmutableMap;
import ratpack.config.ConfigData;
import ratpack.server.ServerConfig;
import static org.junit.Assert.*;
public class Example {
public static void main(String[] args) throws Exception {
ServerConfig serverConfig = ServerConfig
.builder()
.props(ImmutableMap.of("server.port", "5060"))
.sysProps()
.build();
assertEquals(5060, serverConfig.getPort());
}
}
props
in interface ConfigDataBuilder
map
- the mapthis
ServerConfigBuilder props(java.net.URL url)
props
in interface ConfigDataBuilder
url
- the source of the properties datathis
ServerConfigBuilder sysProps()
sysProps
in interface ConfigDataBuilder
this
ServerConfigBuilder sysProps(java.lang.String prefix)
sysProps
in interface ConfigDataBuilder
prefix
- the prefix which should be used to identify relevant system properties;
the prefix will be removed before loading the datathis
ServerConfigBuilder onError(Action<? super java.lang.Throwable> errorHandler)
onError
in interface ConfigDataBuilder
errorHandler
- the error allthis
Action.noop()
,
Action.throwException()
ServerConfigBuilder configureObjectMapper(Action<ObjectMapper> action)
configureObjectMapper
in interface ConfigDataBuilder
action
- an action to perform upon the object mapperthis
ServerConfigBuilder add(ConfigSource configSource)
add
in interface ConfigDataBuilder
configSource
- the configuration source to addthis
ServerConfigBuilder env(java.lang.String prefix, Function<java.lang.String,java.lang.String> mapFunc)
env
in interface ConfigDataBuilder
prefix
- the prefix which should be used to identify relevant environment variablesmapFunc
- the function to transform segments into field namesServerConfigBuilder env(EnvironmentParser environmentParser)
env
in interface ConfigDataBuilder
environmentParser
- the parser to use to interpret environment variablesthis
ServerConfigBuilder json(com.google.common.io.ByteSource byteSource)
json
in interface ConfigDataBuilder
byteSource
- the source of the JSON datathis
ServerConfigBuilder json(java.nio.file.Path path)
json
in interface ConfigDataBuilder
path
- the source of the JSON datathis
ServerConfigBuilder json(java.lang.String path)
If a base dir is set, the path will be resolved relative to it. Otherwise, it will be resolved relative to the file system root.
json
in interface ConfigDataBuilder
path
- the path to the filethis
ServerConfigBuilder json(java.net.URL url)
json
in interface ConfigDataBuilder
url
- the source of the JSON datathis
ServerConfigBuilder yaml(com.google.common.io.ByteSource byteSource)
yaml
in interface ConfigDataBuilder
byteSource
- the source of the YAML datathis
ServerConfigBuilder yaml(java.nio.file.Path path)
yaml
in interface ConfigDataBuilder
path
- the source of the YAML datathis
ServerConfigBuilder yaml(java.lang.String path)
If a base dir is set, the path will be resolved relative to it. Otherwise, it will be resolved relative to the file system root.
yaml
in interface ConfigDataBuilder
path
- the path to the filethis
ServerConfigBuilder yaml(java.net.URL url)
yaml
in interface ConfigDataBuilder
url
- the source of the YAML datathis
default ServerConfigBuilder require(java.lang.String pointer, java.lang.Class<?> type)
The build()
method will fail if the config is not able to provide the requested object.
All objects declared using this method will also automatically be implicitly added to the base registry.
The pointer
argument is of the same format given to the ConfigData.get(String, Class)
method.
import org.junit.Assert;
import ratpack.test.embed.EmbeddedApp;
import java.util.Collections;
public class Example {
static class MyConfig {
public String value;
}
public static void main(String... args) throws Exception {
EmbeddedApp.of(a -> a
.serverConfig(s -> s
.props(Collections.singletonMap("config.value", "foo"))
.require("/config", MyConfig.class)
)
.handlers(c -> c
.get(ctx -> ctx.render(ctx.get(MyConfig.class).value))
)
).test(httpClient ->
Assert.assertEquals("foo", httpClient.getText())
);
}
}
pointer
- a JSON Pointer specifying the point in the configuration data to bind fromtype
- the class of the type to bind tothis
default ServerConfigBuilder require(java.lang.String pointer, java.lang.reflect.Type type)
pointer
- a JSON Pointer specifying the point in the configuration data to bind fromtype
- the type to bind tothis
ServerConfigBuilder require(java.lang.String pointer, com.google.common.reflect.TypeToken<?> type)
pointer
- a JSON Pointer specifying the point in the configuration data to bind fromtype
- the type to bind tothis
ServerConfig build()
build
in interface ConfigDataBuilder
ServerConfigImposition