Skip to content

Commit 2d3564d

Browse files
committed
[>] Serve results
1 parent 98169ca commit 2d3564d

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

README.md

+9-30
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,14 @@ A repository for my live-coding talk [Modern Java in Action](https://nipafx.dev/
44

55
## Next
66

7-
This is where string templates used to come in.
8-
Since [they're out](https://bugs.openjdk.org/browse/JDK-8329948) in JDK 23, there's nothing to do here except boring string formatting and method calls:
9-
* change output in `GitHubCrawl::main` to:
7+
Simple server:
8+
* `ResultServer::launchWebServer`:
109
```java
11-
System.out.printf("""
12-
13-
---
14-
15-
%s
16-
%s
17-
18-
19-
""", Statistician.evaluate(rootPage), Pretty.pageList(rootPage));
20-
```
21-
* parse to HTML in `ResultServer::serve`:
22-
```java
23-
var html = Jsoup.parse("""...""");
24-
```
25-
and update call to `Files.writeString` on next line
26-
* add info in `ResultServer::pageHtml`:
27-
```java
28-
return join(RAW."""
29-
<div class="page level-%d">
30-
<a href="%s">%s</a>
31-
%s
32-
</div>
33-
""".formatted(
34-
level,
35-
page.url().toString(),
36-
Pretty.pageName(page),
37-
reference ? "<span class=\"ref\"></span>" : "");
10+
System.out.println("Visit localhost:8080");
11+
SimpleFileServer
12+
.createFileServer(
13+
new InetSocketAddress(8080),
14+
serverDir.toAbsolutePath(),
15+
OutputLevel.INFO)
16+
.start();
3817
```

src/main/java/dev/nipafx/demo/modern/GitHubCrawl.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ public static void main(String[] args) throws Exception {
2626
var factory = new PageTreeFactory(client);
2727
var rootPage = factory.createPage(config.seedUrl(), config.depth());
2828

29-
// TODO: improve formatting
30-
System.out.println(Statistician.evaluate(rootPage));
31-
System.out.println(Pretty.pageList(rootPage));
29+
System.out.printf("""
30+
31+
---
32+
33+
%s
34+
35+
%s
36+
37+
38+
""", Statistician.evaluate(rootPage), Pretty.pageList(rootPage));
3239

3340
ResultServer.serve(rootPage, Path.of("serve"));
3441
}

src/main/java/dev/nipafx/demo/modern/operations/ResultServer.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package dev.nipafx.demo.modern.operations;
22

3+
import com.sun.net.httpserver.SimpleFileServer;
4+
import com.sun.net.httpserver.SimpleFileServer.OutputLevel;
35
import dev.nipafx.demo.modern.page.GitHubPage;
46
import dev.nipafx.demo.modern.page.Page;
7+
import org.jsoup.Jsoup;
58

69
import java.io.IOException;
10+
import java.net.InetSocketAddress;
711
import java.nio.file.Files;
812
import java.nio.file.Path;
913
import java.util.HashSet;
@@ -17,8 +21,7 @@ public static void serve(Page rootPage, Path serverDir) throws IOException {
1721
if (!Files.exists(serverDir))
1822
Files.createDirectory(serverDir);
1923

20-
// TODO: parse to HTML document
21-
var html = """
24+
var html = Jsoup.parse("""
2225
<!DOCTYPE html>
2326
<html lang="en">
2427
<head>
@@ -32,8 +35,15 @@ public static void serve(Page rootPage, Path serverDir) throws IOException {
3235
</div>
3336
</body>
3437
</html>
35-
""".formatted(Pretty.pageName(rootPage), pageTreeHtml(rootPage));
36-
Files.writeString(serverDir.resolve("index.html"), html);
38+
""".formatted(Pretty.pageName(rootPage), pageTreeHtml(rootPage)));
39+
Files.writeString(serverDir.resolve("index.html"), html.html());
40+
41+
launchWebServer(serverDir);
42+
}
43+
44+
private static void launchWebServer(Path serverDir) {
45+
System.out.println("Visit localhost:8080");
46+
// TODO: launch web server
3747
}
3848

3949
private static String pageTreeHtml(Page rootPage) {
@@ -62,12 +72,16 @@ private static String appendPageTreeHtml(Set<Page> printedPages, Page page, int
6272
}
6373

6474
private static String pageHtml(Page page, boolean reference, int level) {
65-
// TODO: add page info
6675
return """
67-
<div class="page level-0">
68-
<a href=""></a>
76+
<div class="page level-%d">
77+
<a href="%s">%s</a>
78+
%s
6979
</div>
70-
""";
80+
""".formatted(
81+
level,
82+
page.url().toString(),
83+
Pretty.pageName(page),
84+
reference ? "<span class=\"ref\"></span>" : "");
7185
}
7286

7387
}

0 commit comments

Comments
 (0)