Skip to content

Commit

Permalink
Merge branch 'develop' into ft-user-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelOyegbemi committed Aug 31, 2020
2 parents 8948c99 + 0e1d29c commit 7c1968a
Show file tree
Hide file tree
Showing 17 changed files with 1,165 additions and 9,455 deletions.
2 changes: 1 addition & 1 deletion .env.example
Expand Up @@ -2,7 +2,7 @@ PORT=5000
NODE_ENV=development
APP_PROTOCOL=http
APP_URL=XXXXXXX.com
REDIS_PORT=6379
REDIS_URL=redis://127.0.0.1:6379

DB_URL=XXXXXXXXXXXXXX
DB_URL_TEST=XXXXXXXXXXXXXX
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -63,3 +63,6 @@ typings/
# VSCode config files
.vscode/
*.code-workspace

# Backups
db-backup/
23 changes: 23 additions & 0 deletions Dockerfile
@@ -0,0 +1,23 @@
FROM node:13-alpine
MAINTAINER EdustripeDev <edustripedev@gmail.com>
# Update and install packages
RUN apk update
RUN apk add --update git zip vim wget unzip curl nginx python3 py3-pip yarn bash

# Install awscli using pip
RUN pip3 install awscli --upgrade

# prepare a user which runs everything locally!
RUN adduser --disabled-password -s /bin/bash edustripe

COPY ./arch/scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
COPY ./arch/scripts/repo.sh /usr/local/bin/repo.sh

RUN chmod +x /usr/local/bin/repo.sh && chmod +x /usr/local/bin/entrypoint.sh

#set the terminal to xterm
RUN export TERM=xterm

WORKDIR /var/www

CMD ["entrypoint.sh"]
18 changes: 16 additions & 2 deletions README.md
Expand Up @@ -11,7 +11,7 @@ This repository houses the Edustripe Authentication API

- Dependencies: First, install the project's dependencies. Run this command in the project's root folder:
```bash
$ npm install
$ yarn install
```

- Environment variables: Create a `.env` file and copy the variables in the example file by running
Expand All @@ -22,5 +22,19 @@ $ cp .env.example .env
## Start Server
Start the development server by running
```bash
$ npm run dev
$ yarn run dev
```

## Run development server with docker
Ensure you have docker and docker-compose installed on your machine, then run
```bash
$ docker-compose build
$ docker-compose up -d account
```

