WebServer - Redstonecrafter0/RedstoneAPI GitHub Wiki

WebServer

The first thing you should do is block the port of the webserver and use a webserver like nginx or apache2 in front of it und use a SSL vertificate. Next you need to create a templates dir in your spedified baseDir. If you don't have specified a baseDir it will use the current working directory. You max also deliver static content which can be put in a dir called static in your baseDir. This is accessible via the uri /static/ but you may want to use the front server to deliver static content. This WebServer has support for template rendering with Jinja and WebSockets.

Starting

There are three constructors you can use.

WebServer server = new WebServer(); // to create a webserver on port 8080 with the current working directory as the baseDir
WebServer server = new WebServer("", 8080); // In this case the same as above but you may change the host and port.
WebServer server = new WebServer("", 8080, true, WebServer.ErrorHandlerManager.DEFAULT_UNIVERSAL_ERROR_HANDLER, "./"); // In this case the same as above but with even more options like logging, the default error handler and the basedir

webServer.stop(); // to stop the WebServer

To add pages you need to add or remove a RequestHandler

webServer.addHandler(requestHandler);
webServer.removeHandler(requestHandler);
webServer.removeHandlers("path");
webServer.removeHandler("METHOD", "path");

Set an ErrorHandler for handling a singular error like 404 Not Found.

webServer.setErrorHandler(HttpResponseCode.NOT_FOUND, errorHandler);

RequestHandler

For handling incomming requests you need a RequestHandler. Each method represents one ore multiple routes and must return a WebResponse. For one route use @Route and for multiple @Routes. The path in a route must always start with a slash. Which HTTP method to use is defined by the only parameter of the java method. When creating a WebResponse you can always add HttpHeaders at the end of the constructor.

public class MyHandler extends RequestHandler {

  @Route(path = "/")
  public WebResponse home(WebGetRequest request) {
    return new WebResponse("content that could be also byte[]");
  }

  @Routes(routes = {@Route(path = "/1"), @Route(path = "/2")})
  public WebResponse error(WebGetRequest request) {
    return new WebResponse("send 404 Not Found response", HttpResponseCode.NOT_FOUND, new HttpHeader("key", "value"));
  }

  @Route(path = "/redirect")
  public WebResponse echo(WebPostRequest request) {
    return jsonify(request.getContentAsJsonObject()); // its basicly json object echo. it works with JSONArrays too. its possible to use an HttpResponse code and HttpHeaders
  }

  @Route(path = "/redirect")
  public WebResponse redir(WebGetRequest request) {
    return redirect(WebArgument.getByKey(request.getWebArguments(), "url")); //redirect to the url given in the url http:host:port/redirect?url=<urlhere> its possible to use an HttpResponse code and HttpHeaders
  }

  @Route(path = "/download")
  public WebResponse download(WebGetRequest request) {
    return sendFile(new File("path/to/file/to/download")); // its possible to use an HttpResponse code and HttpHeaders
  }

  @Route(path = "/templatetest")
  public WebResponse templateTest(WebGetRequest request) {
    return renderTemplate("path_to_template_in_templates_dir.html.j2"); // here the template will be rendered with jinja. its possible to use an HttpResponse code and HttpHeaders
  }

  @Route(path = "/delete")
  public WebResponse deleteRequest(WebDeleteRequest request) {
    return redirect("/deleted");
  }

  @Route(path = "/put")
  public WebResponse put(WebPutRequest request) {
    return redirect("/done");
  }

  @Route(path = "/patch")
  public WebResponse patch(WebPatchRequest request) {
    return redirect("/patched");
  }

}

ErrorHandler

There are many ways the WebServer can error like when a path is not found are an exception gets thrown but instead of just closing the connection we send an error page with an error code. To set up a custom error page for a single code or as default we use extend the ErrorHandler.

public class MyErrorHandler extends ErrorHandler {

  @Override
  public ErrorResponse handleError(HttpResponseCode code, String url, WebArgument[] args, HttpHeader[] headers) {
    // The code paramater is only useful for using as default error handler
    return new ErrorResponse("this page was not found"); // its possible to send HttpHeaders too
  }

}
⚠️ **GitHub.com Fallback** ⚠️