use of ratpack.server.ServerConfig in project ratpack by ratpack.
the class DefaultRequestFixture method handleChain.
@Override
public HandlingResult handleChain(Action<? super Chain> chainAction) throws Exception {
final DefaultHandlingResult.ResultsHolder results = new DefaultHandlingResult.ResultsHolder();
Registry registry = getEffectiveRegistry(results);
ServerConfig serverConfig = registry.get(ServerConfig.class);
Handler handler = Handlers.chain(serverConfig, registry, chainAction);
return invoke(handler, registry, results);
}
use of ratpack.server.ServerConfig in project ratpack by ratpack.
the class RatpackServerDefinition method build.
public static RatpackServerDefinition build(Action<? super RatpackServerSpec> config) throws Exception {
SpecImpl spec = new SpecImpl();
config.execute(spec);
ServerConfig serverConfig = Optional.ofNullable(spec.serverConfig).orElseGet(() -> ServerConfig.builder().build());
return new RatpackServerDefinition(serverConfig, spec.registry, spec.handler);
}
use of ratpack.server.ServerConfig in project ratpack by ratpack.
the class DefaultRequestFixture method getEffectiveRegistry.
private Registry getEffectiveRegistry(final DefaultHandlingResult.ResultsHolder results) {
ClientErrorHandler clientErrorHandler = (context, statusCode) -> {
results.setClientError(statusCode);
context.getResponse().status(statusCode);
results.getLatch().countDown();
};
ServerErrorHandler serverErrorHandler = (context, throwable1) -> {
results.setThrowable(throwable1);
results.getLatch().countDown();
};
final Registry userRegistry = Registry.builder().add(ClientErrorHandler.class, clientErrorHandler).add(ServerErrorHandler.class, serverErrorHandler).build();
return Exceptions.uncheck(() -> {
ServerConfig serverConfig = serverConfigBuilder.build();
DefaultExecController execController = new DefaultExecController(serverConfig.getThreads());
return ServerRegistry.serverRegistry(new TestServer(), Impositions.none(), execController, serverConfig, r -> userRegistry.join(registryBuilder.build()));
});
}
use of ratpack.server.ServerConfig in project ratpack by ratpack.
the class Guice method buildInjector.
static Injector buildInjector(Registry baseRegistry, Action<? super BindingsSpec> bindingsAction, Function<? super Module, ? extends Injector> injectorFactory) throws Exception {
List<Action<? super Binder>> binderActions = Lists.newLinkedList();
List<Module> modules = Lists.newLinkedList();
ServerConfig serverConfig = baseRegistry.get(ServerConfig.class);
BindingsSpec bindings = new DefaultBindingsSpec(serverConfig, binderActions, modules);
modules.add(new RatpackBaseRegistryModule(baseRegistry));
modules.add(new ConfigModule(serverConfig.getRequiredConfig()));
try {
bindingsAction.execute(bindings);
} catch (Exception e) {
throw uncheck(e);
}
modules.add(new AdHocModule(binderActions));
Optional<BindingsImposition> bindingsImposition = Impositions.current().get(BindingsImposition.class);
if (bindingsImposition.isPresent()) {
BindingsImposition imposition = bindingsImposition.get();
List<Action<? super Binder>> imposedBinderActions = Lists.newLinkedList();
List<Module> imposedModules = Lists.newLinkedList();
BindingsSpec imposedBindings = new DefaultBindingsSpec(serverConfig, imposedBinderActions, imposedModules);
imposition.getBindings().execute(imposedBindings);
imposedModules.add(new AdHocModule(imposedBinderActions));
Module imposedModule = imposedModules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
modules.add(imposedModule);
}
Module masterModule = modules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
return injectorFactory.apply(masterModule);
}
use of ratpack.server.ServerConfig in project ratpack by ratpack.
the class DefaultRatpackServer method start.
@Override
public synchronized void start() throws Exception {
if (isRunning()) {
return;
}
try {
ServerConfig serverConfig;
LOGGER.info("Starting server...");
DefinitionBuild definitionBuild = buildUserDefinition();
if (definitionBuild.error != null) {
if (definitionBuild.getServerConfig().isDevelopment()) {
LOGGER.warn("Exception raised getting server config (will use default config until reload):", definitionBuild.error);
needsReload.set(true);
} else {
throw Exceptions.toException(definitionBuild.error);
}
}
serverConfig = definitionBuild.getServerConfig();
execController = new DefaultExecController(serverConfig.getThreads());
ChannelHandler channelHandler = ExecThreadBinding.bindFor(true, execController, () -> buildHandler(definitionBuild));
channel = buildChannel(serverConfig, channelHandler);
boundAddress = (InetSocketAddress) channel.localAddress();
String startMessage = String.format("Ratpack started %sfor %s://%s:%s", serverConfig.isDevelopment() ? "(development) " : "", getScheme(), getBindHost(), getBindPort());
if (Slf4jNoBindingDetector.isHasBinding()) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info(startMessage);
}
} else {
System.out.println(startMessage);
}
if (serverConfig.isRegisterShutdownHook()) {
shutdownHookThread = new Thread("ratpack-shutdown-thread") {
@Override
public void run() {
try {
DefaultRatpackServer.this.stop();
} catch (Exception ignored) {
ignored.printStackTrace(System.err);
}
}
};
Runtime.getRuntime().addShutdownHook(shutdownHookThread);
}
} catch (Exception e) {
if (execController != null) {
execController.close();
}
stop();
throw e;
}
}
Aggregations