Skip to content

Commit

Permalink
moving to GAE standard, renamed to breadapp to app, renamed breadshee…
Browse files Browse the repository at this point in the history
…t.py to main.py
  • Loading branch information
Thomas committed May 7, 2019
1 parent ccb43e3 commit f73f773
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 97 deletions.
4 changes: 2 additions & 2 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or
# files from your .gitignore file, remove the corresponding line below:
# If you would like to upload your .git directory, .gitignore file, or files from your .gitignore file,
# then remove the corresponding line below:
.git
.gitignore
#!include:.gitignore
9 changes: 5 additions & 4 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# https://yamlchecker.com/
runtime: python
env: flex
runtime: python37

instance_class: F1
# instance_class: F1
# defaults to F1 anyway

entrypoint: gunicorn -w 1 --timeout 60 breadsheet:breadapp
# entrypoint: gunicorn -w 1 --timeout 60 main:app
# no longer needed since renaming breadsheet.py --> main.py, and renaming 'breadapp' --> 'app'

error_handlers:
- file: app/templates/errors/default_error.html
Expand Down
39 changes: 22 additions & 17 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
# initialization file that defines the app as breadapp
# initialization file that defines items within the app
from config import Config
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
import logging as logging_util
from os import path


bootstrap = Bootstrap()
db = SQLAlchemy()
moment = Moment()

basedir = path.abspath(path.dirname(__file__))
local = '/documents/dev/' in basedir.lower()


