mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
import logging
|
|
from logging.handlers import SMTPHandler, RotatingFileHandler
|
|
import os
|
|
from flask import Flask, request, current_app
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_migrate import Migrate
|
|
from flask_login import LoginManager
|
|
from flask_mail import Mail
|
|
from flask_moment import Moment
|
|
from flask_babel import Babel, lazy_gettext as _l
|
|
from config import Config
|
|
|
|
|
|
db = SQLAlchemy(session_options={"autoflush": False})
|
|
migrate = Migrate()
|
|
login = LoginManager()
|
|
login.login_view = 'auth.login'
|
|
login.login_message = _l('Please log in to access this page.')
|
|
mail = Mail()
|
|
moment = Moment()
|
|
babel = Babel()
|
|
|
|
|
|
def create_app(config_class=Config):
|
|
app = Flask(__name__)
|
|
app.config.from_object(config_class)
|
|
|
|
db.init_app(app)
|
|
migrate.init_app(app, db, render_as_batch=True)
|
|
login.init_app(app)
|
|
mail.init_app(app)
|
|
moment.init_app(app)
|
|
babel.init_app(app, locale_selector=get_locale)
|
|
|
|
from app.main import bp as main_bp
|
|
app.register_blueprint(main_bp)
|
|
|
|
from app.errors import bp as errors_bp
|
|
app.register_blueprint(errors_bp)
|
|
|
|
from app.admin import bp as admin_bp
|
|
app.register_blueprint(admin_bp, url_prefix='/admin')
|
|
|
|
from app.activitypub import bp as activitypub_bp
|
|
app.register_blueprint(activitypub_bp)
|
|
|
|
def get_resource_as_string(name, charset='utf-8'):
|
|
with app.open_resource(name) as f:
|
|
return f.read().decode(charset)
|
|
|
|
app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string
|
|
|
|
# send error reports via email
|
|
if app.config['MAIL_SERVER']:
|
|
auth = None
|
|
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
|
|
auth = (app.config['MAIL_USERNAME'],
|
|
app.config['MAIL_PASSWORD'])
|
|
secure = None
|
|
if app.config['MAIL_USE_TLS']:
|
|
secure = ()
|
|
mail_handler = SMTPHandler(
|
|
mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
|
|
fromaddr='rimu@chorebuster.net',
|
|
toaddrs=app.config['ADMINS'], subject='CB Failure',
|
|
credentials=auth, secure=secure)
|
|
mail_handler.setLevel(logging.ERROR)
|
|
app.logger.addHandler(mail_handler)
|
|
|
|
# log rotation
|
|
if not os.path.exists('logs'):
|
|
os.mkdir('logs')
|
|
file_handler = RotatingFileHandler('logs/pyfedi.log',
|
|
maxBytes=1002400, backupCount=15)
|
|
file_handler.setFormatter(logging.Formatter(
|
|
'%(asctime)s %(levelname)s: %(message)s '
|
|
'[in %(pathname)s:%(lineno)d]'))
|
|
file_handler.setLevel(logging.INFO)
|
|
app.logger.addHandler(file_handler)
|
|
|
|
app.logger.setLevel(logging.INFO)
|
|
app.logger.info('Started!') # let's go!
|
|
|
|
return app
|
|
|
|
|
|
def get_locale():
|
|
try:
|
|
return request.accept_languages.best_match(current_app.config['LANGUAGES'])
|
|
except:
|
|
return 'en_US'
|
|
|
|
|
|
from app import models
|