master
Michael Herman 7 years ago
parent 0475a145f4
commit 52459a009c

@ -3,22 +3,52 @@ version: '3.6'
services: services:
web: web:
build: . build:
ports: context: ./services/web
- 5001:5000 dockerfile: Dockerfile
expose:
- 5000
environment: environment:
- FLASK_ENV=production - FLASK_ENV=production
- APP_SETTINGS=project.config.DevelopmentConfig - APP_SETTINGS=project.config.ProductionConfig
- DATABASE_URL=postgres://postgres:postgres@db:5432/users - DB_USER=postgres
- DB_PASSWORD=postgres
- SECRET_CODE=myprecious
depends_on: depends_on:
- db - db
networks:
- app
db: db:
build: build:
context: ./project/db context: ./services/db
dockerfile: Dockerfile dockerfile: Dockerfile
volumes:
- data-volume:/var/lib/postgresql/data
expose: expose:
- 5432 - 5432
environment: environment:
- POSTGRES_USER=postgres - POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres - POSTGRES_PASSWORD=postgres
networks:
- app
nginx:
build:
context: ./services/nginx
dockerfile: Dockerfile
restart: always
ports:
- 80:80
depends_on:
- web
networks:
- app
networks:
app:
driver: bridge
volumes:
data-volume:
driver: local

@ -1,28 +0,0 @@
import os
from flask import Blueprint, jsonify, request
from project.api.models import User
users_blueprint = Blueprint('users', __name__, template_folder='./templates')
@users_blueprint.route('/users/ping', methods=['GET'])
def ping_pong():
return jsonify({
'status': 'success',
'message': 'pong!',
'container_id': os.uname()[1]
})
@users_blueprint.route('/users', methods=['GET'])
def get_all_users():
"""Get all users"""
response_object = {
'status': 'success',
'users': [user.to_json() for user in User.query.all()],
'container_id': os.uname()[1]
}
return jsonify(response_object), 200

@ -1,7 +0,0 @@
import os
class DevelopmentConfig():
"""Development configuration"""
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')

@ -0,0 +1,4 @@
FROM nginx:1.15.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY /prod.conf /etc/nginx/conf.d

@ -0,0 +1,14 @@
server {
listen 80;
location / {
proxy_pass http://web:5000;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}

@ -44,4 +44,4 @@ RUN chown -R app:app $APP_HOME
USER app USER app
# run server # run server
CMD gunicorn -b 0.0.0.0:5000 manage:app CMD gunicorn --log-level=debug -b 0.0.0.0:5000 manage:app

@ -24,6 +24,8 @@ def create_app(script_info=None):
migrate.init_app(app, db) migrate.init_app(app, db)
# register blueprints # register blueprints
from project.api.main import main_blueprint
app.register_blueprint(main_blueprint)
from project.api.users import users_blueprint from project.api.users import users_blueprint
app.register_blueprint(users_blueprint) app.register_blueprint(users_blueprint)

@ -0,0 +1,33 @@
import os
import logging
from flask import Blueprint, jsonify, request
LOGGER = logging.getLogger('gunicorn.error')
SECRET_CODE = os.environ.get('SECRET_CODE')
main_blueprint = Blueprint('main', __name__)
@main_blueprint.route('/ping', methods=['GET'])
def ping_pong():
LOGGER.info('Hitting the "/ping" route')
return jsonify({
'status': 'success',
'message': 'pong!',
'container_id': os.uname()[1]
})
@main_blueprint.route('/secret', methods=['POST'])
def secret():
LOGGER.info('Hitting the "/secret" route')
response_object = {
'status': 'success',
'message': 'nay!',
'container_id': os.uname()[1]
}
if request.get_json().get('secret') == SECRET_CODE:
response_object['message'] = 'yay!'
return jsonify(response_object)

@ -1,5 +1,3 @@
from flask import current_app
from project import db from project import db

@ -0,0 +1,22 @@
import os
import logging
from flask import Blueprint, jsonify
from project.api.models import User
users_blueprint = Blueprint('users', __name__)
LOGGER = logging.getLogger('gunicorn.error')
@users_blueprint.route('/users', methods=['GET'])
def get_all_users():
LOGGER.info('Hitting the "/users" route')
response_object = {
'status': 'success',
'users': [user.to_json() for user in User.query.all()],
'container_id': os.uname()[1]
}
return jsonify(response_object), 200

@ -0,0 +1,10 @@
import os
USER = os.environ.get('DB_USER')
PASSWORD = os.environ.get('DB_PASSWORD')
class ProductionConfig():
"""Production configuration"""
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = f'postgres://{USER}:{PASSWORD}@db:5432/users'
Loading…
Cancel
Save