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

"""API server, with a web client for free"""

# System modules
import time
import logging
import sys

# Third-party modules
from flask import Flask, request
#from flask_cors import CORS
from werkzeug.exceptions import HTTPException

# Custom modules
import api

app = Flask(__name__)
app.register_blueprint(api.api_blueprint, url_prefix='/api')
app.threaded = True
#cors = CORS(app)

if __name__ == "__main__":

    with_web = True
    try:
        arg = sys.argv[1]
        if arg.startswith("--noweb"):
            with_web = False
            port = 5001
        else:
            port = arg
    except IndexError as e:
        port = 5001

    hostname = "0.0.0.0"
    # hostname = socket.getfqdn()

    print("hostname {}".format(hostname))
    
    if with_web:
        from web.server import instance
        socketio = instance.enable(app)
        socketio.run(app, host=hostname, port=port)
    else:
        app.run(host=hostname, port=port)
