Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ytimocin committed Feb 14, 2020
0 parents commit b466508
Show file tree
Hide file tree
Showing 1,325 changed files with 1,506 additions and 0 deletions.
Empty file added .env
Empty file.
14 changes: 14 additions & 0 deletions .gitignore
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/
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/flask-postgres-server.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions .idea/markdown-navigator-enh.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions .idea/markdown-navigator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Dockerfile
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"]
35 changes: 35 additions & 0 deletions README.md
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
39 changes: 39 additions & 0 deletions app/__init__.py
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
Empty file added app/database/__init__.py
Empty file.
Empty file added app/database/models/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions app/database/models/account.py
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)
7 changes: 7 additions & 0 deletions app/docker-entrypoint.sh
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
33 changes: 33 additions & 0 deletions app/routes.py
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)
37 changes: 37 additions & 0 deletions config.py
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'
32 changes: 32 additions & 0 deletions docker-compose.yml
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
Loading

0 comments on commit b466508

Please sign in to comment.