From 0e61ae56ca65351f101e6fb4481e6cf4a6734e00 Mon Sep 17 00:00:00 2001 From: pan261 Date: Thu, 20 Jan 2022 13:04:16 -0500 Subject: [PATCH] parsing incomming message, docker with npm --- docker-compose.yml | 66 +++++++++++++++++++++++----------------------- package.json | 2 +- src/server.ts | 21 +++++++++++++-- src/taskRunner.ts | 5 ++++ src/watcher2.ts | 13 +++++---- 5 files changed, 66 insertions(+), 41 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 1f1d65e..170764d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,38 +25,38 @@ services: network_mode: host command: "node taskRunner.js gantry" - task_runner_lathe: - build: - context: . - dockerfile: Dockerfile - env_file: ./.env - volumes: - - ./src/build:/ffs/ - - /ffs/node_modules - - ${LOCAL_DIR_LATHE}:/ffs/FileShare/Lathe - network_mode: host - command: "node taskRunner.js lathe" + # task_runner_lathe: + # build: + # context: . + # dockerfile: Dockerfile + # env_file: ./.env + # volumes: + # - ./src/build:/ffs/ + # - /ffs/node_modules + # - ${LOCAL_DIR_LATHE}:/ffs/FileShare/Lathe + # network_mode: host + # command: "node taskRunner.js lathe" - task_runner_mill: - build: - context: . - dockerfile: Dockerfile - env_file: ./.env - volumes: - - ./src/build:/ffs/ - - /ffs/node_modules - - ${LOCAL_DIR_MILL}:/ffs/FileShare/Mill - network_mode: host - command: "node taskRunner.js mill" + # task_runner_mill: + # build: + # context: . + # dockerfile: Dockerfile + # env_file: ./.env + # volumes: + # - ./src/build:/ffs/ + # - /ffs/node_modules + # - ${LOCAL_DIR_MILL}:/ffs/FileShare/Mill + # network_mode: host + # command: "node taskRunner.js mill" - task_runner_waterjet: - build: - context: . - dockerfile: Dockerfile - env_file: ./.env - volumes: - - ./src/build:/ffs/ - - /ffs/node_modules - - ${LOCAL_DIR_WATERJET}:/ffs/FileShare/Waterjet - network_mode: host - command: "node taskRunner.js waterjet" \ No newline at end of file + # task_runner_waterjet: + # build: + # context: . + # dockerfile: Dockerfile + # env_file: ./.env + # volumes: + # - ./src/build:/ffs/ + # - /ffs/node_modules + # - ${LOCAL_DIR_WATERJET}:/ffs/FileShare/Waterjet + # network_mode: host + # command: "node taskRunner.js waterjet" \ No newline at end of file diff --git a/package.json b/package.json index 9e68cc0..00ee711 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "node /src/server.js", "test": "echo \"Error: no test specified\" && exit 1", "tsc": "tsc", - "docker:build": "tsc && docker-compose up --build" + "docker:build": "tsc && docker-compose up --build --remove-orphans" }, "repository": { "type": "git", diff --git a/src/server.ts b/src/server.ts index ef48fe2..81ba333 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,5 +1,6 @@ import express = require('express'); import * as config from './config'; +const fs = require('fs'); /** @@ -100,8 +101,24 @@ const createServer = () => { }); app.post('/hook', function (req: any, res: any) { - console.log(req.body); - res.status(200).end; + // console.log(req.data); + // console.log(req.payload); + // console.log(req.res); + // fs.writeFile(__dirname + '/FileShare/res.json', req, (err: any) => { + // if (err) { + // throw err; + // } + // }) + let body: any = []; + req.on('error', (err: any) => { + console.error(err); + }).on('data', (chunk: any) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + console.log(body) + }) + res.status(204).send(); }); // Default endpoint diff --git a/src/taskRunner.ts b/src/taskRunner.ts index 2ee3c55..a67b554 100644 --- a/src/taskRunner.ts +++ b/src/taskRunner.ts @@ -1,5 +1,6 @@ const axios = require('axios'); const watcher = require('./watcher'); +const watcher2 = require('./watcher2') const downloader = require('./downloader'); const fs = require('fs'); import * as config from './config'; @@ -68,6 +69,7 @@ const fetchToken = () => { '\x1b[92mtaskRunner.js::fetchToken:', '\x1b[0mCredentials received' ); + watcher2.createHook(credentials); } else { console.log( '\x1b[96mtaskRunner.js::fetchToken:', @@ -102,6 +104,9 @@ const getCredentials = () => { getCredentials(); + +// Below is the previous watcher implementation + /** * Function to dispatch the watcher */ diff --git a/src/watcher2.ts b/src/watcher2.ts index 12ff73b..1af27f4 100644 --- a/src/watcher2.ts +++ b/src/watcher2.ts @@ -1,21 +1,24 @@ -// const axios = require('axios'); -exports.watch = (credentials: any) => { + +exports.createHook = (credentials: any) => { + const axios = require('axios'); + axios({ method: 'POST', url: 'https://developer.api.autodesk.com/webhooks/v1/systems/data/events/dm.version.added/hooks', headers: { + 'Content-Type': 'application/json', Authorization: `Bearer ${credentials.access_token}` }, - body: { - callbackUrl: "http://localhost:3000/hook", + data: { + callbackUrl: "http://6ae7-128-210-106-76.ngrok.io/hook", autoReactivateHook: false, scope: { folder: "urn:adsk.wipprod:fs.folder:co.IxiedfeBTOGg6NSIGQhXxQ" // Gantry folder } } }).then((res: any) => { - console.log(res.data); + console.log(res); }).catch((err: any) => { console.log(err); });