This repository was archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProcessorApplication.java
More file actions
104 lines (89 loc) · 3.37 KB
/
ProcessorApplication.java
File metadata and controls
104 lines (89 loc) · 3.37 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import demo.domain.Location;
import demo.domain.LocationService;
import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import ratpack.func.Action;
import ratpack.handling.Chain;
import ratpack.handling.Context;
import ratpack.render.Renderer;
import ratpack.render.RendererSupport;
import ratpack.spring.annotation.EnableRatpack;
import reactor.Environment;
import reactor.rx.Streams;
import reactor.rx.broadcast.Broadcaster;
import reactor.spring.context.config.EnableReactor;
import static ratpack.jackson.Jackson.fromJson;
import static ratpack.jackson.Jackson.json;
import static ratpack.websocket.WebSockets.websocketBroadcast;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableConfigurationProperties
@EnableMongoRepositories
@EnableRatpack
@EnableReactor
public class ProcessorApplication {
static {
Environment.initialize();
}
@Bean
public Broadcaster<Location> locationEventStream(Environment env) {
return Broadcaster.create();
}
@Bean
public Action<Chain> handlers(LocationService locations,
ObjectMapper mapper,
ModelMapper beanMapper) {
return (chain) -> {
// Create new Location
chain.post("location", ctx -> {
Location loc = ctx.parse(fromJson(Location.class));
ctx.promise(f -> locations.update(loc)
.consume(f::success))
.then(ctx::render);
});
// Update existing Location
chain.put("location/:id", ctx -> {
String id = ctx.getPathTokens().get("id");
ctx.promise(f -> locations.findOne(id)
.observe(l -> beanMapper.map(l, ctx.parse(fromJson(Location.class))))
.flatMap(locations::update)
.consume(f::success))
.then(ctx::render);
});
// Watch for updates of Locations near us
chain.handler("location/:id/nearby", ctx -> {
String id = ctx.getPathTokens().get("id");
int distance = Integer.valueOf(ctx.getRequest()
.getQueryParams()
.get("distance"));
websocketBroadcast(ctx, locations.nearby(id, distance)
.map(l -> l.toJson(mapper)));
});
};
}
@Bean
public ModelMapper beanMapper() {
return new ModelMapper();
}
@Bean
public Renderer<Location> locationRenderer() {
return new RendererSupport<Location>() {
@Override
public void render(Context ctx, Location loc) throws Exception {
ctx.render(json(loc));
}
};
}
public static void main(String[] args) {
SpringApplication.run(ProcessorApplication.class, args);
}
}