瀏覽代碼

Release v1.0.13

App Generator 2 年之前
父節點
當前提交
926fcc4bf2
共有 10 個文件被更改,包括 104 次插入38 次删除
  1. 7 0
      CHANGELOG.md
  2. 1 1
      Dockerfile
  3. 1 1
      README.md
  4. 12 1
      apps/__init__.py
  5. 0 1
      apps/authentication/oauth.py
  6. 41 16
      apps/config.py
  7. 7 0
      build.sh
  8. 13 0
      render.yaml
  9. 18 15
      requirements.txt
  10. 4 3
      run.py

+ 7 - 0
CHANGELOG.md

@@ -1,5 +1,12 @@
 # Change Log
 
+## [1.0.13] 2023-10-07
+### Changes
+
+- Update Dependencies
+- Silent fallback to SQLite
+- CI/CD for Render
+
 ## [1.0.12] 2023-05-03
 ### Changes
 

+ 1 - 1
Dockerfile

@@ -1,4 +1,4 @@
-FROM python:3.9
+FROM python:3.10
 
 # set environment variables
 ENV PYTHONDONTWRITEBYTECODE 1

+ 1 - 1
README.md

@@ -41,7 +41,7 @@ Visit `http://localhost:5085` in your browser. The app should be up & running.
 
 <br />
 
-## Create/Edit `.env` file
+## Create/Edit `.env` file
 
 The meaning of each variable can be found below: 
 

+ 12 - 1
apps/__init__.py

@@ -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):

+ 0 - 1
apps/authentication/oauth.py

@@ -9,7 +9,6 @@ from flask_login import current_user, login_user
 from flask_dance.consumer import oauth_authorized
 from flask_dance.contrib.github import github, make_github_blueprint
 from flask_dance.consumer.storage.sqla import SQLAlchemyStorage
-from flask_dance.contrib.twitter import twitter, make_twitter_blueprint
 from sqlalchemy.orm.exc import NoResultFound
 from apps.config import Config
 from .models import Users, db, OAuth

+ 41 - 16
apps/config.py

@@ -3,19 +3,54 @@
 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')
+    SECRET_KEY  = os.getenv('SECRET_KEY', None)
+    if not SECRET_KEY:
+        SECRET_KEY = ''.join(random.choice( string.ascii_lowercase  ) for i in range( 32 ))    
 
-    # This will create a file in <app> FOLDER
-    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
-    SQLALCHEMY_TRACK_MODIFICATIONS = False 
+    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') 
 
     # Assets Management
     ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')    
@@ -37,16 +72,6 @@ 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

+ 7 - 0
build.sh

@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+# exit on error
+set -o errexit
+
+python -m pip install --upgrade pip
+
+pip install -r requirements.txt

+ 13 - 0
render.yaml

@@ -0,0 +1,13 @@
+services:
+  - type: web
+    name: flask-black
+    plan: starter
+    env: python
+    region: frankfurt  # region should be same as your database region.
+    buildCommand: "./build.sh"
+    startCommand: "gunicorn run:app"
+    envVars:
+      - key: SECRET_KEY
+        generateValue: true
+      - key: WEB_CONCURRENCY
+        value: 4

+ 18 - 15
requirements.txt

@@ -1,18 +1,21 @@
-flask==2.0.2
-flask_login==0.5.0
-flask_migrate==3.1.0
+flask==2.2.5
+Werkzeug==2.3.7
+jinja2==3.1.2
+flask-login==0.6.2
+flask_migrate==4.0.4
 WTForms==3.0.1
-flask_wtf==1.0.0
-flask_sqlalchemy==2.5.1
-sqlalchemy==1.4.29
-email_validator==1.1.3
-gunicorn==20.1.0
-jinja2==3.0.3
-flask-restx==0.5.1
-Werkzeug==2.0.3
+flask_wtf==1.2.1
+flask-sqlalchemy==3.0.5
+sqlalchemy==2.0.21
+email_validator==2.0.0
+flask-restx==1.1.0
+
 python-dotenv==0.19.2
+
+gunicorn==20.1.0
 Flask-Minify==0.37
-Flask-Dance==5.1.0
-blinker==1.4
-pyOpenSSL
-# flask_mysqldb
+
+flask-dance==7.0.0
+blinker==1.6.2
+d:\work\repo-free\flask-adminator\render.yaml d:\work\repo-free\flask-adminator\build.sh
+

+ 4 - 3
run.py

@@ -32,9 +32,10 @@ if not DEBUG:
     Minify(app=app, html=True, js=False, cssless=False)
     
 if DEBUG:
-    app.logger.info('DEBUG       = ' + str(DEBUG)             )
-    app.logger.info('DBMS        = ' + app_config.SQLALCHEMY_DATABASE_URI)
-    app.logger.info('ASSETS_ROOT = ' + app_config.ASSETS_ROOT )
+    app.logger.info('DEBUG            = ' + str(DEBUG)             )
+    app.logger.info('Page Compression = ' + 'FALSE' if DEBUG else 'TRUE' )
+    app.logger.info('DBMS             = ' + app_config.SQLALCHEMY_DATABASE_URI)
+    app.logger.info('ASSETS_ROOT      = ' + app_config.ASSETS_ROOT )
 
 if __name__ == "__main__":
     app.run()