Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
50 lines (38 loc) · 1.4 KB
/
Copy pathApp.java
File metadata and controls
50 lines (38 loc) · 1.4 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
package com.example;
import static io.github.ghosthack.turismo.Turismo.*;
import java.util.Map;
/**
* A turismo app using the built-in JDK HTTP server (no servlet container).
*
* <p>Run with {@code mvn compile exec:java}, then open
* http://localhost:8080/hello
*/
public class App {
public static void main(String[] args) {
routes();
start(8080);
}
/** Registers the app's routes. Kept separate so tests can reuse it. */
public static void routes() {
get("/hello", "Hello World!");
// Named path parameter: /users/42
get("/users/:id", () -> print("User ", Html.htmlEscape(param("id"))));
// Query parameter: /search?q=turismo
// Always escape request data before writing it into an HTML page.
get("/search", () -> {
type("text/html; charset=utf-8");
print("<p>Your search query was: ", Html.htmlEscape(param("q")), "</p>");
});
// JSON responses need no escaping: the serializer quotes values
get("/api/users/:id", () -> json(Map.of("id", param("id"))));
// POST routes default to 201 Created
post("/users", () -> json(Map.of("created", true)));
// Redirects
get("/old", () -> movedPermanently("/hello"));
get("/go", () -> redirect("/hello"));
notFound(() -> {
status(404);
print("Nothing here");
});
}
}