|
1 | | -package com.baeldung; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | - |
6 | | -import com.baeldung.filter.RequestValidatorFilter; |
7 | | -import com.baeldung.model.Employee; |
8 | | - |
9 | | -import ratpack.guice.Guice; |
10 | | -import ratpack.hikari.HikariModule; |
11 | | -import ratpack.http.MutableHeaders; |
12 | | -import ratpack.jackson.Jackson; |
13 | | -import ratpack.http.MutableHeaders; |
14 | | -import ratpack.server.RatpackServer; |
15 | | - |
16 | | -public class Application { |
17 | | - |
18 | | - public static void main(String[] args) throws Exception { |
19 | | - |
20 | | - List<Employee> employees = new ArrayList<Employee>(); |
21 | | - employees.add(new Employee(1L, "Mr", "John Doe")); |
22 | | - employees.add(new Employee(2L, "Mr", "White Snow")); |
23 | | - |
24 | | - |
25 | | - RatpackServer.start( |
26 | | - server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> { |
27 | | - config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); |
28 | | - config.addDataSourceProperty("URL", |
29 | | - "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); |
30 | | - }))).handlers(chain -> chain |
31 | | - .all( |
32 | | - // ctx -> { |
33 | | - // MutableHeaders headers = |
34 | | - // ctx.getResponse().getHeaders(); |
35 | | - // headers.set("Access-Control-Allow-Origin","*"); |
36 | | - // headers.set("Accept-Language", "en-us"); |
37 | | - // headers.set("Accept-Charset", "UTF-8"); |
38 | | - // ctx.next(); |
39 | | - // } |
40 | | - new RequestValidatorFilter()) |
41 | | - .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) |
42 | | - .get("data/employees", ctx -> ctx.render(Jackson.json(employees))) |
43 | | - .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!")) |
44 | | - .post(":amount", ctx -> ctx |
45 | | - .render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!")))); |
46 | | - } |
47 | | - |
48 | | -} |
49 | | - |
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import com.baeldung.filter.RequestValidatorFilter; |
| 4 | +import com.baeldung.handler.EmployeeHandler; |
| 5 | +import com.baeldung.handler.RedirectHandler; |
| 6 | +import com.baeldung.model.Employee; |
| 7 | +import com.baeldung.repository.EmployeeRepository; |
| 8 | +import com.baeldung.repository.EmployeeRepositoryImpl; |
| 9 | +import com.zaxxer.hikari.HikariConfig; |
| 10 | +import io.netty.buffer.PooledByteBufAllocator; |
| 11 | +import ratpack.func.Action; |
| 12 | +import ratpack.func.Function; |
| 13 | +import ratpack.guice.BindingsSpec; |
| 14 | +import ratpack.guice.Guice; |
| 15 | +import ratpack.handling.Chain; |
| 16 | +import ratpack.hikari.HikariModule; |
| 17 | +import ratpack.http.client.HttpClient; |
| 18 | +import ratpack.jackson.Jackson; |
| 19 | +import ratpack.registry.Registry; |
| 20 | +import ratpack.server.RatpackServer; |
| 21 | +import ratpack.server.RatpackServerSpec; |
| 22 | +import ratpack.server.ServerConfig; |
| 23 | + |
| 24 | +import java.time.Duration; |
| 25 | +import java.time.temporal.ChronoUnit; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +public class Application { |
| 30 | + |
| 31 | + public static void main(String[] args) throws Exception { |
| 32 | + |
| 33 | + final Action<HikariConfig> hikariConfigAction = hikariConfig -> { |
| 34 | + hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); |
| 35 | + hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); |
| 36 | + }; |
| 37 | + |
| 38 | + final Action<BindingsSpec> bindingsSpecAction = bindings -> bindings.module(HikariModule.class, hikariConfigAction); |
| 39 | + final HttpClient httpClient = HttpClient.of(httpClientSpec -> { |
| 40 | + httpClientSpec.poolSize(10) |
| 41 | + .connectTimeout(Duration.of(60, ChronoUnit.SECONDS)) |
| 42 | + .maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH) |
| 43 | + .responseMaxChunkSize(16384) |
| 44 | + .readTimeout(Duration.of(60, ChronoUnit.SECONDS)) |
| 45 | + .byteBufAllocator(PooledByteBufAllocator.DEFAULT); |
| 46 | + }); |
| 47 | + final Function<Registry, Registry> registryFunction = Guice.registry(bindingsSpecAction); |
| 48 | + |
| 49 | + final Action<Chain> chainAction = chain -> chain.all(new RequestValidatorFilter()) |
| 50 | + .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) |
| 51 | + .get("data/employees", ctx -> ctx.render(Jackson.json(createEmpList()))) |
| 52 | + .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens() |
| 53 | + .get("name") + "!!!")) |
| 54 | + .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens() |
| 55 | + .get("amount") + " added successfully !!!")); |
| 56 | + |
| 57 | + final Action<Chain> routerChainAction = routerChain -> { |
| 58 | + routerChain.path("redirect", new RedirectHandler()) |
| 59 | + .prefix("employee", empChain -> { |
| 60 | + empChain.get(":id", new EmployeeHandler()); |
| 61 | + }); |
| 62 | + }; |
| 63 | + final Action<RatpackServerSpec> ratpackServerSpecAction = serverSpec -> serverSpec.registry(registryFunction) |
| 64 | + .registryOf(registrySpec -> { |
| 65 | + registrySpec.add(EmployeeRepository.class, new EmployeeRepositoryImpl()); |
| 66 | + registrySpec.add(HttpClient.class, httpClient); |
| 67 | + }) |
| 68 | + .handlers(chain -> chain.insert(routerChainAction) |
| 69 | + .insert(chainAction)); |
| 70 | + |
| 71 | + RatpackServer.start(ratpackServerSpecAction); |
| 72 | + } |
| 73 | + |
| 74 | + private static List<Employee> createEmpList() { |
| 75 | + List<Employee> employees = new ArrayList<>(); |
| 76 | + employees.add(new Employee(1L, "Mr", "John Doe")); |
| 77 | + employees.add(new Employee(2L, "Mr", "White Snow")); |
| 78 | + return employees; |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments