Skip to content

HTTP Module

PiggyPiglet edited this page Oct 18, 2019 · 4 revisions

The HTTP addon can be used as a fully fledged website, though I wouldn't recommend it. It's more catered towards supplying api's over HTTP. You can either return your own objects (which will be converted via .toString()), or use JsonRoute which uses gson to automatically convert your objects to a json string.

Routes

Like commands, and most other components in RPF, Routes are simple to make an require no manual registration. When creating a route, there's two things you need to set, first the route itself, then the provide method.

public final class TestRoute extends Route {
    public TestRoute() {
        super("test");
    }

    @Override
    protected Object provide(Map<String, List<String>> params) {
        return "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Test</h1></body></html>";
    }
}

This route could then be accessed via http(s)://ip:port/test

JSON Routes

The format for a json route is the exact same as a Route, but instead of simply converting the object to a string, the route backend will utilise the gson library to convert your object into a JSON string (with pretty printing).

Configuration

The HTTP addon will automatically generate a http.json config, and an index.html file. Configure your port/ip in http.json, and your default html content in index.html. Optionally, you can also set an authorization token, which will be used on any routes annotated with @Authenticated. You can also configure an ssl certificate (path should point to a java keystore).

Clone this wiki locally