Skip to content

Commit

Permalink
Merge branch 'main' into search-highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
PianoRollRepresentation committed Jun 22, 2021
2 parents 39a7755 + 2f3b1e4 commit 263a3de
Show file tree
Hide file tree
Showing 52 changed files with 828 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
# kmap.import-action.start-db #
- run: node build/start-database
- run: node build/start-database -d

- run: npm install -g yarn
# kmap.import-action.cache #
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/frontend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
run: echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

# kmap.import-action.start-db #
- run: node build/start-database
- run: node build/start-database -d


- run: yarn install --frozen-lockfile --prefer-offline --network-timeout 1000000
Expand Down
54 changes: 54 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# syntax=docker/dockerfile:1
# This implements a multi-stage container build process

# First build the project
FROM node:14-alpine AS dev
RUN apk update && apk add --no-cache python g++ make bash

# Set environment variables
ENV DOCKER=DOCKER

# Set our working directory
WORKDIR /usr/src/kmap/backend

# Install our dependencies
COPY package*.json ./
COPY yarn.lock* ./
COPY scripts scripts/
RUN chmod +x scripts/backend-prebuild.sh
RUN yarn install

# Build the project
COPY . .
RUN yarn run build

# Now build the container that is used in production
FROM node:14.17-alpine as prod

# Install bash
RUN apk update && apk add --no-cache bash

# Set environment variables
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
ENV DOCKER=DOCKER

# Set our working directory
WORKDIR /usr/src/kmap/backend

# Install our dependencies (production only)
COPY package*.json ./
COPY yarn.lock* ./
COPY scripts scripts/
RUN chmod +x scripts/backend-prebuild.sh
RUN yarn install --production

# Copy the dist folder from the dev container
COPY --from=dev /usr/src/kmap/backend/dist dist

# Copy the docker specific scripts and files
COPY docker docker

# Start
RUN chmod +x docker/run.sh
CMD ["docker/run.sh"]
19 changes: 19 additions & 0 deletions backend/docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# If the host points to localhost (or is not set), we need have to replace this
# with the address of the docker host
if [[ -z ${NEO4J_HOST} || ${NEO4J_HOST} == localhost || ${NEO4J_HOST} == 127.0.0.1 || ${NEO4J_HOST} == ::1 ]]; then
echo "Rewrite neo4j host to docker host, as localhost was specified"
export NEO4J_HOST=$(ip route show | awk '/default/ {print $3}');
fi

if [[ -z ${NEO4J_PORT} ]]; then
echo "Using default neo4j port 7687"
export NEO4J_PORT=7687;
fi

# TODO
export NEO4J_SCHEME=neo4j;

echo "Starting backend..."
node dist/src/main.js
8 changes: 4 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"license": "MIT",
"scripts": {
"postinstall": "run-script-os",
"postinstall:windows": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ..\\build\\backend-prebuild.ps1",
"postinstall:default": "../build/backend-prebuild.sh",
"postinstall:windows": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command .\\scripts\\backend-prebuild.ps1",
"postinstall:default": "scripts/backend-prebuild.sh",
"prebuild": "run-script-os",
"prebuild:windows": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ..\\build\\backend-prebuild.ps1",
"prebuild:default": "../build/backend-prebuild.sh",
"prebuild:windows": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command .\\scripts\\backend-prebuild.ps1",
"prebuild:default": "scripts/backend-prebuild.sh",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Goto the backend dir
cd ../backend
cd $PSScriptRoot\..

# Copy shared files
New-Item -ItemType Directory -Force -Path ".\src\shared"
Expand Down
18 changes: 18 additions & 0 deletions backend/scripts/backend-prebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# Please make sure that this file is executable if you are working in a windows environment
# This can be done by running "git update-index --chmod=+x .\backend-prebuild.sh" in the build directory

# Goto the backend dir
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR"/..

# Only execute if not running inside a docker container
if [ "$DOCKER" != "DOCKER" ]; then
# Copy shared files
cp -a ../shared/src/. ./src/shared/

# Execute linter
rimraf dist
yarn lint
fi
53 changes: 44 additions & 9 deletions backend/src/config/neo4j/createNeo4jDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,57 @@ import {
Driver,
} from 'neo4j-driver';

/**
* Adapted from @nest-neo4j's createDriver
* @param config
* @param neo4jConfig
*/
export const createNeo4jDriver = async (
function delay(timeout: number): Promise<void> {
if (timeout <= 0) {
return Promise.resolve();
}

return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
}

async function tryConnect(driver: Driver): Promise<boolean> {
let session;
try {
await driver.verifyConnectivity();
session = driver.session();
await session.run('MATCH (n) RETURN n LIMIT 1');
return true;
} catch (e) {
return false;
} finally {
await session?.close();
}
}

async function createDriverWithRetry(
config: Neo4jConfig,
neo4jConfig: OriginalNeo4jConfig
): Promise<Driver> => {
): Promise<Driver> {
const driver = neo4jDriver(
`${config.scheme}://${config.host}:${config.port}`,
auth.basic(config.username, config.password),
neo4jConfig
);

await driver.verifyConnectivity();
// eslint-disable-next-line no-await-in-loop
while (!(await tryConnect(driver))) {
const timeout = 5000;
// eslint-disable-next-line no-console
console.log(`Connection Error: Retrying in ${timeout}ms`);
// eslint-disable-next-line no-await-in-loop
await delay(timeout);
}

return driver;
};
}

/**
* Adapted from @nest-neo4j's createDriver
* @param config
* @param neo4jConfig
*/
export const createNeo4jDriver = createDriverWithRetry;
14 changes: 0 additions & 14 deletions build/backend-prebuild.sh

This file was deleted.

13 changes: 0 additions & 13 deletions build/frontend-prebuild.sh

This file was deleted.

66 changes: 66 additions & 0 deletions build/parse-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const debug = false;

function camelize(str) {
return str
.split('-')
.map(
(item, index) => index > 0 ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item.toLowerCase()
).join("");
}

function logFound(arg, key, value) {
if (debug) {
console.log("Found argument '" + arg + "'. Writing property '" + key + "' with value '" + value + "' to args object.");
}
}

const parseArgs = function(input, flags, args) {
const result = {};

for (let i = 0; i < input.length; i++) {
const key = input[i];
let argProcessed = false;

if (flags) {
for (const flag of flags) {
if ('--' + flag.name === key || (typeof flag.abbr === 'string' && '-' + flag.abbr === key)) {

const camelizedName = camelize(flag.name);

logFound(flag.name, camelizedName, 'true');

result[camelizedName] = true;
argProcessed = true;
}
}
}

if (!argProcessed && args) {
for (const arg of args) {
if ('--' + arg.name === key) {
i++;

if (i >= input.length) {
throw new Error('No value present for command-line argument:' + key);
}

const value = input[i];
const camelizedName = camelize(arg.name);

logFound(arg.name, camelizedName, value);

result[camelizedName] = value;
argProcessed = true;
}
}
}

if (!argProcessed) {
throw new Error('Unknown command-line argument: ' + key);
}
}

return result;
}

exports.parseArgs = parseArgs;
Loading

0 comments on commit 263a3de

Please sign in to comment.