diff --git a/flaskapp/app/Dockerfile b/flaskapp/app/Dockerfile new file mode 100644 index 0000000..7638908 --- /dev/null +++ b/flaskapp/app/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.6.8-alpine3.9 + +LABEL MAINTAINER="FirstName LastName " + +ENV GROUP_ID=1000 \ + USER_ID=1000 + +WORKDIR /var/www/ + +ADD . /var/www/ +RUN pip install -r requirements.txt +RUN pip install gunicorn + +RUN addgroup -g $GROUP_ID www +RUN adduser -D -u $USER_ID -G www www -s /bin/sh + +USER www + +EXPOSE 5000 + +CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "wsgi"] diff --git a/flaskapp/app/app.py b/flaskapp/app/app.py new file mode 100644 index 0000000..afac090 --- /dev/null +++ b/flaskapp/app/app.py @@ -0,0 +1,53 @@ +import os +from flask import Flask, request, jsonify +from flask_pymongo import PyMongo + +application = Flask(__name__) + +application.config["MONGO_URI"] = 'mongodb://' + os.environ['MONGODB_USERNAME'] + ':' + os.environ['MONGODB_PASSWORD'] + '@' + os.environ['MONGODB_HOSTNAME'] + ':27017/' + os.environ['MONGODB_DATABASE'] + +mongo = PyMongo(application) +db = mongo.db + +@application.route('/') +def index(): + return jsonify( + status=True, + message='Welcome to the Dockerized MongoDB app!' + ) + +@application.route('/todo') +def todo(): + _todos = db.todo.find() + + item = {} + data = [] + for todo in _todos: + item = { + 'id': str(todo['_id']), + 'todo': todo['todo'] + } + data.append(item) + + return jsonify( + status=True, + data=data + ) + +@application.route('/todo', methods=['POST']) +def createTodo(): + data = request.get_json(force=True) + item = { + 'todo': data['todo'] + } + db.todo.insert_one(item) + + return jsonify( + status=True, + message='To-do saved successfully!' + ), 201 + +if __name__ == "__main__": + ENVIRONMENT_DEBUG = os.environ.get("APP_DEBUG", True) + ENVIRONMENT_PORT = os.environ.get("APP_PORT", 5000) + application.run(host='0.0.0.0', port=ENVIRONMENT_PORT, debug=ENVIRONMENT_DEBUG) diff --git a/flaskapp/app/requirements.txt b/flaskapp/app/requirements.txt new file mode 100644 index 0000000..597c31c --- /dev/null +++ b/flaskapp/app/requirements.txt @@ -0,0 +1,3 @@ +Flask==1.1.1 +Flask-PyMongo==2.3.0 +requests==2.22.0 diff --git a/flaskapp/app/wsgi.py b/flaskapp/app/wsgi.py new file mode 100644 index 0000000..3cc98a4 --- /dev/null +++ b/flaskapp/app/wsgi.py @@ -0,0 +1,5 @@ +from app import application + + +if __name__ == "__main__": + application.run()