import ratpack.exec.Blocking;
import ratpack.hikari.HikariModule;
import ratpack.server.BaseDir;
import ratpack.server.RatpackServer;
import ratpack.groovy.template.TextTemplateModule;
import ratpack.guice.Guice;

import static ratpack.groovy.Groovy.groovyTemplate;

import java.util.*;
import java.sql.*;

import javax.sql.DataSource;

public class Main {
  public static void main(String... args) throws Exception {
    RatpackServer.start(s -> s
        .serverConfig(c -> c
          .baseDir(BaseDir.find())
          .env())

        .registry(Guice.registry(b -> {
          if (System.getenv("JDBC_DATABASE_URL") != null) {
            b.module(HikariModule.class, conf -> {
              conf.addDataSourceProperty("URL", System.getenv("JDBC_DATABASE_URL"));
              conf.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
            });
          }
          b.module(TextTemplateModule.class, conf -> conf.setStaticallyCompile(true));
        }))

        .handlers(chain -> chain
            .get(ctx -> ctx.render(groovyTemplate("index.html")))

            .get("hello", ctx -> {
              ctx.render("Hello!");
            })

            .get("db", ctx -> {
              Blocking.get(() -> {
                try (Connection connection = ctx.get(DataSource.class).getConnection()) {
                  Statement stmt = connection.createStatement();
                  stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
                  stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
                  return stmt.executeQuery("SELECT tick FROM ticks");
                }
              }).onError(throwable -> {
                Map<String, Object> attributes = new HashMap<>();
                attributes.put("message", "There was an error: " + throwable);
                ctx.render(groovyTemplate(attributes, "error.html"));
              }).then(rs -> {
                ArrayList<String> output = new ArrayList<>();
                while (rs.next()) {
                  output.add("Read from DB: " + rs.getTimestamp("tick"));
                }

                Map<String, Object> attributes = new HashMap<>();
                attributes.put("results", output);
                ctx.render(groovyTemplate(attributes, "db.html"));
              });
            })

            .files(f -> f.dir("public"))
        )
    );
  }
}
@@ -1568,14 +1568,22 @@
     
-return
+ResultSet rs =
  stm
@@ -1620,24 +1620,25 @@
 OM ticks%22);%0A
+%0A
             
@@ -1645,50 +1645,192 @@
     
-%7D%0A              %7D).onError(throwable -%3E %7B%0A
+  ArrayList%3CString%3E output = new ArrayList%3C%3E();%0A                  while (rs.next()) %7B%0A                    output.add(%22Read from DB: %22 + rs.getTimestamp(%22tick%22));%0A                  %7D%0A  
     
@@ -1899,32 +1899,34 @@
                 
+  
 attributes.put(%22
@@ -1929,55 +1929,28 @@
 ut(%22
-message%22, %22There was an error: %22 + throwable
+results%22, output
 );%0A
+%0A
     
@@ -1965,346 +1965,299 @@
     
-ctx.render(groovyTemplate(attributes, %22error.html%22));%0A              %7D).then(rs -%3E %7B%0A                ArrayList%3CString%3E output = new ArrayList%3C%3E();%0A                while (rs.next()) %7B%0A                  output.add(%22Read from DB: %22 + rs.getTimestamp(%22tick%22));%0A                %7D%0A%0A                Map%3CString, Object%3E attributes = new HashMap%3C%3E(
+  return attributes;%0A                %7D%0A              %7D).onError(throwable -%3E %7B%0A                Map%3CString, Object%3E attributes = new HashMap%3C%3E();%0A                attributes.put(%22message%22, %22There was an error: %22 + throwable);%0A                ctx.render(groovyTemplate(attributes, %22error.html%22)
 );%0A 
@@ -2261,34 +2261,40 @@
 ;%0A              
-  
+%7D).then(
 attributes.put(%22
@@ -2291,32 +2291,13 @@
 utes
-.put(%22results%22, output);
+ -%3E %7B
 %0A   

Fix database result set processing