#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from flask import Blueprint
from flask import jsonify, session, redirect, url_for, send_file, abort

import config

import version

import utils

adm_view = Blueprint( "adm", __name__ )

@adm_view.route( "/ping" )
def ping():
    """
        Ping function to check if the web application is healthy.
    """
    if config.db.check():
        return "pong"
    else:
        return abort( 500 )

@adm_view.route( "/version" )
def route_version():
    """
        Function to report the version of the web app. The version.py file is
        re-generated by the CI/CD for production.
    """
    try:
        return jsonify( {
            "error": False,
            "version": version.__version__,
            "branch": version.__branch__,
            "commit": version.__commit__,
            "commiturl": version.__commiturl__,
            "treeurl": version.__treeurl__
        } )

    except:
        return jsonify( {
            "error": True
        } )

@adm_view.route( "/favicon.ico" )
def favicon():
    return send_file( "favicon.ico" )

