The decision server

The decision server is an http4k server that hosts the policies of bundles, as OPA's server does. It has OPA's REST API, so an OPA client works with it.

An application composes the server from modules, and gets only the code and the dependencies of the modules that it uses.

The hosted bundles

HostedBundles holds the bundles of the server and one engine.

reload reads a bundle again. If the new bundle cannot be read or compiled, the server keeps the policy that it has. reloadEvery reloads each bundle on a scheduler of the application.

Servers.kt
// a reload of the same modules is a hit
val rego = Rego.standard(Re2Engine, cache = RegoCache(maxBytes = 64L * 1024 * 1024))
val bundles = HostedBundles(
    rego,
    // /v1/data/...
    { Bundle.read(defaultPath) },
    // /bundles/billing/v1/data/...
    mapOf(BundleName("billing") to { Bundle.read(billingPath) }),
)
// read it again now
bundles.reload(BundleName("billing"))
bundles.reloadEvery(Duration.ofMinutes(1), scheduler) { name, error ->
    log("$name: ${error.message}")
}
Servers.kt, lines 25 to 38, on GitHub

The modules

A DecisionServer has an authentication filter and a list of ServerModules. A module has a name, the routes that it adds to the one http4k contract of the server, and a filter. All the modules use the same HostedBundles.

Module Artifact The routes
OpaApi.module(bundles) rego4k-server-policies OPA's Data API and Query API, /health and GET /bundles.
SignedDecisions.module(bundles, key) rego4k-server-signing /v1/signed/data/...: signed decisions. See Signed decisions and inputs.
DebuggerModule.module(bundles) rego4k-server-debugger The debugger below /debugger. See The debugger.
TreeShaking.module(bundles) rego4k-server-shaking The dry run of tree shaking, as JSON (/v1/shaking) and as a page below /shaking.
ApiDocs.module() rego4k-server-docs A page at /openapi with the OpenAPI descriptions of the server. Put it first.
Servers.kt
val server = DecisionServer(
    authentication,
    listOf(
        // first: it reads the descriptions of the modules after it
        ApiDocs.module(),
        OpaApi.module(bundles),
        // /v1/shaking and the page /shaking
        TreeShaking.module(bundles),
    ),
)
Servers.kt, lines 46 to 55, on GitHub

The authentication filter wraps the routes of each module and GET /openapi.json. The server does not authenticate a request itself. For a signed decision, the filter names the principal of the request.

GET /openapi.json describes the routes of all the modules.

A module of your own

A module of the application is a ServerModule with contract routes. The lenses, the bodies and ApiError of rego4k.server.api give the bodies and the errors of OPA.

A route that throws an ApiError or a RegoException gets the error body of OPA. Build a custom decision server has an example.

In a host application

The routes start at /. Mount serves the server below a path of a host application, for example Mount("/tools/policies/opa", server).route. It removes the prefix from each request.

The modules use no absolute URLs and no global state, so the server works below any path, behind the filters of the host.

On the command line

rego4k run --server runs the server with the flags of opa run --server, and flags of rego4k add the other modules. See The command line.

The README of server lists the external dependencies of each module.

Why http4k and Datastar

We chose http4k and Datastar because they share the aims of rego4k:

http4k-core depends only on the Kotlin standard library and two small libraries of Fork Handles (result4k and values4k). The same collection gives rego4k its parser, parser4k. Datastar is one script of 14 KB when compressed.

Each module of the server has typed contract routes and an OpenAPI description.

The pages are rendered on the server with kotlinx.html, and Datastar patches them in the browser. The browser runs only Datastar's small script, so a new look is a change to Kotlin and CSS:

http.send runs on an http4k HttpHandler. The unit tests for http.send give the policy a handler in memory, and production gives it an HTTP client.