Code Monkey home page Code Monkey logo

hyperseaport's Introduction

hyperseaport

A p2p Service Registry (๐Ÿ•ณ๐ŸฅŠ a holepunch project)

Seaport

Features

  • ๐Ÿ“‡ Add existing services (apis, dbs, etc) to the registry
  • ๐Ÿ’ป In just a few lines of code, node apis can be registered
  • โš–๏ธ Failover and load balancing between multiple service instances
  • ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง run multiple versions of apis running, giving time to depreciate them
  • ๐Ÿฅก clients request versions of apis with wildcard matching
  • ๐Ÿ“š clients keep local copies of a readable registry for fast lookups and replication between them
  • ๐Ÿ“ฑ an experimental web proxy to expose services to webapps

Devops in a p2p world is much easier. Come aboard! โ›ด ๐Ÿšข

As your micro service architecture grows to span many processes on many machines just register your services and easily find and connect them together.

Install

npm i -g hyperseaport

CLI Usage

1. Generate a unique seed for your registry

Your registry is unique! The seed ensures a unique and consistent public key. Only those who know the public key can connect and uses the services the registry maintains.

$ hyperseaport seed > ~/.config/hyperseaport

If you inspect the file ~/.config/hyperseaport it will look like

registrySeed=c84c7034a0309479299d81468b7bc59592a96b3a919fd1ff159aea1879407382
registryPublicKey=5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45

The registrySeed should be secret! It's what makes your registry your own. The registryPublicKey should be shared so services and consumers can connect and use.

2. Start a registry

$ hyperseaport --web 8777
โฆ– ./cli.js --web 8777
Writer started.
storing in /Users/ryanr/Library/Preferences/hyperseaport
Registry started.
registryPublicKey=5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45
web server listening on port 8777

The registry is stored as a hyperbee in /Users/ryanr/Library/Prefrences/hyperseaport.
We've started with the experimental web proxy on port 8777. We'll show that off later.

3. Register a service (๐Ÿ“‡ Add existing services)

Here is an example that registers and existing couchdb running on localhost port (-p) 5984 as a service. The role (-r) of the service a semver string that represents the name and version of the running instace. We use the registry publicKey from step 2 to find and connect to the registry, and register our service.

The code below can be run on the same different host than step 2, and it automagically connects and registers the service.

$ hyperseaport service --port 5984 --role [email protected] --registryPublicKey 5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45

started p2p service on abe213285052e5c2f2166d144afcd71e31aa5c7d72656d7b956a2c93f76d260f
connecting to registry 5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45
registered service {
  role: 'couchdb',
  version: '3.2.2',
  _tags: {},
  tags: [],
  hash: 'couchdb|3.2.2|'
}

Note: for service registration the server version should be exact

4. Try out the web proxy (๐Ÿ“ฑ - mobile access)

Assuming the service you registered in 3 is an http service, you can now use the registry proxy (if enabled).

Try some urls like

You should be able to GET PUT PATCH POST etc. As you register more service you can access them via

/role@version/path/on/service

This could be a one stop shop for your mobile apps. We will add more features to it (auth, etc) so it is experimental.

If you dont want to start the web proxy with the registry, or you want more redundand web proxies (round robin dns), you can start them up

$ hyperseaport web --port 5984 --registryPublicKey 5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45

5. Register a proxy

This is the part that is like ngrok, or a reverse proxy. You want to USE the service somewhere else, without knowing the IP, or vpn. So you start a hyperseaport proxy on a port that makes it look like the service is running locally on that port.

This can be done on the same or different host then the steps above.

Lookup the service by a role (r) and expose the service locally on a totally different port (p). We use the registry publicKey from step 2 to find and connect to the registry, and find the service.

$ hyperseaport proxy --port 5985 --role [email protected] --registryPublicKey 5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45

connecting to registry 5b64a8956d8f2404c4f4b4e6f402ef439f610f7fe297718093641359130b0d45
connected to registry
found service {
  id: 'abe213285052e5c2f2166d144afcd71e31aa5c7d72656d7b956a2c93f76d260f',
  remotePublicKey: '03008865d93c046129d65b8d68829e5a4757b3b24844bbd1d36d27fe1e683b59',
  meta: {
    role: 'couchdb',
    version: '3.2.2',
    _tags: {},
    tags: [],
    hash: 'couchdb|3.2.2|'
  }
}
proxy from  5985 to p2p service abe213285052e5c2f2166d144afcd71e31aa5c7d72656d7b956a2c93f76d260f

Note: for service discovery, the version can be a semver range.

5. Use the service

Now port 5985 is proxied to the remote service without knowing where it is. Magic. Call it normally

$ curl http://localhost:5985
{"couchdb":"Welcome","version":"3.2.2","git_sha":"d5b746b7c","uuid":"5e3ccc9fd986f473f182ce246c1e214c","features":["access-ready","partitioned","pluggable-storage-engines","reshard","scheduler"],"vendor":{"name":"The Apache Software Foundation"}}

Node Usage

Create a service (๐Ÿ’ป In just a few lines of code, node apis can be registered)

const {Service, KeyPair} = require('hypersearport')

// Boilerplate node
const http = require('http')
const port = 8992
const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World!')
}
const server = http.createServer(requestListener)
server.listen(port)

// The intersting stuff!!
const registryPublicKey = process.argv[2] // we need one thing from the cli
console.log(registryPublicKey)
const role = '[email protected]'
const keyPair = KeyPair()
const service = Service({ registryPublicKey, role, port, keyPair })
await service.setup()

console.log('service connected')

process.once('SIGINT', function () {
  service.destroy()
})

Consume the service from another process

const {Consumer} = require('hypersearport')

const registryPublicKey = process.argv[2] // we need one thing from the cli
const roles = ['[email protected]']
const options = { registryPublicKey }

// notice we could pass in a bunch of roles we need in the array
const consumer = Consumer(roles, options)
const [helloworld] = await consumer.setup()

const url = `http://localhost:${helloworld.port}`
const resp = await fetch(url)
const data = resp.text()
console.log(data) // 'Hello, World!'

// clean up when done
consumer.destroy().then(() => process.exit())


See more runnable examples.

hyperseaport's People

Contributors

ryanramage avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

22388o

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.