A simple to use API gateway with failover capabilities.
A simple API gateway with failover capabilities for Home Assistant REST sensors and the likes.
To run this application simply execute the following commands:
git clone https://github.com/shawly/api-scapegoat
cd api-scapegoat
npm install
npm start
docker run -p 3000:3000 -v ./config:/config shawly/api-scapegoat
Note: Standalone docker image will follow soon.
API Scapegoat can be configured either via .json or .yaml files. Even though API Scapegoat uses http-proxy-middleware, not every functionality is implemented or will be implemented. Meaning every feature which is not documented here, is not supported.
Example configurations are located in the config
folder, put your own
configurations there as well and remove the examples.
---
route: /api
target: 'http://192.168.100.233:8080'
pathRewrite:
^/api/hello: /other/api/hello
^/api/todos: /todos
changeOrigin: 'false'
router:
'myclient': 'https://jsonplaceholder.typicode.com'
'192.168.100.200:8080': 'http://google.de'
failover:
response:
status_code: 202
headers:
content-type: application/json
server: API Scapegoat
body: '{ "status": "api offline" }'
This option defines the path under which your API target will be served.
Wildcards are allowed, but not necessary if you want to expose the whole
API of your target. To expose your whole API use /api
without any wildcards.
This option defines the target which will receive the requests, this can be
anything from http://192.168.100.233:8080
for a local host, to any remote
host like https://google.com
or websockets ws://echo.websocket.org
.
This option contains settings for the case where your target is unavailable.
The definition of your fallback response which is returned when your target API is unavailable.
The HTTP status code which is returned in your response. If not set, it will
fall back to 200
(OK).
The HTTP response headers of your response. You should at minimum define the
content-type
of your response, everything else is optional. content-length
will always be set according to the length of the response body.
The body of your response, this can be anything you want as long as you define
it as string. From an empty JSON object {}
to a full HTML body, everything
goes.
Rewrites the origin of the 'host' header to the target URL.
Can be 'true'
or 'false'
.
Here you can define multiple multiple paths that should be rewritten, this is
useful for when you defined a route
which is different from the base path of
your target.
route: /mytvapi
pathRewrite:
^/mytvapi: /api
Which will now forward requests from /mytvapi/*
to /api/*
.
Here you can define different targets depending on the Host
header of the
client which triggered the request.
router:
'myclient': 'https://jsonplaceholder.typicode.com'
'192.168.100.200:8080': 'http://google.de'
If your client, lets say your REST sensor, provides the Host
header with the
value myclient
, all requests to your route
will go to
https://jsonplaceholder.typicode.com
instead. This might be useful for cases
where you want multiple targets for the same API.
This repository keeps a change log using GitHub's releases functionality. The format of the log is based on Keep a Changelog.
Releases are based on Semantic Versioning, and use the format
of MAJOR.MINOR.PATCH
. In a nutshell, the version will be incremented
based on the following:
MAJOR
: Incompatible or major changes.MINOR
: Backwards-compatible new features and enhancements.PATCH
: Backwards-compatible bugfixes and package updates.
Got questions?
You can open an issue here on GitHub.
The original setup of this repository is by shawly.
For a full list of all authors and contributors, check the contributor's page.
MIT License
Copyright (c) 2020 shawly
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.