/
MServerMain.java
379 lines (325 loc) · 11.8 KB
/
MServerMain.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright © 2022 Mark Raynsford <code@io7m.com> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.io7m.mesquida.internal;
import com.io7m.mesquida.internal.database.MDatabase;
import com.io7m.mesquida.internal.mq.MMessageService;
import com.io7m.mesquida.internal.private_api.MPrivErrorHandler;
import com.io7m.mesquida.internal.private_api.MPrivMessageQueuePutHandler;
import com.io7m.mesquida.internal.private_api.MPrivRootHandler;
import com.io7m.mesquida.internal.private_api.MPrivStreamAddressPutHandler;
import com.io7m.mesquida.internal.private_api.MPrivStreamDeleteHandler;
import com.io7m.mesquida.internal.private_api.MPrivStreamListHandler;
import com.io7m.mesquida.internal.private_api.MPrivStreamPutHandler;
import com.io7m.mesquida.internal.private_api.MPrivStreamStartHandler;
import com.io7m.mesquida.internal.private_api.MPrivStreamStopHandler;
import com.io7m.mesquida.internal.private_api.MPrivUserListHandler;
import com.io7m.mesquida.internal.private_api.MPrivUserPutHandler;
import com.io7m.mesquida.internal.public_api.MPubPages;
import com.io7m.mesquida.internal.public_api.MPubRootHandler;
import com.io7m.mesquida.internal.public_api.MPubServletHolder;
import com.io7m.mesquida.internal.public_api.MPubStaticHandler;
import com.io7m.mesquida.internal.public_api.MPubStreamEditHandler;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.server.session.DefaultSessionCache;
import org.eclipse.jetty.server.session.DefaultSessionIdManager;
import org.eclipse.jetty.server.session.FileSessionDataStore;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Objects;
/**
* The main server.
*/
public final class MServerMain implements AutoCloseable
{
private static final Logger LOG =
LoggerFactory.getLogger(MServerMain.class);
private final Server serverPrivate;
private final MServerConfiguration configuration;
private final Server serverPublic;
private final MMessageService messageQueue;
private MServerMain(
final MServerConfiguration inConfiguration,
final Server inServerPublic,
final Server inServerPrivate,
final MMessageService inMessageQueue)
{
this.configuration =
Objects.requireNonNull(inConfiguration, "configuration");
this.serverPrivate =
Objects.requireNonNull(inServerPrivate, "server");
this.serverPublic =
Objects.requireNonNull(inServerPublic, "server");
this.messageQueue =
Objects.requireNonNull(inMessageQueue, "messageQueue");
}
/**
* Create a server.
*
* @param configuration The server configuration
* @param database The database
*
* @return A server instance
*
* @throws IOException On errors
*/
public static MServerMain create(
final MServerConfiguration configuration,
final MDatabase database)
throws IOException
{
Objects.requireNonNull(configuration, "configuration");
Objects.requireNonNull(database, "database");
final var publicThreadPool =
new QueuedThreadPool(4, 1);
final var privateThreadPool =
new QueuedThreadPool(4, 1);
final var publicServer =
new Server(publicThreadPool);
final var privateServer =
new Server(privateThreadPool);
final var mq =
MMessageService.create(database);
final var pages = new MPubPages(configuration.locale());
final var httpConfig = new HttpConfiguration();
httpConfig.setSendServerVersion(false);
httpConfig.setSendXPoweredBy(false);
createPublicConnectors(configuration, publicServer, httpConfig);
createPublicHandlers(configuration, publicServer, pages, database);
createPrivateHandlers(configuration, privateServer, database, mq);
createPrivateConnectors(configuration, privateServer, httpConfig);
return new MServerMain(
configuration,
publicServer,
privateServer,
mq
);
}
private static void createPrivateHandlers(
final MServerConfiguration configuration,
final Server server,
final MDatabase database,
final MMessageService messageService)
{
final var contextRoot =
new ContextHandler("/");
contextRoot.setHandler(
new MPrivRootHandler());
final var contextUserPut =
new ContextHandler("/user-put");
contextUserPut.setHandler(
new MPrivUserPutHandler(configuration, database));
final var contextUserList =
new ContextHandler("/user-list");
contextUserList.setHandler(
new MPrivUserListHandler(configuration, database));
final var contextStreamPut =
new ContextHandler("/stream-put");
contextStreamPut.setHandler(
new MPrivStreamPutHandler(configuration, database));
final var contextStreamList =
new ContextHandler("/stream-list");
contextStreamList.setHandler(
new MPrivStreamListHandler(configuration, database));
final var contextStreamAddressPut =
new ContextHandler("/stream-address-put");
contextStreamAddressPut.setHandler(
new MPrivStreamAddressPutHandler(configuration, database));
final var contextStreamDelete =
new ContextHandler("/stream-delete");
contextStreamDelete.setHandler(
new MPrivStreamDeleteHandler(configuration, database));
final var contextStreamStart =
new ContextHandler("/stream-start");
contextStreamStart.setHandler(
new MPrivStreamStartHandler(configuration, messageService, database));
final var contextStreamStop =
new ContextHandler("/stream-stop");
contextStreamStop.setHandler(
new MPrivStreamStopHandler(configuration, messageService, database));
final var contextMqPut =
new ContextHandler("/mq-put");
contextMqPut.setHandler(
new MPrivMessageQueuePutHandler(configuration, messageService, database));
final var contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[]{
contextRoot,
contextUserPut,
contextUserList,
contextStreamPut,
contextStreamList,
contextStreamAddressPut,
contextStreamDelete,
contextStreamStart,
contextStreamStop,
contextMqPut,
});
server.setErrorHandler(new MPrivErrorHandler());
server.setHandler(contexts);
}
private static void createPublicHandlers(
final MServerConfiguration configuration,
final Server server,
final MPubPages pages,
final MDatabase database)
{
/*
* Set up a servlet container.
*/
final var servlets = new ServletContextHandler();
servlets.addServlet(
new MPubServletHolder<>(
MPubRootHandler.class, () -> new MPubRootHandler(pages, database)),
"/"
);
servlets.addServlet(
new MPubServletHolder<>(
MPubStaticHandler.class, MPubStaticHandler::new),
"/static/*"
);
servlets.addServlet(
new MPubServletHolder<>(
MPubStreamEditHandler.class,
() -> new MPubStreamEditHandler(pages, database)),
"/stream-edit/"
);
/*
* Set up a session handler that allows for Servlets to have sessions
* that can survive server restarts.
*/
final var sessionIds = new DefaultSessionIdManager(server);
final var sessionHandler = new SessionHandler();
final var sessionStore = new FileSessionDataStore();
sessionStore.setStoreDir(configuration.sessionDirectory().toFile());
final var sessionCache = new DefaultSessionCache(sessionHandler);
sessionCache.setSessionDataStore(sessionStore);
sessionHandler.setSessionCache(sessionCache);
sessionHandler.setSessionIdManager(sessionIds);
sessionHandler.setHandler(servlets);
/*
* Set up an MBean container so that the statistics handler can export
* statistics to JMX.
*/
final var mbeanContainer =
new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbeanContainer);
/*
* Set up a statistics handler that wraps everything.
*/
final var statsHandler = new StatisticsHandler();
statsHandler.setHandler(sessionHandler);
server.setHandler(statsHandler);
}
private static void createPrivateConnectors(
final MServerConfiguration configuration,
final Server server,
final HttpConfiguration httpConfig)
{
final var httpConnectionFactory =
new HttpConnectionFactory(httpConfig);
final var baseConnector =
new ServerConnector(server, httpConnectionFactory);
final var bindAddress =
configuration.privateAddress();
final var bindPort =
configuration.privatePort();
baseConnector.setReuseAddress(true);
baseConnector.setHost(bindAddress.getHostAddress());
baseConnector.setPort(bindPort);
for (final var connector : server.getConnectors()) {
try {
connector.stop();
} catch (final Exception e) {
LOG.error("could not close connector: ", e);
}
}
server.setConnectors(new Connector[]{
baseConnector,
});
}
private static void createPublicConnectors(
final MServerConfiguration configuration,
final Server server,
final HttpConfiguration httpConfig)
{
final var httpConnectionFactory =
new HttpConnectionFactory(httpConfig);
final var baseConnector =
new ServerConnector(server, httpConnectionFactory);
final var bindAddress =
configuration.publicAddress();
final var bindPort =
configuration.publicPort();
baseConnector.setReuseAddress(true);
baseConnector.setHost(bindAddress.getHostAddress());
baseConnector.setPort(bindPort);
for (final var connector : server.getConnectors()) {
try {
connector.stop();
} catch (final Exception e) {
LOG.error("could not close connector: ", e);
}
}
server.setConnectors(new Connector[]{
baseConnector,
});
}
/**
* Start the web server(s).
*
* @throws Exception On errors
*/
public void start()
throws Exception
{
LOG.info(
"private server starting on http://{}:{}",
this.configuration.privateAddress().getHostAddress(),
Integer.valueOf(this.configuration.privatePort())
);
this.serverPrivate.start();
LOG.info(
"public server starting on http://{}:{}",
this.configuration.publicAddress().getHostAddress(),
Integer.valueOf(this.configuration.publicPort())
);
this.serverPublic.start();
}
@Override
public void close()
throws Exception
{
LOG.debug("stopping private server");
this.serverPrivate.stop();
LOG.debug("stopping public server");
this.serverPublic.stop();
LOG.debug("stopping message queue");
this.messageQueue.close();
}
}