Skip to content

Commit

Permalink
chore: update mediator sample
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed Aug 21, 2021
1 parent 364cc49 commit 14e9ab8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 101 deletions.
22 changes: 5 additions & 17 deletions docker/docker-compose-mediators-ngrok.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@ version: '3'
# This file extends docker-compose-mediators.yml

services:
http-mediator:
mediator:
environment:
NGROK_NAME: http-mediator-ngrok
NGROK_NAME: mediator-ngrok
entrypoint: ./scripts/ngrok-wait.sh
depends_on: [http-mediator-ngrok]
depends_on: [mediator-ngrok]

http-mediator-ngrok:
mediator-ngrok:
image: wernight/ngrok
command: ngrok http -bind-tls=true --log stdout http-mediator:3001
networks:
- hyperledger

ws-mediator:
environment:
NGROK_NAME: ws-mediator-ngrok
entrypoint: ./scripts/ngrok-wait.sh
depends_on: [ws-mediator-ngrok]

ws-mediator-ngrok:
image: wernight/ngrok
command: ngrok http -bind-tls=true --log stdout ws-mediator:3002
command: ngrok http -bind-tls=true --log stdout mediator:3001
networks:
- hyperledger

Expand Down
15 changes: 2 additions & 13 deletions docker/docker-compose-mediators.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
version: '3'

services:
http-mediator:
mediator:
build: ..
image: aries-framework-javascript
container_name: afj-http-mediator
container_name: afj-mediator
command: yarn run-mediator
platform: linux/amd64
networks:
- hyperledger
ports:
- 3001:3001

ws-mediator:
build: ..
image: aries-framework-javascript
container_name: afj-ws-mediator
command: yarn run-mediator-ws
platform: linux/amd64
networks:
- hyperledger
ports:
- 3002:3002

networks:
hyperledger:
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"lint": "eslint --ignore-path .gitignore .",
"validate": "yarn lint && yarn check-types && yarn check-format",
"prepare": "husky install",
"run-mediator": "ts-node ./samples/mediator.ts",
"run-mediator-ws": "ts-node ./samples/mediator-ws.ts"
"run-mediator": "ts-node ./samples/mediator.ts"
},
"devDependencies": {
"@types/cors": "^2.8.10",
Expand Down
10 changes: 7 additions & 3 deletions packages/node/src/transport/HttpInboundTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import express, { text } from 'express'
export class HttpInboundTransport implements InboundTransport {
public readonly app: Express
private port: number
private server?: Server
private _server?: Server

public get server() {
return this._server
}

public constructor({ app, port }: { app?: Express; port: number }) {
this.port = port
Expand Down Expand Up @@ -51,11 +55,11 @@ export class HttpInboundTransport implements InboundTransport {
}
})

this.server = this.app.listen(this.port)
this._server = this.app.listen(this.port)
}

public async stop(): Promise<void> {
this.server?.close()
this._server?.close()
}
}

Expand Down
55 changes: 0 additions & 55 deletions samples/mediator-ws.ts

This file was deleted.

71 changes: 60 additions & 11 deletions samples/mediator.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,75 @@
/**
* This file contains a sample mediator. The mediator supports both
* HTTP and WebSockets for communication and will automatically accept
* incoming mediation requests.
*
* You can get an invitation by going to '/invitation', which by default is
* http://localhost:3001/invitation
*
* To connect to the mediator from another agent, you can set the
* 'mediatorConnectionsInvite' parameter in the agent config to the
* url that is returned by the '/invitation/ endpoint. This will connect
* to the mediator, request mediation and set the mediator as default.
*/

import type { InitConfig } from '@aries-framework/core'

import express from 'express'
import { Server } from 'ws'

import { TestLogger } from '../packages/core/tests/logger'

import { HttpOutboundTransport, Agent, ConnectionInvitationMessage, LogLevel, AgentConfig } from '@aries-framework/core'
import { HttpInboundTransport, agentDependencies } from '@aries-framework/node'
import {
HttpOutboundTransport,
Agent,
ConnectionInvitationMessage,
LogLevel,
AgentConfig,
WsOutboundTransport,
} from '@aries-framework/core'
import { HttpInboundTransport, agentDependencies, WsInboundTransport } from '@aries-framework/node'

const port = process.env.AGENT_PORT ? Number(process.env.AGENT_PORT) : 3001

const agentConfig = {
endpoint: process.env.AGENT_ENDPOINT || `http://localhost:${port}`,
// We create our own instance of express here. This is not required
// but allows use to use the same server (and port) for both WebSockets and HTTP
const app = express()
const socketServer = new Server({ noServer: true })

const endpoints = process.env.AGENT_ENDPOINTS?.split(',') ?? [`http://localhost:${port}`, `ws://localhost:${port}`]

const logger = new TestLogger(LogLevel.info)

const agentConfig: InitConfig = {
endpoints,
label: process.env.AGENT_LABEL || 'Aries Framework JavaScript Mediator',
walletConfig: {
id: process.env.WALLET_NAME || 'AriesFrameworkJavaScript',
key: process.env.WALLET_KEY || 'AriesFrameworkJavaScript',
},
publicDidSeed: process.env.PUBLIC_DID_SEED || '000000000000000000HTTPMediator02',
autoAcceptConnections: true,
autoAcceptMediationRequests: true,
logger: new TestLogger(LogLevel.debug),
logger,
}

// Set up agent
const agent = new Agent(agentConfig, agentDependencies)
const config = agent.injectionContainer.resolve(AgentConfig)
const inboundTransport = new HttpInboundTransport({ port })
const outboundTransport = new HttpOutboundTransport()

agent.registerInboundTransport(inboundTransport)
agent.registerOutboundTransport(outboundTransport)
// Create all transports
const httpInboundTransport = new HttpInboundTransport({ app, port })
const httpOutboundTransport = new HttpOutboundTransport()
const wsInboundTransport = new WsInboundTransport({ server: socketServer })
const wsOutboundTransport = new WsOutboundTransport()

// Register all Transports
agent.registerInboundTransport(httpInboundTransport)
agent.registerOutboundTransport(httpOutboundTransport)
agent.registerInboundTransport(wsInboundTransport)
agent.registerOutboundTransport(wsOutboundTransport)

// Allow to create invitation, no other way to ask for invitation yet
inboundTransport.app.get('/invitation', async (req, res) => {
httpInboundTransport.app.get('/invitation', async (req, res) => {
if (typeof req.query.c_i === 'string') {
const invitation = await ConnectionInvitationMessage.fromUrl(req.url)
res.send(invitation.toJSON())
Expand All @@ -42,6 +83,14 @@ inboundTransport.app.get('/invitation', async (req, res) => {

const run = async () => {
await agent.initialize()

// When an 'upgrade' to WS is made on our http server, we forward the
// request to the WS server
httpInboundTransport.server?.on('upgrade', (request, socket, head) => {
socketServer.handleUpgrade(request, socket, head, (socket) => {
socketServer.emit('connection', socket, request)
})
})
}

run()

0 comments on commit 14e9ab8

Please sign in to comment.