import datetime
import os
from datetime import datetime
import requests
from flask import Flask, render_template, request, Markup, jsonify, send_from_directory, Blueprint, current_app
from isitdown.repository import Pings, PingsRepository, db

logger = None


def init_db(app):
    db.init_app(app)
    with app.app_context():
        db.create_all()
    return db


def create_app(DATABASE_URI=None):
    if type(DATABASE_URI) != str:
        print("Error: Database URI is:" + str(DATABASE_URI))
        return None
    app = Flask(__name__)

    app.config['SQLALCHEMY_DATABASE_URI'] = str(DATABASE_URI)
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    #app.config['SQLALCHEMY_ECHO'] = True
    db = init_db(app)
    app.register_blueprint(bp)

    return app


bp = Blueprint('index', __name__, static_folder="isitdown/static", template_folder="isitdown/templates")


@bp.route("/api/<string:host>")
def jsonCheck(host=""):
    return jsonify(isitdown=PingsRepository.wasDownOneMinuteAgo(host) and doPing(host))


# Some static files:
@bp.route("/favicon.ico")
@bp.route("/robots.txt")
@bp.route("/sitemap.xml")
@bp.route("/humans.txt")
def getRobots():
    return send_from_directory(bp.static_folder, request.path[1:])


@bp.route("/")
@bp.route("/<string:host>")
def check(host=""):
    lastPingList = PingsRepository.getLastPings()
    if len(host) == 0:
        return render_template("index.html", last=lastPingList)
    return render_template("check.html", pingres=PingsRepository.wasDownOneMinuteAgo(host) and doPing(host), host=host, last=lastPingList)


@bp.errorhandler(404)
def page_not_found(error):
    logger.error(error)
    return render_template('404.html'), 404


def doPing(host, prefix="https://"):
    '''
    @:returns true, if host is down
    '''
    httpHost = prefix + host
    isDown = True
    response_code = -1
    current_app.logger.debug("Sending head request to:" + httpHost)

    try:
        resp = requests.head(httpHost, timeout=2, stream=True, allow_redirects=True)
        # If we come here, we had a response. So the site is up:
        isDown = False
        response_code = resp.status_code
    except Exception as e:
        if "Name or service not known" in repr(e):
            return True
        current_app.logger.error("Exception while contacting {}. Exception: {} ".format(host, e))

        # Check both https and http:
        if "Connection refused" in repr(e):
            return doPing(host, "http://")

    # ip_addr = socket.gethostbyname(host) uh-uh

    p = Pings(from_ip=request.access_route[-1], host=Markup(host),time_stamp=datetime.utcnow(), isdown=isDown, response_code=response_code)
    PingsRepository.addPing(p)
    return isDown


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    if "DATABASE_URI" not in os.environ:
        print("Error: missing DATABASE_URI environment variable.")
        import sys
        sys.exit(-1)
    app = create_app(DATABASE_URI=os.environ["DATABASE_URI"])
    logger = app.logger
    app.run(host='0.0.0.0', port=port)

@@ -19,16 +19,26 @@
 port os%0A
+import re%0A
 from dat
@@ -938,24 +938,93 @@
 k(host=%22%22):%0A
+    if not isValidHost(host):%0A        return jsonify(isitdown=False)%0A
     return j
@@ -1306,24 +1306,178 @@
 path%5B1:%5D)%0A%0A%0A
+def isValidHost(host):%0A    regex = r%22(http:%5C/%5C/)*(https:%5C/%5C/)*(%5Ba-zA-Z%5D+%5C.)+(%5Ba-zA-Z%5D)+%22%0A    pattern = re.compile(regex)%0A    return pattern.match(host)%0A%0A%0A
 @bp.route(%22/
@@ -2005,24 +2005,24 @@
 is down%0A
-
     '''%0A
     http
@@ -2009,24 +2009,75 @@
 own%0A    '''%0A
+    if not isValidHost(host):%0A        return True%0A%0A
     httpHost

Check if an host is valid before sending ping