https://github.com/zenflow/composite-service-http-gateway
Configurable http gateway service for use with composite-service
https://github.com/zenflow/composite-service-http-gateway
Last synced: 12 months ago
JSON representation
Configurable http gateway service for use with composite-service
- Host: GitHub
- URL: https://github.com/zenflow/composite-service-http-gateway
- Owner: zenflow
- Created: 2020-11-07T23:49:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-02T23:18:06.000Z (about 3 years ago)
- Last Synced: 2025-06-09T08:46:03.713Z (about 1 year ago)
- Language: JavaScript
- Size: 581 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# composite-service-http-gateway
Configurable http gateway service for use with [`composite-service`](https://github.com/zenflow/composite-service#readme)
[](http://npmjs.com/package/composite-service-http-gateway)
[](https://github.com/zenflow/composite-service-http-gateway/actions?query=branch%3Amaster)
[](https://david-dm.org/zenflow/composite-service-http-gateway)
[](https://codeclimate.com/github/zenflow/composite-service-http-gateway)
[](https://snyk.io/test/github/zenflow/composite-service-http-gateway?targetFile=package.json)
[](https://opensource.org/licenses/MIT)
## Install
```
$ npm install composite-service composite-service-http-gateway
```
## Usage
Call `configureHttpGateway` with a [`HttpGatewayConfig`](./src/HttpGatewayConfig.ts) object
to get a `ServiceConfig` object you can use in your `CompositeServiceConfig`.
```js
const { startCompositeService } = require("composite-service");
const { configureHttpGateway } = require("composite-service-http-gateway");
const apiPort = process.env.API_PORT || 8000;
const webPort = process.env.WEB_PORT || 8001;
const port = process.env.PORT || 3000;
startCompositeService({
services: {
api: {
cwd: `${__dirname}/api`,
command: "node server.js",
env: { PORT: apiPort },
ready: ctx => ctx.onceTcpPortUsed(apiPort),
},
web: {
cwd: `${__dirname}/web`,
command: `next start --port ${webPort}`,
ready: ctx => ctx.onceTcpPortUsed(webPort),
},
gateway: configureHttpGateway({
dependencies: ["api", "web"],
port,
routes: {
"/admin": { static: { root: `${__dirname}/admin/build` } },
"/api": { proxy: { target: `http://localhost:${apiPort}` } },
"/": { proxy: { target: `http://localhost:${webPort}` } },
},
}),
},
});
```
The [`HttpGatewayConfig`](./src/HttpGatewayConfig.ts) object defines
a `port`, a `host` (optional), a collection of `routes`, and an `onReady` hook (optional).
Any additional properties will be included in the returned `ServiceConfig` (except `command` or `ready`).
### Routes
The central property of `HttpGatewayConfig` is `routes`.
In this object, each key is an absolute URL path,
and each value is configuration of how to handle requests to that path and all it's sub-paths.
*Note that the order of entries is significant because
requests will be handled by the *first* matching route,
hence putting the `/` route last in the example above.*
### Handlers
There are currently two "handlers", or ways requests can be handled:
| # | identifier | middleware | description
| --- | --- | --- | ---
| 1 | `proxy` | [`http-proxy-middleware`](https://github.com/chimurai/http-proxy-middleware#readme) | Proxy to another http service, typically a sibling composed service
| 2 | `static` | [`serve-static`](https://github.com/expressjs/serve-static#readme) | Serve static files from the filesystem
Handler config objects are mostly just passed along to the underlying middleware,
so *please refer to the above-linked middleware documentation for details on configuration options*.
That said, the following handler config defaults are updated for convenience:
| handler | config | original default | updated default | reason |
| --- | --- | --- | --- | ---
| `proxy` | `logLevel` | `"info"` | `"warn"` | reduce noise
| `proxy` | `ws` | `false` | `true` | support WebSocket connections out-of-the-box
### Using functions in handler configuration
Non-json-serializable values like functions and regular expressions can be used in handler configuration
thanks to [`serialize-javascript`](https://github.com/yahoo/serialize-javascript).
This however comes with caveats:
1. Functions must be pure (i.e. must not refer to variables outside it's definition)
2. `require()`s inside a function will be resolved from`node_modules/composite-service-http-gateway/server/server.js` (3 levels)