Skip to content

Commit

Permalink
Merge branch '#23-postgres-db'
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Feb 2, 2022
2 parents ac31fc4 + 2693d25 commit a7b62dc
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 70 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Set to ignore everything except /src and package.json
*
!src
!package.json
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ node_modules
credentials.json
download_tests
build

FileShare
upload/
lathes/
mills/
mills/
file_share
postgres/db_data
72 changes: 21 additions & 51 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,63 +1,33 @@
version: "3.8"

services:

# access through `psql -h localhost -p 5432 -d filesync -U user`, password is 'pass'
# to be able to build after the initializing the db, you may need to run `sudo chown -R $USER db_data/`
database:
image: 'postgres:13'
env_file: ./.env
volumes:
- ./postgres/db_data/:/var/lib/postgresql/data/
- ./postgres/init/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
ports:
- ${PORT_DB}:${PORT_DB}

server:
build:
context: .
dockerfile: Dockerfile
depends_on:
- database
env_file: ./.env
volumes:
- ./src/build:/ffs/
- /ffs/node_modules
- ./FileShare:/ffs/FileShare/
- ./file_share:/ffs/file_share/
- ${LOCAL_DIR_GANTRY}:/ffs/file_share/Gantry/
- ${LOCAL_DIR_MILL}:/ffs/file_share/Mill/
- ${LOCAL_DIR_LATHE}:/ffs/file_share/Lathe/
- ${LOCAL_DIR_WATERJET}:/ffs/file_share/Waterjet/
ports:
- ${PORT}:${PORT}
command: "node server.js"

# task_runner_gantry:
# build:
# context: .
# dockerfile: Dockerfile
# env_file: ./.env
# volumes:
# - ./src/build:/ffs/
# - /ffs/node_modules
# - ${LOCAL_DIR_GANTRY}:/ffs/FileShare/Gantry
# 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_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"
- ${PORT_SERVER}:${PORT_SERVER}
command: "node server.js"
4 changes: 3 additions & 1 deletion environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ declare global {
FORGE_CLIENT_ID: string;
FORGE_CLIENT_SECRET: string;
FORGE_CALLBACK_URL: string;
PORT: string;
PORT_SERVER: number;
PORT_DB: number;
WEBHOOK_TOKEN: string;
}
}
}
Expand Down
46 changes: 45 additions & 1 deletion package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
"cookie-session": "^1.4.0",
"crypto": "^1.0.1",
"dotenv": "^10.0.0",
"express": "^4.17.2",
"forge-apis": "^0.8.6"
"express": "^4.17.1",
"forge-apis": "^0.8.6",
"generic-pool": "^3.8.2",
"ts-postgres": "^1.2.1"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
Expand Down
29 changes: 29 additions & 0 deletions postgres/init/create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Primary keys must include folder name in order to avoid collisions
-- between filenames in different folders

CREATE TABLE IF NOT EXISTS fusion (
fusion_id varchar(250) NOT NULL,
folder_name varchar(250) NOT NULL,
file_name varchar(250) NOT NULL,
username varchar(250),
size int,
version int,
PRIMARY KEY (fusion_id)
);

CREATE TABLE IF NOT EXISTS local (
file_name varchar(250) NOT NULL,
folder_name varchar(250) NOT NULL,
fusion_id varchar(250),
PRIMARY KEY (file_name, folder_name)
);

CREATE TABLE IF NOT EXISTS archive (
fusion_id varchar(250) NOT NULL,
file_name varchar(250) NOT NULL,
folder_name varchar(250) NOT NULL,
username varchar(250) NOT NULL,
size int,
version int,
PRIMARY KEY (fusion_id, folder_name)
);
10 changes: 6 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
*/

import { AuthClientThreeLegged } from 'forge-apis';

import * as dotenv from 'dotenv';

dotenv.config({path: '../.env'});

export const ForgeAuthClient = AuthClientThreeLegged;
Expand Down Expand Up @@ -53,9 +51,13 @@ export const folderIDtoLocal: any = {
"urn:adsk.wipprod:fs.folder:co.vJLXAKGbQQayeljr-nwztQ": "/Waterjet"
}

