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
|
/*
* Copyright 2012 Jesse Morgan
*/
package com.p4square.grow.backend;
import org.apache.log4j.Logger;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.Restlet;
import org.restlet.routing.Router;
import com.p4square.grow.config.Config;
import com.p4square.grow.backend.db.CassandraDatabase;
import com.p4square.grow.backend.resources.AccountResource;
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;
/**
* Main class for the backend application.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class GrowBackend extends Application {
private final static Logger LOG = Logger.getLogger(GrowBackend.class);
private final Config mConfig;
private final CassandraDatabase mDatabase;
public GrowBackend() {
this(new Config());
}
public GrowBackend(Config config) {
mConfig = config;
mDatabase = new CassandraDatabase();
}
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
// 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);
return router;
}
/**
* Open the database.
*/
@Override
public void start() throws Exception {
super.start();
// Setup database
mDatabase.setClusterName(mConfig.getString("clusterName", "Dev Cluster"));
mDatabase.setKeyspaceName(mConfig.getString("keyspace", "GROW"));
mDatabase.init();
}
/**
* Close the database.
*/
@Override
public void stop() throws Exception {
LOG.info("Shutting down...");
mDatabase.close();
super.stop();
}
/**
* @return the current database.
*/
public CassandraDatabase getDatabase() {
return mDatabase;
}
/**
* 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);
}
}
}
|