def init_logs(): """ Initializes logging module, creating missing log files if needed """ global _LOGS_ global _LOG_PERF_ global _LOG_DIR_ try: _LOG_DIR_ = ini.config('LOGGER', 'log_dir', 'OPENSHIFT_LOG_DIR', default='logs/') _LOGS_ = { 'stdout': ini.config('LOGGER', 'std_log', default='mapif.log'), 'perfout': ini.config('LOGGER', 'perf_log', default='mapif.perf.log'), 'stderr': ini.config('LOGGER', 'err_log', default='mapif.err.log') } _LOG_PERF_ = ini.config('LOGGER', 'log_perf', default=False, boolean=True) for logfile in _LOGS_.values(): _create_log(logfile) mprint("Logging module successfully initialized.") except Exception as e: mprint("Logging module initialization failed.") log_error("Logging module initialization failed.", e)
def _get_complete_database_name(database): url = None if ini.config('DB', 'engine') == 'postgre': if ini.getenv('OPENSHIFT_POSTGRESQL_DB_URL'): url = ini.getenv('OPENSHIFT_POSTGRESQL_DB_URL') else: url = "postgresql://{0}:{1}@{2}/{3}".format( ini.config('DB', 'postgre_user'), ini.config('DB', 'postgre_pass'), ini.config('DB', 'postgre_host'), database) elif ini.config('DB', 'engine') == 'sqlite': url = "sqlite:///database/{0}.sqlite".format(database) return url
def init_logs(): """ Initializes logging module, creating missing log files if needed """ global _LOGS_ global _LOG_PERF_ global _LOG_DIR_ try: _LOG_DIR_ = ini.config('LOGGER', 'log_dir', 'OPENSHIFT_LOG_DIR', default='logs/') _LOGS_ = { 'stdout': ini.config('LOGGER', 'std_log', default='mapif.log'), 'perfout': ini.config('LOGGER', 'perf_log', default='mapif.perf.log'), 'stderr': ini.config('LOGGER', 'err_log', default='mapif.err.log') } _LOG_PERF_ = ini.config('LOGGER','log_perf', default=False, boolean=True) for logfile in _LOGS_.values(): _create_log(logfile) mprint("Logging module successfully initialized.") except Exception as e: mprint("Logging module initialization failed.") log_error("Logging module initialization failed.", e)
def check_captcha(request): """ Google ReCaptcha validation process """ payload = { 'secret': ini.config('RECAPTCHA','recaptcha_secret_key'), 'response': request.form['g-recaptcha-response'], 'remoteip': request.remote_addr } resp = requests.post('https://www.google.com/recaptcha/api/siteverify', params=payload) data = json.loads(resp.text) if not data['success']: logger.log_trace("from validator: {0} may be a bot !".format(request.remote_addr)) return data['success']
def _database_op(dbname, action='create'): """ DEPRECATED """ engine = create_engine(_get_complete_database_name(dbname)) if ini.config('DB', 'engine') == 'postgre': connection = engine.connect() connection.execute('commit') try: if action == 'create': if not database_exists(engine.url): connection.execute('CREATE DATABASE "{0}"'.format(dbname)) elif action == 'drop': connection.execute('DROP DATABASE "{0}"'.format(dbname)) except Exception as e: logger.log_error('_database_op error: details below.', e) connection.close() else: # default is sqlite if action == 'create': if not database_exists(engine.url): create_database(engine.url) elif action == 'drop': remove("database/{0}.sqlite".format(dbname))
def _get_default_database_name(): return _get_complete_database_name(ini.config('DB', 'db_name'))
def _check_authorized(form): config_key = ini.config('USER', 'api_key', default=None) if config_key is not None and config_key == form['api_key']: return True return False
logger.mprint("Loading configuration file...") _APP_ROOT_=ini.getenv('OPENSHIFT_REPO_DIR', '') if not ini.init_config(_APP_ROOT_+'mapif.ini'): logger.mprint("Configuration file is missing. Server can't be started !") exit(-1) # initialize logging module logger.mprint("Starting logging module...") logger.init_logs() # initialize DB module logger.mprint("Starting DB module...") db.init_db() # set locale locale.setlocale(locale.LC_ALL, ini.config('APP', 'locale', default='fr_FR.UTF-8')) # create our little application :) app = Flask(__name__) # check Configuration section for more details app.config.from_object(__name__) # load secret key from configuration file or generated a UUID and use it as secret key app.secret_key = ini.config('APP', 'secret_key', default=str(uuid.uuid4())) # enable/disable debug app.debug = ini.config('APP', 'debug', default=False, boolean=True) if not app.debug: logger.mprint("Debugger is disabled !") else:
_APP_ROOT_ = ini.getenv('OPENSHIFT_REPO_DIR', '') if not ini.init_config(_APP_ROOT_ + 'mapif.ini'): logger.mprint("Configuration file is missing. Server can't be started !") exit(-1) # initialize logging module logger.mprint("Starting logging module...") logger.init_logs() # initialize DB module logger.mprint("Starting DB module...") db.init_db() # set locale locale.setlocale(locale.LC_ALL, ini.config('APP', 'locale', default='fr_FR.UTF-8')) # create our little application :) app = Flask(__name__) # check Configuration section for more details app.config.from_object(__name__) # load secret key from configuration file or generated a UUID and use it as secret key app.secret_key = ini.config('APP', 'secret_key', default=str(uuid.uuid4())) # enable/disable debug app.debug = ini.config('APP', 'debug', default=False, boolean=True) if not app.debug: logger.mprint("Debugger is disabled !") else: