|
| 1 | +package com.stubbornjava.webapp; |
| 2 | + |
| 3 | +import static com.stubbornjava.common.undertow.handlers.CustomHandlers.timed; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import com.stubbornjava.HelperRoutes; |
| 9 | +import com.stubbornjava.PageRoutes; |
| 10 | +import com.stubbornjava.common.undertow.SimpleServer; |
| 11 | +import com.stubbornjava.common.undertow.handlers.CustomHandlers; |
| 12 | +import com.stubbornjava.guide.GuideRoutes; |
| 13 | +import com.stubbornjava.post.JavaLibRoutes; |
| 14 | +import com.stubbornjava.post.PostRoutes; |
| 15 | +import com.stubbornjava.undertow.handlers.MiddlewareBuilder; |
| 16 | +import com.stubbornjava.webapp.themes.ThemeRoutes; |
| 17 | + |
| 18 | +import io.undertow.server.HttpHandler; |
| 19 | +import io.undertow.server.RoutingHandler; |
| 20 | +import io.undertow.server.handlers.BlockingHandler; |
| 21 | +import io.undertow.server.handlers.PathHandler; |
| 22 | + |
| 23 | +public class StubbornJavaWebApp { |
| 24 | + private static final Logger logger = LoggerFactory.getLogger(StubbornJavaWebApp.class); |
| 25 | + |
| 26 | + private static HttpHandler exceptionHandler(HttpHandler next) { |
| 27 | + return CustomHandlers.exception(next) |
| 28 | +// .addExceptionHandler(ResponseException.NotFound.class, HelperRoutes::notFound) |
| 29 | + .addExceptionHandler(Throwable.class, PageRoutes::error); |
| 30 | + } |
| 31 | + |
| 32 | + private static HttpHandler wrapWithMiddleware(HttpHandler handler) { |
| 33 | + return MiddlewareBuilder.begin(PageRoutes::redirector) |
| 34 | + .next(BlockingHandler::new) |
| 35 | + .next(CustomHandlers::gzip) |
| 36 | + .next(ex -> CustomHandlers.accessLog(ex, logger)) |
| 37 | + .next(CustomHandlers::statusCodeMetrics) |
| 38 | + .next(StubbornJavaWebApp::exceptionHandler) |
| 39 | + .complete(handler); |
| 40 | + } |
| 41 | + |
| 42 | + // These routes do not require any authentication |
| 43 | + private static final HttpHandler basicRoutes = new RoutingHandler() |
| 44 | + .get("/", timed("getHome", PageRoutes::home)) |
| 45 | + .get("/ping", timed("ping", PageRoutes::ping)) |
| 46 | + |
| 47 | + .get("/favicon.ico", timed("favicon", CustomHandlers.resource("images/"))) |
| 48 | + .get("robots.txt", timed("robots", PageRoutes::robots)) |
| 49 | + |
| 50 | + .get("/posts/{slug}", timed("getPost", PostRoutes::getPost)) |
| 51 | + |
| 52 | + .get("/tags/{tag}/posts", timed("getRecentPostsWithTag", PostRoutes::recentPostsWithTag)) |
| 53 | + |
| 54 | + .get("/java-libraries", timed("getJavaLibraries", JavaLibRoutes::listLibraries)) |
| 55 | + .get("/java-libraries/{library}", timed("getLibrary", JavaLibRoutes::getLibrary)) |
| 56 | + .get("/libraries/{library}/posts", timed("getLibraryRedirect", JavaLibRoutes::getLibraryRedirect)) |
| 57 | + |
| 58 | + .get("/guides/{slug}", timed("getGuide", GuideRoutes::getGuide)) |
| 59 | + |
| 60 | + .get("/best-selling-html-css-themes-and-website-templates", timed("popularThemes", ThemeRoutes::popularThemes)) |
| 61 | + |
| 62 | + .get("/dev/metrics", timed("getMetrics", HelperRoutes::getMetrics)) |
| 63 | + |
| 64 | + .setFallbackHandler(timed("notFound", PageRoutes::notFound)) |
| 65 | + ; |
| 66 | + |
| 67 | + private static final HttpHandler staticRoutes = new PathHandler(basicRoutes) |
| 68 | + .addPrefixPath("/css", timed("getCss", CustomHandlers.resource("css/"))) |
| 69 | + .addPrefixPath("/js", timed("getJs", CustomHandlers.resource("js/"))) |
| 70 | + .addPrefixPath("/assets/fonts", timed("getJs", CustomHandlers.resource("fonts/"))) |
| 71 | + .addPrefixPath("/images", timed("getImages", CustomHandlers.resource("images/"))) |
| 72 | + ; |
| 73 | + |
| 74 | + private static void startServer() { |
| 75 | + SimpleServer server = SimpleServer.simpleServer(wrapWithMiddleware(staticRoutes)); |
| 76 | + server.start(); |
| 77 | + } |
| 78 | + |
| 79 | + public static void main(String[] args) { |
| 80 | + startServer(); |
| 81 | + } |
| 82 | +} |
0 commit comments