|
| 1 | +package com.stubbornjava.examples.undertow.handlebars; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import com.google.common.base.Strings; |
| 7 | +import com.stubbornjava.common.SimpleResponse; |
| 8 | +import com.stubbornjava.common.undertow.Exchange; |
| 9 | +import com.stubbornjava.common.undertow.SimpleServer; |
| 10 | +import com.stubbornjava.common.undertow.handlers.CustomHandlers; |
| 11 | +import com.stubbornjava.undertow.handlers.MiddlewareBuilder; |
| 12 | + |
| 13 | +import io.undertow.server.HttpHandler; |
| 14 | +import io.undertow.server.HttpServerExchange; |
| 15 | +import io.undertow.server.RoutingHandler; |
| 16 | +import io.undertow.server.handlers.BlockingHandler; |
| 17 | + |
| 18 | +public class WebpackServer { |
| 19 | + private static final Logger log = LoggerFactory.getLogger(WebpackServer.class); |
| 20 | + |
| 21 | + public static void notFound(HttpServerExchange exchange) { |
| 22 | + exchange.setStatusCode(404); |
| 23 | + Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/notfound", SimpleResponse.create()); |
| 24 | + } |
| 25 | + |
| 26 | + public static void error(HttpServerExchange exchange) { |
| 27 | + exchange.setStatusCode(500); |
| 28 | + Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/serverError", SimpleResponse.create()); |
| 29 | + } |
| 30 | + |
| 31 | + // Render homepage |
| 32 | + public static void home(HttpServerExchange exchange) { |
| 33 | + exception(exchange); |
| 34 | + Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/home", SimpleResponse.create()); |
| 35 | + } |
| 36 | + |
| 37 | + // Render hello {name} page. |
| 38 | + public static void hello(HttpServerExchange exchange) { |
| 39 | + exception(exchange); |
| 40 | + String name = Exchange.queryParams() |
| 41 | + .queryParam(exchange, "name") |
| 42 | + .filter(s -> !Strings.isNullOrEmpty(s)) |
| 43 | + .orElse("world"); |
| 44 | + SimpleResponse response = SimpleResponse.create() |
| 45 | + .with("name", name); |
| 46 | + Exchange.body().sendHtmlTemplate(exchange, "static/templates/src/hello", response); |
| 47 | + } |
| 48 | + |
| 49 | + private static void exception(HttpServerExchange exchange) { |
| 50 | + if (Exchange.queryParams().queryParamAsBoolean(exchange, "exception").orElse(false)) { |
| 51 | + throw new RuntimeException("Poorly Named Exception!!!"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // {{start:server}} |
| 56 | + private static HttpHandler exceptionHandler(HttpHandler next) { |
| 57 | + return CustomHandlers.exception(next) |
| 58 | + .addExceptionHandler(Throwable.class, WebpackServer::error); |
| 59 | + } |
| 60 | + |
| 61 | + private static HttpHandler wrapWithMiddleware(HttpHandler handler) { |
| 62 | + return MiddlewareBuilder.begin(BlockingHandler::new) |
| 63 | + .next(CustomHandlers::gzip) |
| 64 | + .next(ex -> CustomHandlers.accessLog(ex, log)) |
| 65 | + .next(CustomHandlers::statusCodeMetrics) |
| 66 | + .next(WebpackServer::exceptionHandler) |
| 67 | + .complete(handler); |
| 68 | + } |
| 69 | + |
| 70 | + private static final HttpHandler ROUTES = new RoutingHandler() |
| 71 | + .get("/", WebpackServer::home) |
| 72 | + .get("/hello", WebpackServer::hello) |
| 73 | + .get("/static*", CustomHandlers.resource("")) |
| 74 | + .setFallbackHandler(WebpackServer::notFound) |
| 75 | + ; |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + SimpleServer server = SimpleServer.simpleServer(wrapWithMiddleware(ROUTES)); |
| 79 | + server.start(); |
| 80 | + } |
| 81 | + // {{end:server}} |
| 82 | +} |
0 commit comments