123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- import os
- import random
- import string
- import logging
- from datetime import timedelta
- from dotenv import load_dotenv
- if os.path.exists(".env"):
- load_dotenv(".env", verbose=True)
- class BaseConfig:
- """基础配置"""
- # persist_directory = os.environ.get('PERSIST_DIRECTORY')
- # CG_TOKEN = os.getenv("CHATGPT_TOKEN", "your-chatgpt-token")
- SUPERADMIN = "admin"
- SYSTEM_NAME = "Pear Admin"
- # 主题面板的链接列表配置
- SYSTEM_PANEL_LINKS = [
- {
- "icon": "layui-icon layui-icon-auz",
- "title": "官方网站",
- "href": "http://www.pearadmin.com",
- },
- {
- "icon": "layui-icon layui-icon-auz",
- "title": "开发文档",
- "href": "http://www.pearadmin.com",
- },
- {
- "icon": "layui-icon layui-icon-auz",
- "title": "开源地址",
- "href": "https://gitee.com/Jmysy/Pear-Admin-Layui",
- },
- ]
- ASSETS_ROOT = os.getenv("ASSETS_ROOT", "/static/assets")
- STATIC_URL = "/static/"
- SECRET_KEY = os.getenv("SECRET_KEY", None)
- if not SECRET_KEY:
- SECRET_KEY = "".join(random.choice(string.ascii_lowercase) for i in range(32))
- # base
- PROJECT_NAME = "flask_blog"
- # 当前 config.py 文件目录
- DEBUG = False
- # 当前文件所在目录的绝对路径 /apps
- BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
- BASE_URL = "http://"
- ADMIN_EMAILS = ""
- # 分页
- POST_PER_PAGE = 8 # 每页文章数
- COMMENT_PER_PAGE = 10 # 每页评论数
- LANGUAGE_CODE = "zh-hans"
- TIME_ZONE = "PRC"
- UP_DIR = os.path.join(
- os.path.abspath(os.path.dirname(__file__)), "app/static/uploads/"
- ) # 文件上传路径
- FC_DIR = os.path.join(
- os.path.abspath(os.path.dirname(__file__)), "app/static/uploads/users/"
- ) # 用户头像上传路径
- UPLOADED_PHOTOS_DEST = "static/upload"
- UPLOADED_FILES_ALLOW = ["gif", "jpg"]
- UPLOADS_AUTOSERVE = True
- JSON_AS_ASCII = False
- # db
- 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
- # MSSQL: f"mssql+pymssql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=cp936"
- # MySQL: f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4"
- # Oracle: f"oracle+cx_oracle://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}"
- # SQLite "sqlite:/// database.db"
- # Postgres f"postgresql+psycopg2://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}"
- # Oracle的第二种连接方式
- # dsnStr = cx_Oracle.makedsn({HOSTNAME}, 1521, service_name='orcl')
- # connect_str = "oracle://%s:%s@%s" % ('{USERNAME}', ' {PASSWORD}', dsnStr)
- # 在SQLALCHEMY_BINDS 中设置:'{数据库连接别名}': '{连接串}'
- # 最后在models的数据模型class中,在__tablename__前设置 __bind_key__ = '{数据库连接别名}' 即可,表示该数据模型不使用默认的数据库连接,改用“SQLALCHEMY_BINDS”中设置的其他数据库连接
- # SQLALCHEMY_BINDS = {
- # 'testMySQL': 'mysql+pymysql://test:123456@192.168.1.1:3306/test?charset=utf8',
- # 'testMsSQL': 'mssql+pymssql://test:123456@192.168.1.1:1433/test?charset=cp936',
- # 'testOracle': 'oracle+cx_oracle://test:123456@192.168.1.1:1521/test',
- # 'testSQLite': 'sqlite:///database.db
- # }
- # 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')
- # redis
- REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
- REDIS_PORT = os.getenv("REDIS_PORT", 6379)
- REDIS_DB = os.getenv("REDIS_DB", 0)
- REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", None)
- CELERY_BROKER_URL = "redis://{}:{}/{}".format(REDIS_HOST, REDIS_PORT, REDIS_DB)
- # mail
- MAIL_SERVER = os.getenv("MAIL_SERVER", "smtp.qq.com")
- MAIL_PORT = os.getenv("MAIL_PORT", 465)
- MAIL_USE_TLS = False
- MAIL_USE_SSL = True
- MAIL_DEBUG = True
- MAIL_USERNAME = os.getenv("MAIL_USERNAME", None)
- MAIL_PASSWORD = os.getenv("MAIL_PASSWORD", None) # 生成的授权码
- MAIL_DEFAULT_SENDER = MAIL_USERNAME
- # token配置
- TIMEOUT_TOKEN = 10 * 24 * 60 * 60
- TIMEOUT_EMAIL = 10 * 60
- PERMANENT_SESSION_LIFETIME = timedelta(hours=2)
- # log
- LOG_LEVEL = logging.WARN
- # 插件配置,填写插件的文件名名称,默认不启用插件。
- PLUGIN_ENABLE_FOLDERS = []
- # 七牛云
- QINIU_ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY", None)
- QINIU_SECRET_KEY = os.getenv("QINIU_SECRET_KEY", None)
- QINIU_BUCKET_NAME = os.getenv("QINIU_BUCKET_NAME", None)
- # github
- GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID", None)
- GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET", None)
- if GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET:
- SOCIAL_AUTH_GITHUB = True
- @staticmethod
- def init_app(app):
- pass
- class DevelopmentConfig(BaseConfig):
- """开发环境配置, 开启调试模式, 使用 sqlite"""
- DEBUG = True
- SQLALCHEMY_TRACK_MODIFICATIONS = True
- # SQLALCHEMY_ECHO = True
- # SQLALCHEMY_COMMIT_ON_TEARDOWN = True
- SQLALCHEMY_DATABASE_URI = "sqlite:///../flask-note.sqlite3.db"
- class TestingConfig(BaseConfig):
- DEBUG = True
- class ProductionConfig(BaseConfig):
- DEBUG = False
- SQLALCHEMY_DATABASE_URI = (
- "mysql+pymysql://lyq:123456@ok.yoqi.me:13306/travel?charset=utf8"
- )
- # 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)
- # Security
- SESSION_COOKIE_HTTPONLY = True
- REMEMBER_COOKIE_HTTPONLY = True
- REMEMBER_COOKIE_DURATION = 3600
- config = {
- "default": DevelopmentConfig,
- "development": DevelopmentConfig, # 开发环境配置, 开启调试模式, 使用 sqlite
- # "testing": TestingConfig,
- "production": ProductionConfig,
- }
|