Skip to content

Commit

Permalink
Release v1.0.9 - DB Management Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
app-generator committed Dec 31, 2022
1 parent f2d00a9 commit 8fe5164
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 38 deletions.
13 changes: 12 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ DEBUG=True
FLASK_APP=run.py
FLASK_ENV=development

# If not provided, a random one is generated
# SECRET_KEY=<YOUR_SUPER_KEY_HERE>

# Used for CDN (in production)
# No Slash at the end
ASSETS_ROOT=/static/assets

# LOCAL 5001 FLask
# If DB credentials (if NOT provided, or wrong values SQLite is used)
# DB_ENGINE=mysql
# DB_HOST=localhost
# DB_NAME=appseed_db
# DB_USERNAME=appseed_db_usr
# DB_PASS=pass
# DB_PORT=3306

# LOCAL 5001 Flask
# GITHUB_ID = <YOUR_GITHUB_ID>
# GITHUB_SECRET = <YOUR_GITHUB_SECRET>
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [1.0.9] 2022-12-31
### Changes

- `DB Management` Improvement
- `Silent fallback` to **SQLite**

## [1.0.8] 2022-09-07
### Improvements

Expand Down
13 changes: 12 additions & 1 deletion apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ def configure_database(app):

@app.before_first_request
def initialize_database():
db.create_all()
try:
db.create_all()
except Exception as e:

print('> Error: DBMS Exception: ' + str(e) )

# fallback to SQLite
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')

print('> Fallback to SQLite ')
db.create_all()

@app.teardown_request
def shutdown_session(exception=None):
Expand Down
76 changes: 50 additions & 26 deletions apps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,68 @@
Copyright (c) 2019 - present AppSeed.us
"""

import os
import os, random, string

class Config(object):

basedir = os.path.abspath(os.path.dirname(__file__))

# Set up the App SECRET_KEY
# SECRET_KEY = config('SECRET_KEY' , default='S#perS3crEt_007')
SECRET_KEY = os.getenv('SECRET_KEY', 'S#perS3crEt_007')

# This will create a file in <app> FOLDER
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
SQLALCHEMY_TRACK_MODIFICATIONS = False

# Assets Management
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')

# Set up the App SECRET_KEY
SECRET_KEY = os.getenv('SECRET_KEY', None)
if not SECRET_KEY:
SECRET_KEY = ''.join(random.choice( string.ascii_lowercase ) for i in range( 32 ))

# Social AUTH context
SOCIAL_AUTH_GITHUB = False

GITHUB_ID = os.getenv('GITHUB_ID')
GITHUB_SECRET = os.getenv('GITHUB_SECRET')
GITHUB_ID = os.getenv('GITHUB_ID' , None)
GITHUB_SECRET = os.getenv('GITHUB_SECRET', None)

# Enable/Disable Github Social Login
if GITHUB_ID and GITHUB_SECRET:
SOCIAL_AUTH_GITHUB = True

SOCIAL_AUTH_GITHUB = True

SQLALCHEMY_TRACK_MODIFICATIONS = False

DB_ENGINE = os.getenv('DB_ENGINE' , None)
DB_USERNAME = os.getenv('DB_USERNAME' , None)
DB_PASS = os.getenv('DB_PASS' , None)
DB_HOST = os.getenv('DB_HOST' , None)
DB_PORT = os.getenv('DB_PORT' , None)
DB_NAME = os.getenv('DB_NAME' , None)

USE_SQLITE = True

# try to set up a Relational DBMS
if DB_ENGINE and DB_NAME and DB_USERNAME:

try:

# Relational DBMS: PSQL, MySql
SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
DB_ENGINE,
DB_USERNAME,
DB_PASS,
DB_HOST,
DB_PORT,
DB_NAME
)

USE_SQLITE = False

except Exception as e:

print('> Error: DBMS Exception: ' + str(e) )
print('> Fallback to SQLite ')

if USE_SQLITE:

# This will create a file in <app> FOLDER
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')

class ProductionConfig(Config):
DEBUG = False

Expand All @@ -37,21 +73,9 @@ class ProductionConfig(Config):
REMEMBER_COOKIE_HTTPONLY = True
REMEMBER_COOKIE_DURATION = 3600

# PostgreSQL database
SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
os.getenv('DB_ENGINE' , 'mysql'),
os.getenv('DB_USERNAME' , 'appseed_db_usr'),
os.getenv('DB_PASS' , 'pass'),
os.getenv('DB_HOST' , 'localhost'),
os.getenv('DB_PORT' , 3306),
os.getenv('DB_NAME' , 'appseed_db')
)


class DebugConfig(Config):
DEBUG = True


# Load all possible configurations
config_dict = {
'Production': ProductionConfig,
Expand Down
22 changes: 12 additions & 10 deletions env.sample
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# True for development, False for production
# True in development, False in production
DEBUG=True

# Flask ENV
FLASK_APP=run.py
SECRET_KEY=YOUR_SUPER_KEY
FLASK_ENV=development

# Used for CDN (in production)
# No Slash at the end
ASSETS_ROOT=/static/assets
# If not provided, a random one is generated
# SECRET_KEY=<YOUR_SUPER_KEY_HERE>

# If DEBUG=False (production mode)
# If DB credentials (if NOT provided, or wrong values SQLite is used)
# DB_ENGINE=mysql
# DB_NAME=appseed_db
# DB_HOST=localhost
# DB_PORT=3306
# DB_NAME=appseed_db
# DB_USERNAME=appseed_db_usr
# DB_PASS=<STRONG_PASS>
# DB_PASS=pass
# DB_PORT=3306

# Used for CDN (in production)
# No Slash at the end
ASSETS_ROOT=/static/assets

# SOCIAL AUTH Github
# GITHUB_ID=YOUR_GITHUB_ID
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ blinker==1.4
pyOpenSSL

# flask_mysqldb
# psycopg2-binary

0 comments on commit 8fe5164

Please sign in to comment.