public void run(WebServer server) throws IOException {
// Track sessions using a cookie strategy and an in-memory session pool
sessionTracker = new CookieSessionTracker(new SessionPool(new SecureIdentifierPolicy(), clock));
server.add(sessionTracker)
.start(new DynamicRoutes() {{
map("/").to(new Application() {
public void handle(Request request, Response response) throws Exception {
Session session = Session.get(request);
String username = session.contains("username") ? session.<String>get("username") : "Guest";
response.body("Hello, " + username);
}
});
post("/login").to(new Application() {
public void handle(Request request, Response response) throws Exception {
String username = request.parameter("username");
Session session = Session.get(request);
session.put("username", username);
response.redirectTo("/");
}
});
delete("/logout").to(new Application() {
public void handle(Request request, Response response) throws Exception {
Session session = Session.get(request);
session.invalidate();
response.redirectTo("/");
}