Skip to content

Serving freshly baked Eclair programs over HTTP

License

Notifications You must be signed in to change notification settings

luc-tielen/bakery

Repository files navigation

bakery

Serving freshly baked Eclairs over HTTP.

Bakery is a simple webserver in front of the Eclair compiler that makes it possible to compile a program by submitting the program via an endpoint, and getting the compiled WASM program back as a result.

The compiled WASM can be used directly in combination with eclair-wasm-bindings.

Running the server

A Dockerfile is provided. Run the following commands to clone this repo, build the image and run it:

$ git clone git@github.com:luc-tielen/bakery.git && cd bakery
$ docker build -f Dockerfile . -t bakery
$ docker run -p 8080:8080 --rm -it bakery

Endpoints

  • POST / : expects a JSON body of the form {"program": "..."}. Returns a WASM file on success, otherwise a HTTP error status code is returned with the error.

Usage instructions

You can run bakery yourself using the Docker image in this repository, or you can use a publicly hosted image at https://bakery.fly.dev.

With bakery and the eclair-wasm-bindings package, you can run Eclair code as follows:

import {
  withEclair,
  fact,
  program,
  U32,
  INPUT,
  OUTPUT,
} from 'eclair-wasm-bindings';

const eclairCode = `
@def edge(u32, u32).
@def reachable(u32, u32).

reachable(x, y) :-
  edge(x, y).

reachable(x, z) :-
  edge(x, y),
  reachable(y, z).
`;

const compileEclairCode = (program: string) =>
  fetch('https://bakery.fly.dev', {
    // locally it is http://127.0.0.1:8080
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ program }),
  });

const main = async () => {
  const memory = new WebAssembly.Memory({ initial: 10 });
  const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(
    compileEclairCode(eclairCode),
    { env: { memory } }
  );

  withEclair(wasmInstance, memory, (handle) => {
    const edge = fact('edge', INPUT, [U32, U32]);
    const reachable = fact('reachable', OUTPUT, [U32, U32]);
    const path = program(handle, [edge, reachable]);

    path.edge.addFact([1, 2]);
    path.edge.addFacts([
      [2, 3],
      [3, 4],
    ]);

    path.run();

    const reachableFacts = path.reachable.getFacts();
    console.log(reachableFacts);
  });
};

main();

About

Serving freshly baked Eclair programs over HTTP

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published