@Override public void run(String... args) throws Exception { server.start(); }
@Override public void destroy() throws Exception { server.stop(); }
/** * Convenience method to {@link #of(Action) define} and {@link #start()} the server in one go. * * @param definition the server definition * @return the newly created and started server * @throws Exception any thrown by {@link #of(Action)} or {@link #start()} */ static RatpackServer start(Action<? super RatpackServerSpec> definition) throws Exception { RatpackServer server = of(definition); server.start(); return server; }
/** * {@inheritDoc} */ @Override default URI getAddress() { RatpackServer server = getServer(); try { if (!server.isRunning()) { server.start(); } return new URI(server.getScheme(), null, server.getBindHost(), server.getBindPort(), "/", null, null); } catch (Exception e) { throw uncheck(e); } }
public void execute(final Closure<?> closure) throws Exception { GroovyVersionCheck.ensureRequiredVersionUsed(GroovySystem.getVersion()); lock.lock(); try { if (running != null) { running.stop(); running = null; } Path scriptFile = ClosureUtil.findScript(closure); if (scriptFile == null) { running = RatpackServer.start(new RatpackClosureConfigurer(closure, false)); } else { running = RatpackServer.start(Groovy.Script.app(scriptFile)); } } finally { lock.unlock(); } } }
@Bean public RatpackServer ratpackServer(ApplicationContext context) throws Exception { return RatpackServer.of(ratpackServerSpec -> ratpackServerSpec.serverConfig(serverConfig) .registry(joinedRegistry(context)).handlers(chainConfigurers)); }
/** * Returns the address to the root of the server, starting it if necessary. * * @return the address to the root of the server */ @Override public URI getAddress() { if (server == null) { try { server = createImpositions().impose(() -> { RatpackServer server = createServer(); server.start(); return server; }); } catch (Exception e) { throw uncheck(e); } } URI address; try { address = new URI(server.getScheme() + "://" + server.getBindHost() + ":" + server.getBindPort() + "/"); } catch (URISyntaxException e) { throw uncheck(e); } return address; }
/** * Creates an embedded application from the given function. * * @param definition a function that defines the server * @return a newly created embedded application * @throws java.lang.Exception if an error is encountered creating the application * @see ratpack.server.RatpackServer#of(Action) */ static EmbeddedApp of(Action<? super RatpackServerSpec> definition) throws Exception { return fromServer(RatpackServer.of(d -> definition.execute(new EmbeddedRatpackServerSpec(d)))); }
/** * Starts the server defined by calling {@link Groovy.Script#appWithArgs(String...)}. * * @param args not used * @throws Exception any when starting the server */ public static void main(String... args) throws Exception { RatpackServer.start(Groovy.Script.appWithArgs(args)); } }
/** * Stops the server returned by {@link #getServer()}. * <p> * Exceptions thrown by calling {@link RatpackServer#stop()} are suppressed and written to {@link System#err System.err}. */ @Override default void close() { try { getServer().stop(); } catch (Exception e) { LoggerFactory.getLogger(this.getClass()).error("", e); } }
/** * Creates an embedded application using the given server config, and server creating function. * * @param serverConfig the server configuration * @param definition a function to create the server to embed * @return a newly created embedded application */ static EmbeddedApp fromServer(ServerConfig serverConfig, Action<? super RatpackServerSpec> definition) { return fromServer(uncheck(() -> RatpackServer.of(b -> definition.execute(b.serverConfig(serverConfig))))); }
); RatpackServer.start(serverSpec -> serverSpec .serverConfig(config -> config .baseDir(BaseDir.find())
/** * Stops the server if it is running. * * @see RatpackServer#stop() */ public void stop() { if (server != null) { try { server.stop(); server = null; } catch (Exception e) { throw uncheck(e); } } }
@Override public void init() { try { RatpackServer.start(server -> { server.serverConfig(c -> { for(Action<ServerConfigBuilder> cfg : getConfigurators()) { c = cfg.with(c); } }); server.registry(Guice.registry(b -> { for(Action<BindingsSpec> binding : getBindings()) { b = binding.with(b); } })); server.handlers(chain -> { for(Action<Chain> handler : getHandlers()) { // chain = chain.insert(handler); chain = handler.with(chain); } }); }); } catch (Exception e) { logger.error("Ratpack failure", e); } }
ratpackServer.stop(); return null; }))
public static void main(String... args) throws Exception { Db db = new Db(); RatpackServer.start(server -> server .registryOf(rs -> rs.add(db)) .handlers(chain -> chain .all(ctx -> { LOGGER.info("All"); ctx.next(); }) .get("callout", ctx -> { Db database = ctx.get(Db.class); Promise<String> response = ctx.get(HttpClient.class) .get(new URI("http://wiremock:7070/name")) .map(r -> r.getBody().getText()); Promise<String> withDb = response.flatMap(database::getRecord); ctx.render(withDb); }) .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!")) )); } }
public static void main(String[] args) throws Exception { RatpackServer.start(s -> s .serverConfig(c -> c.baseDir(BaseDir.find())) .registry(Guice.registry(b -> b.module(MyModule.class))) .handlers(chain -> chain .path("foo", ctx -> ctx.render("from the foo handler")) // Map to /foo .path("bar", ctx -> ctx.render("from the bar handler")) // Map to /bar .prefix("nested", nested -> { // Set up a nested routing block, which is delegated to `nestedHandler` nested.path(":var1/:var2?", ctx -> { // The path tokens are the :var1 and :var2 path components above Map<String, String> pathTokens = ctx.getPathTokens(); ctx.render( "from the nested handler, var1: " + pathTokens.get("var1") + ", var2: " + pathTokens.get("var2") ); }); }) .path("injected", MyHandler.class) // Map to a dependency injected handler .prefix("static", nested -> nested.fileSystem("assets/images", Chain::files)) // Bind the /static app path to the src/ratpack/assets/images dir .all(ctx -> ctx.render("root handler!")) ) ); } }
public static void main(String... args) throws Exception { ServerConfig serverConfig = ServerConfig.of(config -> config .port(5050) .baseDir(BaseDir.find()) .json("proxy-config.json") .require("/rpcclient", RpcConfig.class) ); RatpackServer.start (server -> server .serverConfig(serverConfig) .registry(Guice.registry(b -> b. moduleConfig(BitcoinRpcProxyModule.class, serverConfig.get("/rpcclient", RpcConfig.class)))) .handlers(chain -> chain .post("rpc", RpcProxyHandler.class) .get("status", ChainStatusHandler.class) .get("gen", GenerateHandler.class) .get(ctx -> ctx.getResponse().send("Hello world! (Not RPC)")) ) ); } }