@Override public void run(String... args) throws Exception { server.start(); }
/** * 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; }
/** * 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)); } }
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(); } } }
/** * {@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); } }
/** * 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; }
); RatpackServer.start(serverSpec -> serverSpec .serverConfig(config -> config .baseDir(BaseDir.find())
@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); } }
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)")) ) ); } }