entrypoint.py 2.17 KB
Newer Older
1
# Copyright (c) 2016, Daniele Venzano
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

18
from flask import Flask
19 20
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
21
from tornado.ioloop import IOLoop, PeriodicCallback
22

23
import zoe_lib.config as config
24 25 26 27
import zoe_api.db_init
import zoe_api.api_endpoint
import zoe_api.rest_api
import zoe_api.web
28

29
log = logging.getLogger("zoe_api")
Daniele Venzano's avatar
Daniele Venzano committed
30
LOG_FORMAT = '%(asctime)-15s %(levelname)s %(name)s (%(threadName)s): %(message)s'
31 32 33 34 35 36 37


def zoe_web_main() -> int:
    """
    This is the entry point for the Zoe Web script.
    :return: int
    """
38 39
    config.load_configuration()
    args = config.get_conf()
40
    if args.debug:
Daniele Venzano's avatar
Daniele Venzano committed
41
        logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
42
    else:
Daniele Venzano's avatar
Daniele Venzano committed
43
        logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
44 45 46 47
    logging.getLogger("requests").setLevel(logging.WARNING)
    logging.getLogger("tornado").setLevel(logging.DEBUG)

    log.info("Starting HTTP server...")
48
    app = Flask(__name__, static_url_path='/does-not-exist')
49
    app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
50

51
    zoe_api.db_init.init()
52

53 54 55 56 57
    api_endpoint = zoe_api.api_endpoint.APIEndpoint()
    config.api_endpoint = api_endpoint

    app.register_blueprint(zoe_api.rest_api.api_init(api_endpoint))
    app.register_blueprint(zoe_api.web.web_init())
58 59 60 61

    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(args.listen_port, args.listen_address)
    ioloop = IOLoop.instance()
62

63
    retry_cb = PeriodicCallback(api_endpoint.retry_submit_error_executions, 30000)
64 65
    retry_cb.start()

66 67 68 69
    try:
        ioloop.start()
    except KeyboardInterrupt:
        print("CTRL-C detected, terminating")