def create_app(config_class=Config):
# initialize logging
logfile = 'logs/{}.log'.format(__file__)
logging_util.basicConfig(filename=logfile, filemode='w', level=logging_util.DEBUG, datefmt='%H:%M:%S',
log_dir = 'logs' if local else 'tmp'
log_file = '{dir}/syslog.log'.format(dir=log_dir)
logging_util.basicConfig(filename=log_file, level=logging_util.DEBUG, datefmt='%H:%M:%S',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging_util.getLogger(__name__)
logger.info("Logging initialized")
logger.info("Logging initialized from the /app/__init__.py file")

breadapp = Flask(__name__)
breadapp.config.from_object(config_class)
app = Flask(__name__)
app.config.from_object(config_class)

bootstrap.init_app(breadapp)
db.init_app(breadapp)
moment.init_app(breadapp)
bootstrap.init_app(app)
db.init_app(app)
moment.init_app(app)

from app.errors import bp as errors_bp
breadapp.register_blueprint(errors_bp)
app.register_blueprint(errors_bp)

from app.main import bp as main_bp
breadapp.register_blueprint(main_bp)
app.register_blueprint(main_bp)

from app.convert import bp as convert_bp
breadapp.register_blueprint(convert_bp)
app.register_blueprint(convert_bp)

# log errors when running in production mode
"""if not breadapp.debug and not breadapp.testing:
"""if not app.debug and not app.testing:
pass
if not os.path.exists('logs'):
os.mkdir('logs')
Expand All @@ -46,12 +51,12 @@ def create_app(config_class=Config):
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'))
file_handler.setLevel(logging.INFO)
breadapp.logger.addHandler(file_handler)
app.logger.addHandler(file_handler)
breadapp.logger.setLevel(logging.INFO)
breadapp.logger.info('Breadsheet startup')"""
app.logger.setLevel(logging.INFO)
app.logger.info('Breadsheet startup')"""

return breadapp
return app


from app import models
48 changes: 0 additions & 48 deletions breadsheet.py

This file was deleted.

50 changes: 29 additions & 21 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import logging
import os
import logging as logging_util
from os import path, environ
from google.cloud import firestore

basedir = os.path.abspath(os.path.dirname(__file__))
basedir = path.abspath(path.dirname(__file__))
local = '/documents/dev/' in basedir.lower()
print("local = {}".format(local))
print("__file__ = {}".format(__file__))

# initialize logging
log_dir = 'logs' if local else 'tmp'
log_file = '{dir}/syslog.log'.format(dir=log_dir)
logging_util.basicConfig(filename=log_file, level=logging_util.DEBUG, datefmt='%H:%M:%S',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger_config = logging_util.getLogger(__name__)
logger_config.info("Logging initialized from the /config.py file")


class Config(object):
# local = True
# another line for later use
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')
logging.debug("local = {}".format(local))
logging.debug("basedir: {}".format(basedir))
logger_config.debug("local = {}".format(local))
logger_config.debug("basedir: {}".format(basedir))
print("local = {}".format(local))
print("basedir: {}".format(basedir))

if local:
from dotenv import load_dotenv
load_dotenv(os.path.join(basedir, '.env'))
load_dotenv(path.join(basedir, '.env'))

logging.debug("Readme file exists? {}".format(os.path.isfile('README.md')))
print("Readme file exists? {}".format(os.path.isfile('README.md')))
logger_config.debug("Readme file exists? {}".format(path.isfile('README.md')))
print("Readme file exists? {}".format(path.isfile('README.md')))

SECRET_KEY = os.environ.get('SECRET_KEY') or '1mW7@LN0n32L6ntaj0d8jzsXiAW4mkPL7u5l'
BUCKET_NAME = os.environ.get('GCP_BUCKET_NAME') or 'local_bucketname'
db_user = os.environ.get('GCP_CLOUDSQL_USER')
db_pw = os.environ.get('GCP_CLOUDSQL_PW')
db_name = os.environ.get('GCP_CLOUDSQL_DBNAME')
db_ip = os.environ.get('GCP_CLOUDSQL_IP')
db_port = os.environ.get('GCP_CLOUDSQL_PORT')
db_instance = os.environ.get('GCP_CLOUDSQL_INSTANCE')
SECRET_KEY = environ.get('SECRET_KEY') or '1mW7@LN0n32L6ntaj0d8jzsXiAW4mkPL7u5l'
BUCKET_NAME = environ.get('GCP_BUCKET_NAME') or 'local_bucketname'
db_user = environ.get('GCP_CLOUDSQL_USER')
db_pw = environ.get('GCP_CLOUDSQL_PW')
db_name = environ.get('GCP_CLOUDSQL_DBNAME')
db_ip = environ.get('GCP_CLOUDSQL_IP')
db_port = environ.get('GCP_CLOUDSQL_PORT')
db_instance = environ.get('GCP_CLOUDSQL_INSTANCE')

else:
logging.debug("JSON file exists? {}".format(os.path.isfile('breadsheet-prod.json')))
print("JSON file exists? {}".format(os.path.isfile('breadsheet-prod.json')))
logger_config.debug("JSON file exists? {}".format(path.isfile('breadsheet-prod.json')))
print("JSON file exists? {}".format(path.isfile('breadsheet-prod.json')))

# supplying the private (prod) key to explicitly use creds for the default service acct
fire = firestore.Client().from_service_account_json('breadsheet-prod.json')
Expand All @@ -60,10 +68,10 @@ class Config(object):
SQLALCHEMY_DATABASE_URI = db_url
# SQLALCHEMY_DATABASE_URI = 'postgres+psycopg2://USER:PW@/breadsheet?host=/cloudsql/breadsheet:us-west1:breadsheet'

logging.debug("SQLALCHEMY_DATABASE_URI = {}".format(db_url))
logger_config.debug("SQLALCHEMY_DATABASE_URI = {}".format(db_url))
print("SQLALCHEMY_DATABASE_URI = {}".format(db_url))

logging.debug("BUCKET_NAME = {}".format(BUCKET_NAME))
logger_config.debug("BUCKET_NAME = {}".format(BUCKET_NAME))
print("BUCKET_NAME = {}".format(BUCKET_NAME))

# silence the madness
Expand Down
54 changes: 54 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# script for Flask to obtain our application instance, named 'app'
from app import create_app, db
from app.models import Recipe, Step
import logging as logging_util
from os import path, environ

basedir = path.abspath(path.dirname(__file__))
local = '/documents/dev/' in basedir.lower()

# initialize logging
log_dir = 'logs' if local else 'tmp'
log_file = '{dir}/syslog.log'.format(dir=log_dir)
logging_util.basicConfig(filename=log_file, level=logging_util.DEBUG, datefmt='%H:%M:%S',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger_main = logging_util.getLogger(__name__)


try:
import googleclouddebugger
googleclouddebugger.enable()

logger_main.info("Cloud Debugger initialized! \n")
print("Cloud Debugger initialized! \n")

except ImportError:
logger_main.info("Cloud Debugger import failed. \n")
print("Cloud Debugger import failed. \n")


try:
from dotenv import load_dotenv
load_dotenv(path.join(basedir, '.env'))
cred_path = environ.get('LOCAL_GCP_CREDENTIALS_PATH')
file = cred_path if local else 'breadsheet-prod.json'

# logging setup
import google.cloud.logging
client = google.cloud.logging.Client.from_service_account_json(file)
client.setup_logging() # attaches Stackdriver to python's standard logging module

logger_main.info("Logging to Stackdriver initialized! \n")
print("Logging to Stackdriver initialized! [from print()] \n")

# except OSError:
except ImportError:
logger_main.info("Logging to Stackdriver failed. \n")
print("Logging to Stackdriver failed. \n")

app = create_app()


@app.shell_context_processor
def make_shell_context():
return {'db': db, 'Recipe': Recipe, 'Step': Step}
9 changes: 4 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ez-setup==0.9
Flask==1.0.2
Flask-Bootstrap==3.3.7.1
Flask-Moment==0.7.0
Flask-SQLAlchemy==2.3.2
Flask-SQLAlchemy==2.4.0
Flask-WTF==0.14.2
google-api-core==1.9.0
google-auth==1.6.3
Expand All @@ -24,7 +24,6 @@ google-cloud-storage==1.14.0
google-resumable-media==0.3.2
googleapis-common-protos==1.5.9
grpcio==1.19.0
gunicorn==19.9.0
httplib2==0.12.1
idna==2.8
itsdangerous==1.1.0
Expand All @@ -47,15 +46,15 @@ python-dateutil==2.8.0
python-dotenv==0.10.1
python-editor==1.0.4
pytz==2018.9
regex==2019.3.9
regex==2019.4.14
requests==2.21.0
rsa==4.0
six==1.12.0
SQLAlchemy==1.3.3
times==0.7
tzlocal==1.5.1
uritemplate==3.0.0
urllib3==1.24.2
urllib3==1.25.2
visitor==0.1.3
Werkzeug==0.14.1
Werkzeug==0.15.2
WTForms==2.2.1

0 comments on commit f73f773

Please sign in to comment.