Interface RatpackServerSpec

  • All Known Subinterfaces:
    GroovyRatpackServerSpec

    public interface RatpackServerSpec
    A buildable specification of a Ratpack server.

    This type is used when creating a new Ratpack server, via RatpackServer.of(Action).

    There are three aspects of a Ratpack server, which can be set via the following methods:

    The other methods of this interface are alternate, sometimes more convenient or concise, variants of those methods.

    None of the methods of this interface are additive (i.e. calling handlers(Action) twice will result in the second “value” being used).

    All of the methods are effectively optional, as there are default values for the three different aspects (each of the base methods details what the default value is). However, in practice you almost always want to at least set a request handler.

    • Method Detail

      • registry

        default RatpackServerSpec registry​(Registry registry)
        Sets the user registry to exactly the given registry.
        Parameters:
        registry - the user registry
        Returns:
        this
      • registry

        RatpackServerSpec registry​(Function<? super Registry,​? extends Registry> function)
        Sets the user registry as the return value of the given function.

        The given function receives the “base” registry (i.e. the base infrastructure provided by Ratpack) as its argument.

        If a user registry is not set, an empty registry will be used.

        Parameters:
        function - a function that provides the user registry
        Returns:
        this
      • handler

        default RatpackServerSpec handler​(java.lang.Class<? extends Handler> handlerType)
        Sets the root handler by getting a handler of the given type from the server registry.

        This can be useful when integrating with something that can wire together objects, such as Google Guice.

        
         import ratpack.guice.Guice;
         import ratpack.handling.Context;
         import ratpack.handling.Handler;
         import ratpack.test.embed.EmbeddedApp;
        
         import javax.inject.Inject;
        
         import static org.junit.Assert.assertEquals;
        
         public class Example {
           public static class MyHandler implements Handler {
             private final String value;
        
             @Inject
             public MyHandler(String value) {
               this.value = value;
             }
        
             public void handle(Context ctx) throws Exception {
               ctx.render(value);
             }
           }
        
           public static void main(String... args) throws Exception {
             EmbeddedApp.of(s -> s
                 .registry(Guice.registry(b -> b
                     .bindInstance("Hello World!")
                     .bind(MyHandler.class)
                 ))
                 .handler(MyHandler.class)
             ).test(httpClient ->
                 assertEquals("Hello World!", httpClient.getText())
             );
           }
         }
         
        Parameters:
        handlerType - the type of handler to retrieve from the registry
        Returns:
        this
      • handlers

        default RatpackServerSpec handlers​(Action<? super Chain> handlers)
        Sets the root handler to the chain specified by the given action.

        The server registry is available during the action via the Chain.getRegistry() method of the given chain.

        Parameters:
        handlers - an action defining a handler chain
        Returns:
        this
        See Also:
        Chain
      • handler

        RatpackServerSpec handler​(Function<? super Registry,​? extends Handler> handlerFactory)
        Sets the root handler to the return of the given function.

        The given function receives the effective server registry. This is the base registry (common Ratpack infrastructure) joined with the user registry (i.e. the registry set on this spec).

        All requests will be routed to the given handler.

        Generally, it is more convenient to use the handlers(Action) method than this as it makes it easy to build a handler chain.

        The Handlers type provides handler implementations that may be of use.

        If a handler is not set, the handler returned by Handlers.notFound() will be used (i.e. all requests will result in a 404).

        Parameters:
        handlerFactory - a factory for the root handler
        Returns:
        this
        See Also:
        Handlers, handlers(Action)