## Run test with docker
Ensure you have docker and docker-compose installed on your machine, then run
```bash
$ docker-compose build
$ docker-compose run -e APP_ENV=test account
```
@@ -1,4 +1,3 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Credentials', {
id: {
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/cache.js
@@ -1,8 +1,9 @@
import redis from 'redis';
import env from '../config/env';

const REDIS_PORT = process.env.REDIS_PORT || 6379;
const REDIS_URL = env.REDIS_URL || 'redis://127.0.0.1:6379';

export const redisClient = redis.createClient(REDIS_PORT);
export const redisClient = redis.createClient(REDIS_URL);

/**
* @function cache
Expand Down
28 changes: 28 additions & 0 deletions arch/conf.d/development.conf
@@ -0,0 +1,28 @@
server {
listen 80;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:5000/;
}

# Block all git access
if ($request_uri ~* ^.*\.git.*$) {
return 404;
}

# Enable Gzip for
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types image/jpeg image/png text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
gzip_min_length 70000;
gunzip on;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
}
8 changes: 8 additions & 0 deletions arch/db/setup-test-db.sql
@@ -0,0 +1,8 @@
CREATE DATABASE edustripe_account_test
WITH
OWNER = edustripe
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
33 changes: 33 additions & 0 deletions arch/scripts/entrypoint.sh
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

export WORKING_DIR=/var/www
export APP_WORKSPACE=${APP_WORKSPACE:-$APP_NAME}
export APP_ENV=${APP_ENV:-local}

echo ">>Preparing app $APP_NAME..."
echo ">>APP_ENV: $APP_ENV"

if [[ $APP_ENV != "local" && $APP_ENV != "test" ]]; then
# Download and setup repository if not in local environment
source repo.sh init
fi

#Change directory to APP_WORKSPACE
cd "$WORKING_DIR/$APP_WORKSPACE" || exit 1

#Install dependencies
echo ">>Running yarn to install or update dependencies..."
yarn

if [ $APP_ENV == "test" ]; then
chmod +x "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/run-test.sh"
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/run-test.sh" || exit 1
echo ">>Test ran successfully";exit 0
else
#Do any necessary setup Move this back
chmod +x "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/setup.sh"
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/setup.sh" || exit 1
fi

chmod +x "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/start-server.sh"
source "$WORKING_DIR/$APP_WORKSPACE/arch/scripts/start-server.sh" || exit 1
152 changes: 152 additions & 0 deletions arch/scripts/repo.sh
@@ -0,0 +1,152 @@
#!/usr/bin/env bash

set -e
set -o pipefail

GIT_OAUTH2_USER="${GIT_OAUTH2_USER:-oauth2}"
GIT_BRANCH="${GIT_BRANCH:-master}"

ACTION="${1:-init}"
shift

VALID_ACTIONS=("checkout" "pull" "clone" "reset" "status" "init")
if [[ ! " ${VALID_ACTIONS[*]} " == *" $ACTION "* ]]; then
printf '>>ERROR: invalid action %s \nAction is the first argument and can only be any of:\n %s\n' "$ACTION" "$(IFS=, ; echo "${VALID_ACTIONS[*]}")" >&2
exit 1
fi

while :; do
case $1 in
-b|--branch)
if [ "$2" ]; then
GIT_BRANCH=$2
shift
else
printf 'ERROR: %s is a non-empty optional argument\n' "$1" >&2
exit 1
fi
;;
-u|--url)
if [ "$2" ]; then
FULL_GIT_URL=$2
shift
else
printf 'ERROR: %s is a non-empty optional argument\n' "$1" >&2
exit 1
fi
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'ERROR: Unknown option : %s\n' "$1" >&2
exit 1
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done

if [ -z "$APP_WORKSPACE" ]; then
echo ">>Need to set APP_NAME env variable";exit 1
fi

if [ -z "$FULL_GIT_URL" ]; then
if [ -z "$REPOSITORY_URL" ]; then
echo ">>Need to set REPOSITORY_URL env variable or provide -u argument"
exit 1
elif [ -z "$GIT_OAUTH2_TOKEN" ]; then
echo ">>Need to set GIT_OAUTH2_TOKEN env variable or provide -u argument"
exit 1
else
FULL_GIT_URL="https://${GIT_OAUTH2_USER}:${GIT_OAUTH2_TOKEN}@${REPOSITORY_URL}"
fi
fi

checkout ()
{
if [ -d "$WORKING_DIR/$APP_WORKSPACE/.git" ]; then
printf ">>Checking out to branch [%s]:\n" "$GIT_BRANCH"
cd "$WORKING_DIR/$APP_WORKSPACE";git checkout $GIT_BRANCH;
else
printf ">>The [%s] repo is not cloned.\n" "$APP_WORKSPACE"
fi
}

pull ()
{
echo ">>Changing directory to $WORKING_DIR/$APP_WORKSPACE"
cd "$WORKING_DIR/$APP_WORKSPACE" || exit 1
echo ">>Pulling latest changes from branch $GIT_BRANCH into $WORKING_DIR/$APP_WORKSPACE"
git pull $FULL_GIT_URL $GIT_BRANCH
checkout
}

clone ()
{
echo ">>Changing directory to $WORKING_DIR"
cd $WORKING_DIR;
echo ">>Cloning fresh repository..."
git clone $FULL_GIT_URL "$WORKING_DIR/$APP_WORKSPACE"
if [ -d "$WORKING_DIR/$APP_WORKSPACE/.git" ]; then
echo ">>Successfull clone $REPOSITORY_URL"
checkout
else
echo ">>Error occurred, could not clone $REPOSITORY_URL, Exiting..."
exit 1
fi
}

init ()
{
if [ -d "$WORKING_DIR/$APP_WORKSPACE" ]; then
if [ -d "$WORKING_DIR/$APP_WORKSPACE/.git" ]; then
echo ">>Repository $REPOSITORY_URL already exist"
pull
else
echo ">>Workspace $APP_WORKSPACE already exist but not a git repository; removing old Workspace."
rm -fr "$WORKING_DIR/$APP_WORKSPACE"
clone
fi
else
clone
fi
}

reset ()
{
cd "$WORKING_DIR/$APP_WORKSPACE" || echo ">>Directory $WORKING_DIR/$APP_WORKSPACE does not exits" && exit 1
git reset --hard HEAD
git checkout $GIT_BRANCH
git reset --hard "origin/$GIT_BRANCH"
git pull
}

status ()
{
if [ -d "$WORKING_DIR/$APP_WORKSPACE/.git" ]; then
printf ">>Git status for [%s]:\n" "$APP_WORKSPACE"
cd "$WORKING_DIR/$APP_WORKSPACE";git status;
else
printf "The [%s] repo is not cloned.\n" "$APP_WORKSPACE"
fi
}

if [ "$ACTION" == "checkout" ]; then
checkout
elif [ "$ACTION" == "clone" ]; then
clone
elif [ "$ACTION" == "pull" ]; then
pull
elif [ "$ACTION" == "reset" ]; then
read -p ">>This will override any uncommited changes in your local git checkouts. Would you like to proceed? [y/n] " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
reset
fi
elif [ "$ACTION" == "status" ]; then
status
elif [ "$ACTION" == "init" ]; then
init
fi
16 changes: 16 additions & 0 deletions arch/scripts/run-test.sh
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e
set -o pipefail

if [ "$TEST_ONLY" == "1" ]; then
echo ">>Running test with 'yarn test:all'..."
yarn test:all
else
echo ">>Running lint with 'yarn lint'..."
yarn lint
echo ">>Build app 'yarn build'..."
yarn build
echo ">>Running test with 'yarn test'..."
yarn test
fi
24 changes: 24 additions & 0 deletions arch/scripts/setup.sh
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -e
set -o pipefail

if [ "$APP_ENV" != "local" ]; then
#Update nginx config and start it if not on local environment
echo ">>Copying nginx configuration..."
cat "$WORKING_DIR/$APP_WORKSPACE/arch/conf.d/$APP_ENV.conf" > /etc/nginx/conf.d/default.conf
echo ">>Starting nginx..."
nginx || exit 1

## Copy environment file
cp ".env.$APP_ENV" ".env"
fi

if [ "$UNDO_MIGRATION_ON_STARTUP" == 1 ]; then
echo ">>Undoing database migrations..."
yarn db:migrate:undo
fi

#Run migrations
echo ">>Running database migrations..."
yarn db:migrate
15 changes: 15 additions & 0 deletions arch/scripts/start-server.sh
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e
set -o pipefail

if [ $APP_ENV == "local" ]; then
echo ">>Running local server..."
yarn dev
else
# Build and run server
echo ">>Building app with 'yarn build...'"
yarn build
echo "Starting $APP_ENV server..."
yarn start
fi

0 comments on commit 7c1968a

Please sign in to comment.