Skip to content

AdamLuotonen/api-server

 
 

Repository files navigation

@promaster-sdk/api-server

npm version travis build code style: prettier MIT license

Promaster API server reference implementation

Overview

A Promaster API server has two end-points, one that recieves files from promaster-edit (Publish API), and one that serves data to front-end applications (Client API). This repo contain a stand-alone server that implements both end-points, and middlewares that can be embedded as part of your own server.

Publish API

The Publish API is called by promaster-edit to recieve published files and therefore has to be implemented according to the specification documented at the promaster documentation site. A server that implements the Publish API can be built in any language or platform as long as it adheres to this specfication. The Publish API persists the received filed (to disk or other cache) so they can later be served by the Client API. The middleware in this repo contains a reference implementation of the Publish API in typescript/javascript.

Client API

The Client API serves the data from the files recieved via the Publish API to a client application (which will often be a selection software). This package has a reference implementation of the REST API documented at the promaster documentation site in javascript/typescript. However, the shape of a Client API can of course be anything that the client application requires and can be written in any language or platform.

How to install

yarn add @promaster-sdk/api-server

How to use the stand-alone server

node lib/server.js

The server can be configured with environment variables as, see the config schema for all settings..

How to use the middlewares

The implementations in this package are exported as Koa middlewares. If you are using Koa, you can install and use these middlewares in your own server to make it become a Promaster Publish API target and also a Promater Client API server.

Publish API

The example below will mount the Publish API middleware on the /publish endpoint and store published files in the /files folder. The middleware will create the folder if it does not exist:

import * as Koa from "koa";
import * as mount from "koa-mount";
import { createPublishApiMiddleware } from "@promaster-sdk/api-server";

const app = new Koa();
const publishApi = createPublishApiMiddleware(
    () => "/files",
    undefined: string, // prefix?: string - If the middleware is mounted on a subpath
    50, // readFilesInParallel: number - Max parallel read requests
    true, // pruneFiles = true - If files should be deleted when not referenced by any marker files
  (databaseId) => {
    console.log(`Publish for database ${databaseId} complete.`);
    return Promise.resolve();
  }), // onPublishComplete: (databaseId: string) => Promise<void> =  - Register callback for when a database publish has been completed;
app.use(mount("/publish", publishApi));

Client REST API

This repo has a reference implementation of a REST API that can serve the files recieved by the Publish API to front-end clients. How the clients call the API is documented at the promaster documentation site.

NOTE: This is only a reference implementation, you can write your own Client API that suits the need of your application. For example you may need an authentication solution, or want to only serve parts of the data.

The Koa middleware for the Client REST API can be embedded into your existing Koa application like this:

import * as Koa from "koa";
import * as mount from "koa-mount";
import { createClientRestMiddleware } from "@promaster-sdk/api-server";

const app = new Koa();
const clientRestApi = createClientRestMiddleware(
  () => "/files",
  () => "http://myserver/"
);
app.use(mount("/rest", clientRestApi));

Client GraphQL API

This repo has an implementation of a GraphQL API that can serve the files recieved by the Publish API to front-end clients. The schema is determined dynamically from the tables available in hte published files which makes it possible to generate types from the schema that corresponds to the tables.

NOTE: This is only a reference implementation, you can write your own Client API that suits the need of your application. For example you may need an authentication solution, or want to only serve parts of the data.

The Koa middleware for the Client GraphQL API can be embedded into your existing Koa application like this:

import * as Koa from "koa";
import * as mount from "koa-mount";
import { createClientRestMiddleware } from "@promaster-sdk/api-server";

const app = new Koa();
const clientGraphQLApi = createClientGraphQLMiddleware(
  () => "/files",
  () => "http://myserver/"
);
app.use(mount("/graphql", clientGraphQLApi));

How to develop

Clone the repo and run:

cat << EOF > .env
PUBLISH_AUTHORIZATION=mytoken
EOF
yarn start

In promaster-edit, register a new server on port 4500 with an authorization header value of mytoken. Publish once to the this server, then you can try the Client API at http://localhost:4500/rest/v3/markers.

How to publish to local server

If you are developing a new API server and want promaster to publish to it you can expose your local server on the internet using ngrok.

Install ngrok CLI tool and then start it with this commmand:

ngrok http 4500
  • This should give you a temporary public URL that sends traffic to localhost:4500.
  • The URL will be something like http://xxxxxxx.ngrok.io.
  • In promaster-edit under the publish area, create an API server with this URL.
  • You can now publish to this API server and the data will be sent to localhost:4500.

How to publish new version

yarn publish

About

Promaster API server reference implementation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.7%
  • Dockerfile 0.3%