-
Notifications
You must be signed in to change notification settings - Fork 13
/
ServerTodoMVC.java
73 lines (61 loc) · 2.62 KB
/
ServerTodoMVC.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
package live.connector.vertxui.samples.server.todomvc;
import java.lang.invoke.MethodHandles;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Context;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import live.connector.vertxui.client.FigWheelyClient;
import live.connector.vertxui.samples.client.todomvc.View;
import live.connector.vertxui.samples.server.AllExamplesServer;
import live.connector.vertxui.server.FigWheelyServer;
import live.connector.vertxui.server.VertxUI;
public class ServerTodoMVC extends AbstractVerticle {
private final static Logger log = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
public static void main(String[] args) {
Vertx.vertx().deployVerticle(MethodHandles.lookup().lookupClass().getName());
}
@Override
public void start() {
boolean debug = true;
// This line usually compiles java to javascript, generates a index.html
// and serves the index.html and the javascript. However, this
// application is also an example for using an existing index.html.
// The only difference is at startup time: call VertxUI.with()
// with url=null (2nd parameter) so it only compiles, give false
// as last parameter so that there is no index.html generated (not
// necessary), and then server folder /a/ with the javascript yourself.
VertxUI.with(View.class, null, debug, false);
Router router = Router.router(vertx);
router.get("/a/*").handler(StaticHandler.create(VertxUI.getTargetFolder(debug) + "/a"));
// serve the already existing index.html and .css files.
router.get("/*").handler(FigWheelyServer.staticHandler("assets/todos", "/"));
// if debugging, figwheely can notify the browser of code changes.
if (debug) {
router.get(FigWheelyClient.urlJavascript).handler(FigWheelyServer.create());
}
// Make sure that when we exit, we close vertxes too.
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
Context context = Vertx.currentContext();
if (context == null) {
return;
}
Vertx vertx = context.owner();
vertx.deploymentIDs().forEach(vertx::undeploy);
vertx.close();
}
});
// Create and start the server
vertx.createHttpServer(new HttpServerOptions().setCompressionSupported(true)).requestHandler(router)
.listen(AllExamplesServer.port, listenHandler -> {
if (listenHandler.failed()) {
log.log(Level.SEVERE, "Startup error", listenHandler.cause());
System.exit(0);// stop on startup error
}
});
}
}