From 293d6ddba691cb39f0b9eb361f8552b34d251713 Mon Sep 17 00:00:00 2001 From: IakMastro Date: Wed, 19 May 2021 17:46:33 +0300 Subject: [PATCH] Database initialization --- .gitignore | 3 ++- backend/app.py | 19 +++++++++++++++---- backend/requirements.txt | 3 +-- db/Dockerfile | 3 +++ db/init-db.d/seed.js | 25 +++++++++++++++++++++++++ docker-compose.yml | 10 ++++++++-- install_and_run_arch.sh | 12 ++++++++++++ install_and_run_debian.sh | 22 ++++++++++++++++++++++ restart.sh | 5 +++++ run.sh | 3 +++ stop.sh | 3 +++ 11 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 db/Dockerfile create mode 100644 db/init-db.d/seed.js create mode 100644 install_and_run_arch.sh create mode 100644 install_and_run_debian.sh create mode 100755 restart.sh create mode 100755 run.sh create mode 100644 stop.sh diff --git a/.gitignore b/.gitignore index 0be0f99..df6b0f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea __pycache__ -node_modules \ No newline at end of file +node_modules +data \ No newline at end of file diff --git a/backend/app.py b/backend/app.py index 05d358f..ef64a89 100644 --- a/backend/app.py +++ b/backend/app.py @@ -2,21 +2,32 @@ from flask import Flask, jsonify, request from flask_cors import CORS -import pymongo +from flask_pymongo import PyMongo +# Configuration DEBUG = True +# Initialize app app = Flask(__name__) app.config.from_object(__name__) -mongo_client = pymongo.MongoClient("mongodb://datinguser:datinguserpasswd@mongodb:27017/") + +# Mongodb connection initialization to database gameStore' +app.config['MONGO_URI'] = "mongodb://datinguser:datinguserpasswd@mongodb:27017/tinderClone?authSource=admin" +mongo = PyMongo(app) + +# Enable CORS (CORS is a library that enables cross-origin requests) +# For example different protocol, IP address, domain name or port CORS(app, resources={r'/*': {'origins': '*'}}) +# Sanity check route @app.route('/ping', methods=['GET']) def ping_pong(): - query = mongo_client['test']['ping'].find_one()['text'] - return jsonify(query) + return jsonify("pong!") + +# Routing +# TODO: REST API functions here if __name__ == '__main__': app.run() diff --git a/backend/requirements.txt b/backend/requirements.txt index 0d3b9cb..3c3a3e0 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,4 +1,3 @@ flask flask-cors -flask-pymongo -pymongo \ No newline at end of file +flask-pymongo \ No newline at end of file diff --git a/db/Dockerfile b/db/Dockerfile new file mode 100644 index 0000000..74eeb76 --- /dev/null +++ b/db/Dockerfile @@ -0,0 +1,3 @@ +FROM mongo + +COPY ./init-db.d/seed.js /docker-entrypoint-initdb.d \ No newline at end of file diff --git a/db/init-db.d/seed.js b/db/init-db.d/seed.js new file mode 100644 index 0000000..efa15ec --- /dev/null +++ b/db/init-db.d/seed.js @@ -0,0 +1,25 @@ +db.users.drop(); +db.users.insertMany([ + { + _id: 1, + email: 'foo.bar@email.com', + name: "foo.bar", + password: 'lol password ez', + birthday: "1970/01/01", + gender: "Male", + weight: '80kg', + height: '1.75m', + hair_colour: 'black', + eye_colour: 'blue', + sexual_orientation: "Heterosexual", + education: "University", + smoker: false, + drinker: false, + children: false, + status: "Student", + bio: "bla bla bla bla bla bla bla bla bla bla", + pfp: "path", + photos: "path", + type: "premium" + } +]); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8313d2e..e386baa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ version: "3.8" services: client: build: client + container_name: tinder_client ports: - "8080:8080" volumes: @@ -15,6 +16,7 @@ services: backend: build: backend + container_name: tinder_api ports: - "5000:5000" volumes: @@ -29,6 +31,7 @@ services: mongo-express: image: mongo-express restart: always + container_name: mongo-express_tinder ports: - "8081:8081" environment: @@ -38,11 +41,14 @@ services: - mongo mongo: - image: mongo - container_name: mongodb + build: db + container_name: mongodb_tinder restart: always ports: - "27017:27017" + volumes: + - ./db/data:/data/db environment: MONGO_INITDB_ROOT_USERNAME: datinguser MONGO_INITDB_ROOT_PASSWORD: datinguserpasswd + MONGO_INITDB_DATABASE: tinderClone diff --git a/install_and_run_arch.sh b/install_and_run_arch.sh new file mode 100644 index 0000000..3605a82 --- /dev/null +++ b/install_and_run_arch.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +echo "Installing docker" +sudo pacman -Syu docker +sudo systemctl start docker.service +sudo systemctl enable docker.service + +sudo groupadd docker +sudo usernod -aG docker "${USERNAME}" + +echo "Successfully installed docker" +./run.sh diff --git a/install_and_run_debian.sh b/install_and_run_debian.sh new file mode 100644 index 0000000..b41e063 --- /dev/null +++ b/install_and_run_debian.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +echo "Installing docker" +sudo apt-get install \ + apt-transport-https \ + ca-certificates \ + curl \ + gnupg \ + lsb-release + +curl -fsSL https://download.docker.com/linux/debian/gpg | \ + sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + +echo \ + "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \ + $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + +sudo apt-get update +sudo apt-get install docker-ce docker-ce-cli containerd.io + +echo "Successfully installed docker" +./run.sh \ No newline at end of file diff --git a/restart.sh b/restart.sh new file mode 100755 index 0000000..7157efe --- /dev/null +++ b/restart.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +docker-compose down +sudo rm -rf db/data +docker-compose up -d --force-recreate \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..7ce6ee1 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +docker-compose up -d \ No newline at end of file diff --git a/stop.sh b/stop.sh new file mode 100644 index 0000000..870e2d1 --- /dev/null +++ b/stop.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +docker-compose stop \ No newline at end of file