-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathServer.java
More file actions
55 lines (43 loc) · 1.41 KB
/
Server.java
File metadata and controls
55 lines (43 loc) · 1.41 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
package io.vertx.example.web.cookie;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.http.Cookie;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.launcher.application.VertxApplication;
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class Server extends VerticleBase {
public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
}
@Override
public Future<?> start() throws Exception {
Router router = Router.router(vertx);
// on every path increment the counter
router.route().handler(ctx -> {
Cookie someCookie = ctx.request().getCookie("visits");
long visits = 0;
if (someCookie != null) {
String cookieValue = someCookie.getValue();
try {
visits = Long.parseLong(cookieValue);
} catch (NumberFormatException e) {
visits = 0;
}
}
// increment the tracking
visits++;
// Add a cookie - this will get written back in the response automatically
ctx.response().addCookie(Cookie.cookie("visits", "" + visits));
ctx.next();
});
// Serve the static resources
router.route().handler(StaticHandler.create("io/vertx/example/web/cookie/webroot"));
return vertx
.createHttpServer()
.requestHandler(router)
.listen(8080);
}
}