Skip to content

Serve existing http based APIs as fugazi modules

Notifications You must be signed in to change notification settings

fugazi-io/proxify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fugazi proxify

A node package for serving remote APIs as fugazi modules.
It does so by:

  • Serving the module descriptor
  • Proxying all of the requests to the original server and adding CORS headers for the responses

Motivation

The fugazi terminal is restricted by the browser security, and more precisely the Same-origin policy, and so every request made must be served from the same host as the one that fugazi was loaded from.
That can be bypassed using CORS, or by hosting the fugazi proxy-frame, but in some cases changing the configuration of the server which exposes the API isn't easy, or against policies.

By running proxify on the local machine, the entire API is "proxied" with CORS headers and can be loaded into the fugazi terminal.

Installation

npm install @fugazi/proxify

Build

npm run compile
// or
node_modules/typescript/bin/tsc -p scripts

Running

npm start [OPTIONS] descriptor
// OR
node scripts/bin/index.js [OPTIONS] descriptor

If you want to pass arguments then:

npm run start -- --listen-host 3232
// or
node scripts/bin/index.js --listen-host 3232

descriptor

Either a url or a path to a file to a root module descriptor.

Options

  • --listen-host: The host to which proxify will bind to, default is localhost
  • --listen-port: The port to which proxify will bind to, default is 33334

Predefined Modules

Few REST based Descriptors are provided "out of the box" and can be used right away, or serve for reference and basis for more customizations.

The modules are located under modules directory.

To run a module, replace the BASE_URL placeholder for remote.origin, with the actual url of your server, e.g. http://localhost:1234.

Docker Module

A Docker descriptor to interact with the REST API of a Docker engine is provided under modules/docker.json.

npm start [OPTIONS] modules/docker.json

Jenkins Module

A Jenkins descriptor to interact with the REST API of a Jenkins server is provided under modules/jenkins.json.

npm start [OPTIONS] modules/jenkins.json

Example

Let's take an example of a (very) simple http based API for a music database with the following endpoints:

  • GET /labels - returns a list of all record labels

  • GET /artists - returns a list of all artists

  • GET /genres - returns a list of all genres

  • GET /label/{ id } - returns the record label for the given id

  • GET /artist/{ id } - returns the artist for the given id

  • GET /genre/{ id } - returns the genre for the given id

Let's also say that the API is served from http://db.music.com/api.

To use this api from the fugazi client all we need to do is: We create a descriptor file:

{
	"name": "music.db",
	"title": "The music database API",
	"remote": {
		"origin": "http://db.music.com",
		"base": "/api"
	},
	"commands": {
		"allLabels": {
			"returns": "list",
			"syntax": [
				"get all labels",
				"labels"
			],
			"handler": {
				"endpoint": "/labels"
			}
		},
		"allArtists": {
			"returns": "list",
			"syntax": [
				"get all artists",
				"artists"
			],
			"handler": {
				"endpoint": "/artists"
			}
		},
		"allGenres": {
			"returns": "list",
			"syntax": [
				"get all genres",
				"genres"
			],
			"handler": {
				"endpoint": "/genres"
			}
		},
		"getLabel": {
			"returns": "map",
			"syntax": "label (id string)",
			"handler": {
				"endpoint": "/label/{ id }"
			}
		},
		"getArtist": {
			"returns": "map",
			"syntax": "artist (id string)",
			"handler": {
				"endpoint": "/artist/{ id }"
			}
		},
		"getGenre": {
			"returns": "map",
			"syntax": "genre (id string)",
			"handler": {
				"endpoint": "/genre/{ id }"
			}
		}
	}
}

And save it in the directory where we installed the proxify package, let's say ~/fugazi/proxify/music.db.json.
Now we run it:

~/fugazi/proxify> npm start music.db.json

The output should be something like:

server started, listening to localhost:33334
load module descriptor from: http://localhost:33334/music.db.json

Now, inside the fugazi terminal we simply load the module:

load module from "http://localhost:33334/music.db.json"

And then we can just execute the commands:

get all artists
genre 1243tg
etc

Contact

Feel free to create issues if you're running into trouble, and welcome to ask any question in our gitter.