forked from openzipkin/brave-example
/
Backend.java
40 lines (35 loc) · 1.31 KB
/
Backend.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package brave.example;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;
import ratpack.guice.Guice;
import ratpack.handling.Context;
import ratpack.server.RatpackServer;
import ratpack.server.ServerConfig;
import ratpack.zipkin.ServerTracingModule;
public final class Backend {
String printDate(Context ctx) {
String response = new Date().toString();
Optional<String> username = ctx.header("user_name");
if (username.isPresent()) response += " " + username.get();
return response;
}
public static void main(String[] args) throws Exception {
ServerConfig serverConfig = ServerConfig.embedded()
.props(Collections.singletonMap("brave.localServiceName", "backend"))
.sysProps() // allows overrides like ratpack.brave.zipkin.baseUrl=...
.port(9000)
.build();
RatpackServer.start(server -> server.serverConfig(serverConfig)
.registry(Guice.registry(bindings -> bindings
.moduleConfig(ServerTracingModule.class,
serverConfig.get("/brave", BraveConfig.class).toModuleConfig())
.bind(Backend.class)
))
.handlers(chain -> chain
.get("health", ctx -> ctx.render("ok"))
.get("api", ctx -> ctx.render(ctx.get(Backend.class).printDate(ctx)))
)
);
}
}