Skip to content

Commit

Permalink
remove env file, add deploy script to publish docker containers
Browse files Browse the repository at this point in the history
  • Loading branch information
tubleronchik committed May 20, 2024
1 parent 67f8792 commit d2e326d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 33 deletions.
3 changes: 0 additions & 3 deletions .env.template

This file was deleted.

44 changes: 44 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: "Deploy"

env:
ADDON_CONTAINER_NAME: addon-libp2p-ws-proxy
CONTAINER_NAME: libp2p-ws-proxy
BUILD_FROM_PROXY: node:20-alpine

on:
push:
branches:
- dev

jobs:
publish:
name: Publish
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Login to ghcr.io
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}
- name: Publish Addon Proxy
uses: home-assistant/builder@master
with:
args: |
--all \
--target ${{ env.ADDON_CONTAINER_NAME }} \
--docker-hub ghcr.io/pinoutltd \
--image ${{ env.ADDON_NAME }}-{arch}
- name: Build and Push Proxy Image
run: |
docker build --build-arg BUILD_FROM=${{ env.BUILD_FROM_PROXY }} --tag ghcr.io/pinoutltd/${{ env.CONTAINER_NAME }}:latest .
docker push ghcr.io/pinoutltd/${{ env.CONTAINER_NAME }}:latest
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM node:18-alpine AS dependencies
ARG BUILD_FROM
FROM $BUILD_FROM
WORKDIR /proxy
RUN chown -R node:node /proxy
RUN chmod -R 777 /proxy
Expand All @@ -7,8 +8,6 @@ COPY package-lock.json ./
RUN npm ci --only=production
COPY . .
ARG PORT=8888
ENV PORT=${PORT}
EXPOSE ${PORT}
CMD [ -d "node_modules" ] && npm run start || npm ci --only=production && npm run start
EXPOSE 9999
USER node
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ Set the configuration file by specifying the relay address and the directory nam

Build:

Specify the port number on which the WebSocket server will be launched. The `PORT` variable specifying the port number in the Docker container. It can be any variable except `9999`.

Specify the port number on which the WebSocket server will be launched.
```
docker build --build-arg PORT=<port> -t proxy:v0.0.1 .
docker build --build-arg BUILD_FROM="node:20-alpine" -t proxy:v0.0.1 .
```

Launch:

To enable access to the WebSocket server from another application, it is essential to establish a port mapping between the host and the container. While not mandatory, it is advisable to align the port numbers in the container and on the host. So use the same port number as you did during build:

```
docker run --name libp2p-proxy --detach -p 127.0.0.1:<port>:<port> -p 127.0.0.1:9999:9999 -v ${PWD}:/proxy proxy:v0.0.1
docker run --name libp2p-proxy --detach -p 127.0.0.1:8888:8888 -p 127.0.0.1:9999:9999 -v ${PWD}:/proxy proxy:v0.0.1
```

To see logs:
Expand All @@ -51,7 +50,7 @@ docker stop libp2p-proxy
---

Requirements:
1. Node v.18.16.1
1. Node v.20.10.0

Installation:

Expand All @@ -67,7 +66,6 @@ Launch:

Specify the port number on which the WebSocket server will be launched.
```
export PORT=<port>
node src/index.js
```
Then you can connect a websocket client and libp2p nodes to proxy messages between them.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { multiaddr } from '@multiformats/multiaddr';
import dotenv from 'dotenv';
import { Libp2pManager } from './libp2pManager.js';
import { WebSocketManager } from './wsManager.js';
import { MessageHandler } from './messageHandler.js';
import { createDir4SavedData } from '../utils/saveData.js';
import { Logger } from '../utils/logger.js';

dotenv.config();

async function run() {
const logger = new Logger();
await createDir4SavedData(logger);
Expand Down
5 changes: 1 addition & 4 deletions src/libp2pManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
import { circuitRelayTransport } from 'libp2p/circuit-relay';
import { identifyService } from 'libp2p/identify';
import { multiaddr } from '@multiformats/multiaddr';
import dotenv from 'dotenv';
import { ConfigurationManager } from '../utils/configurationManager.js';

dotenv.config();

/**
* Libp2p manager. Contains all methods that are used for libp2p communication.
* @param logger Instance of the Logger class.
*/
export class Libp2pManager {
constructor(logger) {
this.configuration = new ConfigurationManager(logger);
this.realayAddress = process.env.RELAY_ADDRESS;
this.realayAddress = '/dns4/libp2p-relay-1.robonomics.network/tcp/443/wss/p2p/12D3KooWEMFXXvpZUjAuj1eKR11HuzZTCQ5HmYG9MNPtsnqPSERD';
this.logger = logger;
}

Expand Down
5 changes: 1 addition & 4 deletions src/messageHandler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import WebSocket from 'ws';
import fs from 'fs/promises';
import dotenv from 'dotenv';
import { saveMsg2File } from '../utils/saveData.js';

dotenv.config();

/**
* Handler for messsages from libp2p and websocket channels. It proxes msgs from
* libp2p to websocket clients and msgs from websocket clients to libp2p nodes.
Expand Down Expand Up @@ -70,7 +67,7 @@ export class MessageHandler {
* @param node Instance of the libp2p node.
*/
async sendSavedMsg(connectedPeerId, node) {
const directoryPath = process.env.SAVED_DATA_DIR_PATH;
const directoryPath = 'saved_data';
try {
const files = await fs.readdir(directoryPath);
files.forEach(async (file) => {
Expand Down
5 changes: 1 addition & 4 deletions src/wsManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { WebSocketServer } from 'ws';
import dotenv from 'dotenv';

dotenv.config();

/**
* Handler for messsages from libp2p and websocket channels. It proxes msgs from
Expand All @@ -23,7 +20,7 @@ export class WebSocketManager {
* @returns Instance of the server.
*/
#createWebsocketServer() {
const port = Number(process.env.PORT);
const port = 8888;
const wss = new WebSocketServer({ port }, () => {
this.logger.INFO(`WebSocket server listening on port ${port}`);
});
Expand Down
2 changes: 1 addition & 1 deletion utils/configurationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
*/
export class ConfigurationManager {
constructor(logger) {
this.filePath = process.env.PEER_ID_CONFIG_PATH;
this.filePath = 'peerIdJson.json';
this.logger = logger;
}

Expand Down
5 changes: 1 addition & 4 deletions utils/saveData.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fs from 'fs/promises';
import dotenv from 'dotenv';

dotenv.config();

const directoryPath = process.env.SAVED_DATA_DIR_PATH;
const directoryPath = 'saved_data';

/**
* Creates directory for saving data if it doesn't exists.
Expand Down

0 comments on commit d2e326d

Please sign in to comment.