cs141135
4 years ago
3 changed files with 84 additions and 0 deletions
@ -0,0 +1,26 @@ |
|||||
|
upstream app_server { |
||||
|
server flask:5000; |
||||
|
} |
||||
|
|
||||
|
server { |
||||
|
listen 80; |
||||
|
server_name _; |
||||
|
error_log /var/log/nginx/error.log; |
||||
|
access_log /var/log/nginx/access.log; |
||||
|
client_max_body_size 64M; |
||||
|
|
||||
|
location / { |
||||
|
try_files $uri @proxy_to_app; |
||||
|
} |
||||
|
|
||||
|
location @proxy_to_app { |
||||
|
gzip_static on; |
||||
|
|
||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
||||
|
proxy_set_header X-Forwarded-Proto $scheme; |
||||
|
proxy_set_header Host $http_host; |
||||
|
proxy_buffering off; |
||||
|
proxy_redirect off; |
||||
|
proxy_pass http://app_server; |
||||
|
} |
||||
|
} |
@ -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) |
@ -0,0 +1,5 @@ |
|||||
|
from app import application |
||||
|
|
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
application.run() |
Loading…
Reference in new issue