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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* Copyright 2012 Jesse Morgan
*/
package com.p4square.grow.backend;
import java.io.IOException;
import com.codahale.metrics.MetricRegistry;
import com.p4square.grow.provider.*;
import org.apache.log4j.Logger;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;
import org.restlet.resource.Directory;
import org.restlet.routing.Router;
import com.p4square.grow.config.Config;
import com.p4square.grow.model.Message;
import com.p4square.grow.model.MessageThread;
import com.p4square.grow.model.Playlist;
import com.p4square.grow.model.Question;
import com.p4square.grow.model.TrainingRecord;
import com.p4square.grow.model.UserRecord;
import com.p4square.grow.backend.resources.AccountResource;
import com.p4square.grow.backend.resources.BannerResource;
import com.p4square.grow.backend.resources.HealthCheckResource;
import com.p4square.grow.backend.resources.SurveyResource;
import com.p4square.grow.backend.resources.SurveyResultsResource;
import com.p4square.grow.backend.resources.TrainingRecordResource;
import com.p4square.grow.backend.resources.TrainingResource;
import com.p4square.grow.backend.feed.ThreadResource;
import com.p4square.grow.backend.feed.TopicResource;
import com.p4square.restlet.metrics.MetricRouter;
/**
* Main class for the backend application.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class GrowBackend extends Application implements GrowData, ProvidesNotificationService {
private final static Logger LOG = Logger.getLogger(GrowBackend.class);
private final MetricRegistry mMetricRegistry;
private final Config mConfig;
private final GrowData mGrowData;
private final NotificationService mNotificationService;
public GrowBackend() {
this(new Config(), new MetricRegistry());
}
public GrowBackend(Config config, MetricRegistry metricRegistry) {
mConfig = config;
mMetricRegistry = metricRegistry;
mGrowData = new DynamoGrowData(config);
mNotificationService = new SESNotificationService(config);
}
public MetricRegistry getMetrics() {
return mMetricRegistry;
}
@Override
public Restlet createInboundRoot() {
Router router = new MetricRouter(getContext(), mMetricRegistry);
// Account API
router.attach("/accounts/{userId}", AccountResource.class);
// Survey API
router.attach("/assessment/question/{questionId}", SurveyResource.class);
router.attach("/accounts/{userId}/assessment", SurveyResultsResource.class);
router.attach("/accounts/{userId}/assessment/answers/{questionId}",
SurveyResultsResource.class);
// Training API
router.attach("/training/{level}", TrainingResource.class);
router.attach("/training/{level}/videos/{videoId}", TrainingResource.class);
router.attach("/accounts/{userId}/training", TrainingRecordResource.class);
router.attach("/accounts/{userId}/training/videos/{videoId}",
TrainingRecordResource.class);
// Misc.
router.attach("/banner", BannerResource.class);
router.attach("/ping", HealthCheckResource.class);
// Feed
router.attach("/feed/{topic}", TopicResource.class);
router.attach("/feed/{topic}/{thread}", ThreadResource.class);
//router.attach("/feed/{topic/{thread}/{message}", MessageResource.class);
router.attachDefault(new Directory(getContext(), new Reference(getClass().getResource("apiinfo.html"))));
return router;
}
/**
* Open the database.
*/
@Override
public void start() throws Exception {
super.start();
mGrowData.start();
}
/**
* Close the database.
*/
@Override
public void stop() throws Exception {
LOG.info("Shutting down...");
mGrowData.stop();
super.stop();
}
@Override
public Provider<String, UserRecord> getUserRecordProvider() {
return mGrowData.getUserRecordProvider();
}
@Override
public Provider<String, Question> getQuestionProvider() {
return mGrowData.getQuestionProvider();
}
@Override
public CollectionProvider<String, String, String> getVideoProvider() {
return mGrowData.getVideoProvider();
}
@Override
public Provider<String, TrainingRecord> getTrainingRecordProvider() {
return mGrowData.getTrainingRecordProvider();
}
/**
* @return the Default Playlist.
*/
public Playlist getDefaultPlaylist() throws IOException {
return mGrowData.getDefaultPlaylist();
}
@Override
public CollectionProvider<String, String, MessageThread> getThreadProvider() {
return mGrowData.getThreadProvider();
}
@Override
public CollectionProvider<String, String, Message> getMessageProvider() {
return mGrowData.getMessageProvider();
}
@Override
public Provider<String, String> getStringProvider() {
return mGrowData.getStringProvider();
}
@Override
public CollectionProvider<String, String, String> getAnswerProvider() {
return mGrowData.getAnswerProvider();
}
@Override
public NotificationService getNotificationService() { return mNotificationService; }
/**
* Stand-alone main for testing.
*/
public static void main(String[] args) throws Exception {
// Start the HTTP Server
final Component component = new Component();
component.getServers().add(Protocol.HTTP, 9095);
component.getClients().add(Protocol.HTTP);
component.getDefaultHost().attach(new GrowBackend());
// Setup shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
component.stop();
} catch (Exception e) {
LOG.error("Exception during cleanup", e);
}
}
});
LOG.info("Starting server...");
try {
component.start();
} catch (Exception e) {
LOG.fatal("Could not start: " + e.getMessage(), e);
}
}
}
|