-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
venv/ | ||
|
||
*.pyc | ||
__pycache__/ | ||
|
||
instance/ | ||
|
||
.pytest_cache/ | ||
.coverage | ||
htmlcov/ | ||
|
||
dist/ | ||
build/ | ||
*.egg-info/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# pull official base image | ||
FROM python:3.8.0-alpine | ||
|
||
# set work directory | ||
WORKDIR /usr/src/app | ||
|
||
# set environment variables | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev | ||
|
||
# install dependencies | ||
RUN pip install --upgrade pip | ||
COPY ./requirements.txt /usr/src/app/requirements.txt | ||
RUN export LDFLAGS="-L/usr/local/opt/openssl/lib" | ||
RUN pip install -r requirements.txt | ||
|
||
# copy project | ||
COPY . /usr/src/app/ | ||
|
||
EXPOSE 5000 | ||
|
||
RUN ls -la app/ | ||
|
||
ENTRYPOINT ["app/docker-entrypoint.sh"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# https://opensource.com/article/19/11/python-web-api-flask | ||
|
||
1. pip install flask-restful | ||
2. pip freeze > requirements.txt | ||
3. python main.py | ||
|
||
# Adding gunicorn and nginx | ||
# https://medium.com/faun/deploy-flask-app-with-nginx-using-gunicorn-7fda4f50066a | ||
|
||
1. asdas | ||
|
||
# sqlAlchemy | ||
|
||
1. pip install flask_sqlalchemy | ||
|
||
# app/__init__.py | ||
|
||
1. export FLASK_APP=app | ||
2. export FLASK_ENV=development | ||
3. flask run | ||
|
||
# will add gunicorn later | ||
|
||
# https://stackoverflow.com/questions/55839111/installing-psycopg2-fails-on-macos-with-unclear-error-message | ||
|
||
# flask-alembic | ||
|
||
1. flask db init => creates the migrations folder | ||
2. flask db revision -m "create account table" => creates a new version | ||
3. flask db upgrade => runs the migrations | ||
|
||
# https://testdriven.io/blog/dockerizing-flask-with-postgres-gunicorn-and-nginx/ | ||
|
||
# docker-compose | ||
1. docker-compose up --build --remove-orphans |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from flask import Flask | ||
from flask_migrate import Migrate | ||
from flask_redis import FlaskRedis | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
# from flask_migrate import Migrate | ||
# from . import routes, models | ||
|
||
# Globally accessible libraries | ||
db = SQLAlchemy() | ||
r = FlaskRedis() | ||
|
||
|
||
def create_app(): | ||
"""Initialize the core application.""" | ||
app = Flask(__name__, instance_relative_config=False) | ||
app.config.from_object('config.Config') | ||
|
||
# Initialize Plugins | ||
db.init_app(app) | ||
r.init_app(app) | ||
|
||
with app.app_context(): | ||
# Include our Routes | ||
from . import routes | ||
|
||
# Register Blueprints | ||
# app.register_blueprint(auth.auth_bp) | ||
# app.register_blueprint(admin.admin_bp) | ||
|
||
# Migration | ||
migrate = Migrate(app, db) | ||
|
||
# No need for this | ||
# Should be taken care of by migrate | ||
# Create tables for our models | ||
# db.create_all() | ||
|
||
return app |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from app import db | ||
|
||
|
||
class Account(db.Model): | ||
"""Model for accounts.""" | ||
|
||
__tablename__ = 'account' | ||
|
||
id = db.Column(db.Integer, | ||
primary_key=True) | ||
name = db.Column(db.String(64), | ||
index=False, | ||
unique=True, | ||
nullable=False) | ||
created_at = db.Column(db.DateTime, | ||
index=False, | ||
unique=False, | ||
nullable=False) | ||
|
||
def __repr__(self): | ||
return '<Account {}>'.format(self.name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
flask db upgrade | ||
|
||
gunicorn -c gunicorn.config.py wsgi:app |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from datetime import datetime as dt | ||
|
||
from flask import current_app as app, request, make_response | ||
|
||
from . import db | ||
from .database.models.account import Account | ||
|
||
|
||
@app.route('/', methods=['GET']) | ||
def hello_world(): | ||
return { | ||
'hello': 'world' | ||
} | ||
|
||
|
||
@app.route('/accounts/', methods=['POST']) | ||
def create_user(): | ||
"""Create an account.""" | ||
data = request.get_json() | ||
name = data['name'] | ||
if name: | ||
new_account = Account(name=name, | ||
created_at=dt.now()) | ||
db.session.add(new_account) # Adds new User record to database | ||
db.session.commit() # Commits all changes | ||
return make_response(f"{new_account} successfully created!") | ||
else: | ||
return make_response(f"Name can't be null!") | ||
|
||
|
||
# @app.route('/products/<barcode>/', methods=['GET']) | ||
# def products(barcode): | ||
# return openfoodfacts.products.get_product(barcode) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Flask config class.""" | ||
import os | ||
|
||
|
||
class Config: | ||
"""Set Flask configuration vars.""" | ||
|
||
# General Config | ||
TESTING = True | ||
DEBUG = True | ||
SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/' | ||
SESSION_COOKIE_NAME = 'my_cookie' | ||
|
||
# Database | ||
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI', | ||
'postgresql+psycopg2://test:[email protected]:5401/test') | ||
SQLALCHEMY_USERNAME = 'test' | ||
SQLALCHEMY_PASSWORD = 'test' | ||
SQLALCHEMY_DATABASE_NAME = 'test' | ||
SQLALCHEMY_TABLE = 'migrations' | ||
SQLALCHEMY_DB_SCHEMA = 'public' | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
|
||
# AWS | ||
AWS_SECRET_KEY = 'OBIVU5BIG$%&*IRTU#GV&^UHACKEDYGFTI7U5EGEWRG' | ||
AWS_KEY_ID = 'B&*D%F^&IYGUIFU' | ||
|
||
# My API | ||
API_ENDPOINT = 'http://unsecureendpoint.com/' | ||
API_ACCESS_TOKEN = 'HBV%^&UDFIUGFYGJHVIFUJ' | ||
API_CLIENT_ID = '3857463' | ||
|
||
# Test User Details | ||
TEST_USER = 'Johnny "My Real Name" Boi' | ||
TEST_PASSWORD = 'myactualpassword' | ||
TEST_SOCIAL_SECURITY_NUMBER = '420-69-1738' | ||
TEST_SECURITY_QUESTION = 'Main Street, USA' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: '3.6' | ||
|
||
services: | ||
api: | ||
build: . | ||
depends_on: | ||
- db | ||
environment: | ||
STAGE: test | ||
SQLALCHEMY_DATABASE_URI: postgresql+psycopg2://test:test@db/test | ||
networks: | ||
- default | ||
ports: | ||
- 5000:5000 | ||
volumes: | ||
- ./app:/usr/src/app/app | ||
- ./migrations:/usr/src/app/migrations | ||
restart: always | ||
|
||
db: | ||
environment: | ||
POSTGRES_USER: test | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: test | ||
image: postgres:latest | ||
networks: | ||
- default | ||
ports: | ||
- 5405:5432 | ||
restart: always | ||
volumes: | ||
- ./postgres-data:/var/lib/postgresql/data |