export const port = process.env.PORT;
export const port = process.env.PORT_SERVER;
export const forgeClientId = process.env.FORGE_CLIENT_ID;
export const forgeClientSecret = process.env.FORGE_CLIENT_SECRET;
export const forgeCallbackURL = process.env.FORGE_CALLBACK_URL;
export const hookCallbackURL = `${process.env.HOOK_CALLBACK_HOSTNAME}/hook`;
export const webhookToken: any = process.env.WEBHOOK_TOKEN;
export const webhookToken = process.env.WEBHOOK_TOKEN;
export const postgresUser = process.env.POSTGRES_USER;
export const postgresPassword = process.env.POSTGRES_PASSWORD;
export const postgresPort = process.env.PORT_DB;
export const postgresDB = process.env.POSTGRES_DB;
108 changes: 108 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Client, Query } from 'ts-postgres';
import { createPool } from 'generic-pool';
import * as config from './config';

const pool = createPool({
create: async () => {
return new Promise<Client>((resolve, reject) => {

const client = new Client({
'host': 'database', // name of docker container, not 'localhost'
'port': config.postgresPort,
'user': config.postgresUser,
'password': config.postgresPassword,
'database': config.postgresDB
});

client.connect()
.then(() => {
console.log("successfully connected to db")
resolve(client);
}).catch((err: any) => {
console.log()
console.log(err)
reject(err);
});
});
},
destroy: async (client: Client) => {
return client.end()
},
validate: (client: Client) => {
return Promise.resolve(!client.closed);
}
}, {
testOnBorrow: true,
max: 10
})

const executeQuery = (query: Query) => {
console.log('executing query');

const res = pool.acquire();

return res.then(async (client: Client) => {
console.log('aquired client from pool')
const result = await client.execute(query);
pool.release(client).then(() => {
console.log("client released back into pool")
});
return result;
}).catch((err: any) => {
console.log(err);
})
}

export const shutdown = () => {
pool.drain()
.then(() => {
return pool.clear();
});
}

export const insert = (table: string, params: any) => {

let fileName = params.fileName;
let folderName = params.folderName;
let fusionID = null;
let username = null;
let size = null;
let version = null;

if (params.hasOwnProperty('fusionID')) {fusionID = params.fusionID}
if (params.hasOwnProperty('username')) {username = params.username}
if (params.hasOwnProperty('size')) {size = params.size}
if (params.hasOwnProperty('version')) {version = params.version}

let query: Query;

console.log("params: ", params);

switch(table) {
case 'fusion':
query = new Query (
"INSERT INTO fusion (fusion_id, folder_name, file_name, username, size, version)" +
"VALUES ($1, $2, $3, $4, $5, $6)",
[fusionID, folderName, fileName, username, size, version]
);
break;
case 'local':
query = new Query (
"INSERT INTO local (file_name, folder_name, fusion_id)" +
"VALUES ($1, $2, $3)",
[fileName, folderName, fusionID]
);
break;
case 'archive':
query = new Query (
"INSERT INTO archive (fusion_id, folder_name, file_name, username, size, version)" +
"VALUES ($1, $2, $3, $4, $5, $6)",
[fusionID, folderName, fileName, username, size, version]
);
break;
default:
throw new Error("Invalid table/params");
}

return executeQuery(query);
}
6 changes: 3 additions & 3 deletions src/old/taskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const watcher = require('./watcher');
const downloader = require('./downloader');
const fs = require('fs');
import { AuthToken } from 'forge-apis';
import * as config from './config';
import * as localhook from './localhook';
import * as uploader from './uploadfile';
import * as config from '../config';
import * as localhook from '../localhook';
import * as uploader from '../uploadfile';


/**
Expand Down
2 changes: 1 addition & 1 deletion src/old/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// const axios = require('axios');
const axios = require('axios');

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 12/2/2021
Expand Down
Loading

0 comments on commit a7b62dc

Please sign in to comment.