#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask, Response
from api import api_blueprint as api
from api import web_blueprint as web
# from utils.logger import log


app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api')
app.register_blueprint(web, url_prefix='/web')

app.template_folder = "./web/templates"
app.static_folder = "./web/static"

from flask import Response
import subprocess
import time

# def get_message():
#     '''this could be any function that blocks until data is ready'''
#     time.sleep(1.0)
#     s = time.ctime(time.time())
#     return s

# @app.route('/stream')
# def stream():
#     def eventStream():
#         while True:
#             # wait for source data to be available, then push it
#             yield 'data: {}\n\n'.format(get_message())
#     return Response(eventStream(), mimetype="text/event-stream")


# @app.route('/stream')
# def stream():
#     def inner():
#         proc = subprocess.Popen(
#             ['tail -f /var/log/syslog'],
#             shell=True,
#             stdout=subprocess.PIPE
#         )

#         for line in iter(proc.stdout.readline,''):
#             time.sleep(0.4) 
#             yield 'data: {}\n\n'.format(line.rstrip().decode())
#             #yield line.rstrip().decode() + '<br/>\n'
#     print("qui")
#     return Response(inner(), mimetype="text/event-stream")





if __name__ == "__main__":
    import sys
    import socket
    
    try:
        port = sys.argv[1]
    except IndexError:
        port = 5000
        
    hostname = socket.getfqdn()
    app.run(host=hostname, port=port, threaded=True, debug=True)
