-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceMain.java
More file actions
123 lines (105 loc) · 4.89 KB
/
Copy pathServiceMain.java
File metadata and controls
123 lines (105 loc) · 4.89 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.codeforcommunity;
import com.codeforcommunity.api.IAnnouncementsProcessor;
import com.codeforcommunity.api.IAuthProcessor;
import com.codeforcommunity.api.ICheckoutProcessor;
import com.codeforcommunity.api.IEventsProcessor;
import com.codeforcommunity.api.IProtectedUserProcessor;
import com.codeforcommunity.api.IPublicAnnouncementsProcessor;
import com.codeforcommunity.api.IPublicEventsProcessor;
import com.codeforcommunity.api.IRequestsProcessor;
import com.codeforcommunity.auth.JWTAuthorizer;
import com.codeforcommunity.auth.JWTCreator;
import com.codeforcommunity.auth.JWTHandler;
import com.codeforcommunity.logger.SLogger;
import com.codeforcommunity.processor.AnnouncementsProcessorImpl;
import com.codeforcommunity.processor.AuthProcessorImpl;
import com.codeforcommunity.processor.CheckoutProcessorImpl;
import com.codeforcommunity.processor.EventsProcessorImpl;
import com.codeforcommunity.processor.ProtectedUserProcessorImpl;
import com.codeforcommunity.processor.PublicAnnouncementsProcessorImpl;
import com.codeforcommunity.processor.PublicEventsProcessorImpl;
import com.codeforcommunity.processor.RequestsProcessorImpl;
import com.codeforcommunity.propertiesLoader.PropertiesLoader;
import com.codeforcommunity.requester.Emailer;
import com.codeforcommunity.rest.ApiRouter;
import io.vertx.core.Vertx;
import java.util.Properties;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
public class ServiceMain {
private DSLContext db;
public static void main(String[] args) {
try {
ServiceMain serviceMain = new ServiceMain();
serviceMain.initialize();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Start the server, get everything going. */
public void initialize() throws ClassNotFoundException {
updateSystemProperties();
createDatabaseConnection();
initializeServer();
}
/** Adds any necessary system properties. */
private void updateSystemProperties() {
String propertyKey = "vertx.logger-delegate-factory-class-name";
String propertyValue = "io.vertx.core.logging.SLF4JLogDelegateFactory";
Properties systemProperties = System.getProperties();
systemProperties.setProperty(propertyKey, propertyValue);
System.setProperties(systemProperties);
}
/** Connect to the database and create a DSLContext so jOOQ can interact with it. */
private void createDatabaseConnection() throws ClassNotFoundException {
// Load configuration from db.properties file
String databaseDriver = PropertiesLoader.loadProperty("database_driver");
String databaseUrl = PropertiesLoader.loadProperty("database_url");
String databaseUsername = PropertiesLoader.loadProperty("database_username");
String databasePassword = PropertiesLoader.loadProperty("database_password");
// This throws an exception of the database driver is not on the classpath
Class.forName(databaseDriver);
// Create a DSLContext from the above configuration
this.db = DSL.using(databaseUrl, databaseUsername, databasePassword);
}
/** Initialize the server and get all the supporting classes going. */
private void initializeServer() {
JWTHandler jwtHandler = new JWTHandler(PropertiesLoader.loadProperty("jwt_secret_key"));
JWTAuthorizer jwtAuthorizer = new JWTAuthorizer(jwtHandler);
JWTCreator jwtCreator = new JWTCreator(jwtHandler);
String productName = "LLB";
Vertx vertx = Vertx.vertx();
SLogger.initializeLogger(vertx, productName);
// Log uncaught exceptions to Slack
vertx.exceptionHandler(SLogger::logApplicationError);
Emailer emailer = new Emailer(this.db);
IAuthProcessor authProcessor = new AuthProcessorImpl(this.db, emailer, jwtCreator);
IProtectedUserProcessor protectedUserProcessor =
new ProtectedUserProcessorImpl(this.db, emailer);
IRequestsProcessor requestsProcessor = new RequestsProcessorImpl(this.db, emailer);
IEventsProcessor eventsProcessor = new EventsProcessorImpl(this.db);
IPublicEventsProcessor publicEventsProcessor = new PublicEventsProcessorImpl(this.db);
IAnnouncementsProcessor announcementProcessor =
new AnnouncementsProcessorImpl(this.db, emailer);
IPublicAnnouncementsProcessor publicAnnouncementsProcessor =
new PublicAnnouncementsProcessorImpl(this.db, emailer);
ICheckoutProcessor checkoutProcessor = new CheckoutProcessorImpl(this.db, emailer);
ApiRouter router =
new ApiRouter(
authProcessor,
protectedUserProcessor,
requestsProcessor,
eventsProcessor,
publicEventsProcessor,
announcementProcessor,
publicAnnouncementsProcessor,
checkoutProcessor,
jwtAuthorizer);
startApiServer(router, vertx);
}
/** Start up the actual API server that will listen for requests. */
private void startApiServer(ApiRouter router, Vertx vertx) {
ApiMain apiMain = new ApiMain(router);
apiMain.startApi(vertx);
}
}