public class ClosureBackedEmbeddedApplication extends LaunchConfigEmbeddedApplication
EmbeddedApplication
implementation that allows the application to be defined in code at runtime.
This implementation is usually sufficient for testing Ratpack modules or extensions.
import ratpack.test.embed.PathBaseDirBuilder import ratpack.groovy.test.TestHttpClients import ratpack.session.SessionModule import java.nio.file.Files import static ratpack.groovy.test.embed.EmbeddedApplications.embeddedApp def tmp = Files.createTempDirectory("ratpack-test") def baseDir = new PathBaseDirBuilder(tmp) // Create a file within the application base dir baseDir.file "public/foo.txt", "bar" def app = embeddedApp(baseDir) { // Configure the launch config launchConfig { other "some.other.property": "value" reloadable true } // Configure the module registry bindings { add new SessionModule() } // Use the GroovyChain DSL for defining the application handlers handlers { get { render "root" } assets "public" } } // Test the application with the test http client def client = TestHttpClients.testHttpClient(app) assert client.getText() == "root" assert client.getText("foo.txt") == "bar" // Cleanup app.close()
PathBaseDirBuilder
,
EmbeddedApplications
Constructor and Description |
---|
ClosureBackedEmbeddedApplication()
Constructor.
|
ClosureBackedEmbeddedApplication(BaseDirBuilder baseDirBuilder)
Constructor.
|
ClosureBackedEmbeddedApplication(Factory<Path> baseDirFactory)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
bindings(Closure<?> closure)
Specifies the bindings of the application.
|
protected Action<? super BindingsSpec> |
createBindingsAction()
Provides the module registry configuration action.
|
protected GuiceBackedHandlerFactory |
createHandlerFactory(LaunchConfig launchConfig)
Returns the factory to use to create the actual handler.
|
protected Function<? super Injector,? extends Handler> |
createHandlerTransformer(LaunchConfig launchConfig)
Provides the object that, given the
Injector created by the module definition, creates the application handler. |
protected Function<? super Module,? extends Injector> |
createInjectorFactory(LaunchConfig launchConfig)
Creates a module to injector transformer based on the given launch config.
|
protected LaunchConfig |
createLaunchConfig()
Constructs the launch config using the other
create* methods. |
List<Module> |
getModules()
A mutable list of modules to use in this application.
|
void |
handlers(Closure<?> closure)
Specifies the handlers of the application.
|
void |
launchConfig(Closure<?> closure)
Modifies the launch config of the application.
|
createServer
close, getAddress, getServer
public ClosureBackedEmbeddedApplication()
public ClosureBackedEmbeddedApplication(Factory<Path> baseDirFactory)
Useful for deferring the base dir determination
baseDirFactory
- The factory for the base dirpublic ClosureBackedEmbeddedApplication(BaseDirBuilder baseDirBuilder)
baseDirBuilder
- the builder whose BaseDirBuilder.build()
method will be called to provide the base dir for this apppublic List<Module> getModules()
protected LaunchConfig createLaunchConfig()
create*
methods.
A launch config will be created using LaunchConfigBuilder.baseDir(java.io.File)
, with the path returned by the factory given at construction.
The launch config will also default to using a port value of 0
(i.e. an ephemeral port).
Register modules and objects using the bindings(groovy.lang.Closure)
method.
Define the handlers using the handlers(groovy.lang.Closure)
method.
Prefer overriding specific create*
methods instead of this one.
createLaunchConfig
in class LaunchConfigEmbeddedApplication
public void handlers(@DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> closure)
The given closure will not be executed until this application is started.
Subsequent calls to this method will replace the previous definition. Calling this method after the application has started has no effect.
closure
- The definition of the application handlerspublic void bindings(@DelegatesTo(value=GroovyBindingsSpec.class,strategy=1) Closure<?> closure)
The given closure will not be executed until this application is started.
Subsequent calls to this method will replace the previous definition. Calling this method after the application has started has no effect.
closure
- The definition of the application handlerspublic void launchConfig(@DelegatesTo(value=LaunchConfigBuilder.class,strategy=1) Closure<?> closure)
The given closure will not be executed until this application is started.
Subsequent calls to this method will replace the previous definition. Calling this method after the application has started has no effect.
closure
- The definition of the application handlersprotected Function<? super Module,? extends Injector> createInjectorFactory(LaunchConfig launchConfig)
Delegates to Guice.newInjectorFactory(ratpack.launch.LaunchConfig)
.
launchConfig
- The launch configGuice.newInjectorFactory(ratpack.launch.LaunchConfig)
with the given launch configprotected GuiceBackedHandlerFactory createHandlerFactory(LaunchConfig launchConfig)
This implementation does not add any implicit behaviour. Subclasses may wish to provide different implementations that do perform implicit behaviour (e.g. implied modules)
launchConfig
- The launch configprotected Action<? super BindingsSpec> createBindingsAction()
This implementation creates an action that registers getModules()
in order, then executes the closure given with bindings(groovy.lang.Closure)
.
protected Function<? super Injector,? extends Handler> createHandlerTransformer(LaunchConfig launchConfig)
Injector
created by the module definition, creates the application handler.
This implementation uses the closure given to handlers(groovy.lang.Closure)
to build a handler using the Groovy.chain(ratpack.launch.LaunchConfig, ratpack.registry.Registry, groovy.lang.Closure)
method.
An registry based on the injector backs the chain.
launchConfig
- the launch config