dev.mccue.microhttp-html

What is it

microhttp-html provides HtmlResponse, a class which implements IntoResponse and thus can be used alongside microhttp and microhttp-handler to produce responses which contain html.

It automatically adds the appropriate Content-Type header, determines the HTTP reason phrase with reasonphrase, and accepts the Html type provided by html.

Why use it

If you are using microhttp with microhttp-handler, it boxes up the logic needed in order to return html responses. This would otherwise be cumbersome to write at every needed location

Getting Started

At time of writing template processors are a preview-feature, so you will need to use the latest version of the library and the latest JDK.

import dev.mccue.microhttp.handler.RouteHandler;
import dev.mccue.microhttp.html.HtmlResponse;

import java.util.Properties;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import static dev.mccue.html.Html.HTML;

class IndexHandler extends RouteHandler {
    IndexHandler() {
        super("GET", Pattern.compile("/"));
    }

    @Override
    public HtmlResponse handleRoute(
            Matcher matcher,
            Request request
    ) {
        var name = "bob";
        return new HtmlResponse(HTML."""
                <html>
                  <body>
                    <h1> Hello \{name} </h1>
                  </body>
                </html>
                """);
    }
}

<- Index