aboutsummaryrefslogtreecommitdiff
path: root/src/bootstrap.py
blob: cf8991f03d7e69009d9c8ba9f24099714081fe56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
# -*- coding: utf-8 -

# required imports and code exection for basic functionning

import os
import conf
import logging
import flask_restless
from urllib.parse import urlsplit


def set_logging(log_path, log_level=logging.INFO,
                log_format='%(asctime)s %(levelname)s %(message)s'):
    formater = logging.Formatter(log_format)
    handler = logging.FileHandler(log_path)
    handler.setFormatter(formater)
    for logger_name in ('bootstrap', 'web', 'manager', 'runserver', 'classic_crawler'):
        logger = logging.getLogger(logger_name)
        logger.addHandler(handler)
        logger.setLevel(log_level)

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

# Create Flask application
application = Flask('web')
if conf.ON_HEROKU:
    from flask_sslify import SSLify
    SSLify(application, subdomains=True)
if os.environ.get('Newspipe_TESTING', False) == 'true':
    application.debug = logging.DEBUG
    application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
    application.config['TESTING'] = True
else:
    application.debug = conf.LOG_LEVEL <= logging.DEBUG
    application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    application.config['SQLALCHEMY_DATABASE_URI'] \
            = conf.SQLALCHEMY_DATABASE_URI

scheme, domain, _, _, _ = urlsplit(conf.PLATFORM_URL)
application.config['SERVER_NAME'] = domain
application.config['PREFERRED_URL_SCHEME'] = scheme

set_logging(conf.LOG_PATH, log_level=conf.LOG_LEVEL)

# Create secrey key so we can use sessions
application.config['SECRET_KEY'] = getattr(conf, 'WEBSERVER_SECRET', None)
if not application.config['SECRET_KEY']:
    application.config['SECRET_KEY'] = os.urandom(12)

application.config['SECURITY_PASSWORD_SALT'] = getattr(conf,
                                                'SECURITY_PASSWORD_SALT', None)
if not application.config['SECURITY_PASSWORD_SALT']:
    application.config['SECURITY_PASSWORD_SALT'] = os.urandom(12)

db = SQLAlchemy(application)

# Create the Flask-Restless API manager.
manager = flask_restless.APIManager(application, flask_sqlalchemy_db=db)


def populate_g():
    from flask import g
    g.db = db
    g.app = application
bgstack15