Skip to content

Commit 73e655e

Browse files
committed
The MicroMonolith!!
1 parent 14a1ed2 commit 73e655e

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.stubbornjava.examples.undertow;
2+
3+
import com.stubbornjava.common.undertow.Exchange;
4+
import com.stubbornjava.examples.undertow.contenttypes.ContentTypesServer;
5+
import com.stubbornjava.examples.undertow.rest.RestServer;
6+
import com.stubbornjava.examples.undertow.routing.ConstantStringHandler;
7+
8+
import io.undertow.Undertow;
9+
import io.undertow.server.HttpHandler;
10+
import io.undertow.server.RoutingHandler;
11+
12+
public class MicroMonolith {
13+
14+
public static void main(String[] args) {
15+
16+
// {{start:restServer}}
17+
Undertow.builder()
18+
.addHttpListener(8080, "0.0.0.0", RestServer.ROOT)
19+
.build()
20+
.start();
21+
// {{end:restServer}}
22+
23+
// {{start:contentTypesServer}}
24+
Undertow.builder()
25+
.addHttpListener(8081, "0.0.0.0", ContentTypesServer.ROUTES)
26+
.build()
27+
.start();
28+
// {{end:contentTypesServer}}
29+
30+
// {{start:multiPortServer}}
31+
Undertow.builder()
32+
.addHttpListener(8082, "0.0.0.0", RestServer.ROOT)
33+
.addHttpListener(8083, "0.0.0.0", ContentTypesServer.ROUTES)
34+
.build()
35+
.start();
36+
// {{end:multiPortServer}}
37+
38+
// {{start:combinedServer}}
39+
RoutingHandler combinedHanlder = new RoutingHandler().addAll(RestServer.ROUTES)
40+
.addAll(ContentTypesServer.ROUTES);
41+
Undertow.builder()
42+
.addHttpListener(8084, "0.0.0.0", combinedHanlder)
43+
.build()
44+
.start();
45+
// {{end:combinedServer}}
46+
47+
// {{start:microserviceService}}
48+
Undertow.builder()
49+
.addHttpListener(8085, "0.0.0.0", exchange -> {
50+
Integer port = Exchange.queryParams()
51+
.queryParamAsInteger(exchange, "port")
52+
.orElse(null);
53+
if (null != port) {
54+
try {
55+
HttpHandler handler = new ConstantStringHandler("web server with port " + port);
56+
Undertow.builder()
57+
.addHttpListener(port, "0.0.0.0", handler)
58+
.build()
59+
.start();
60+
} catch (Exception e) {
61+
String message = "error trying to create web sertver with port " + port;
62+
Exchange.body().sendText(exchange, message);
63+
return;
64+
}
65+
Exchange.body().sendText(exchange, "server with port " + port + " created");
66+
return;
67+
}
68+
Exchange.body().sendText(exchange, "port cannot be null");
69+
})
70+
.build()
71+
.start();
72+
// {{end:microserviceService}}
73+
}
74+
}

stubbornjava-examples/src/main/java/com/stubbornjava/examples/undertow/contenttypes/ContentTypesServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.stubbornjava.common.undertow.SimpleServer;
44

5-
import io.undertow.server.HttpHandler;
65
import io.undertow.server.RoutingHandler;
76

87
public class ContentTypesServer {
@@ -17,7 +16,7 @@ public class ContentTypesServer {
1716
* to be a CSI cyber crimes specialist ripping apart some code for a clue to
1817
* whatever it does.
1918
*/
20-
private static final HttpHandler ROUTES = new RoutingHandler()
19+
public static final RoutingHandler ROUTES = new RoutingHandler()
2120
.get("/helloWorldText", ContentTypeHandlers::helloWorldText)
2221
.get("/helloWorldHtml", ContentTypeHandlers::helloWorldHtml)
2322
.get("/helloWorldFileDownload", ContentTypeHandlers::helloWorldFileDownload)

stubbornjava-examples/src/main/java/com/stubbornjava/examples/undertow/rest/RestServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class RestServer {
1717

1818
// {{start:routes}}
19-
private static final HttpHandler ROUTES = new RoutingHandler()
19+
public static final RoutingHandler ROUTES = new RoutingHandler()
2020
.get("/users", timed("listUsers", UserRoutes::listUsers))
2121
.get("/users/{email}", timed("getUser", UserRoutes::getUser))
2222
.get("/users/{email}/exception", timed("getUser", UserRoutes::getUserThrowNotFound))
@@ -38,7 +38,7 @@ public class RestServer {
3838
ROUTES.handleRequest(exchange);
3939
};
4040

41-
private static final HttpHandler ROOT = CustomHandlers.exception(EXCEPTION_THROWER)
41+
public static final HttpHandler ROOT = CustomHandlers.exception(EXCEPTION_THROWER)
4242
.addExceptionHandler(ApiException.class, ApiHandlers::handleApiException)
4343
.addExceptionHandler(Throwable.class, ApiHandlers::serverError)
4444
;

0 commit comments

Comments
 (0)