entrypoint.py 2.78 KB
Newer Older
1
# Copyright (c) 2016, Daniele Venzano
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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.

16 17
"""Zoe API entrypoint module."""

18
import logging
19
import os
20 21

from tornado.httpserver import HTTPServer
22
from tornado.ioloop import IOLoop, PeriodicCallback
23
from tornado.web import Application
24

25
import zoe_lib.config
26
import zoe_lib.state
Daniele Venzano's avatar
Daniele Venzano committed
27 28
import zoe_api.api_endpoint
import zoe_api.rest_api
29
import zoe_api.master_api
Daniele Venzano's avatar
Daniele Venzano committed
30
import zoe_api.web
31
import zoe_api.auth.ldap
32
from zoe_api.web.request_handler import JinjaApp
33

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


38
def zoe_web_main(test_conf=None) -> int:
39 40 41 42
    """
    This is the entry point for the Zoe Web script.
    :return: int
    """
43 44
    zoe_lib.config.load_configuration(test_conf)
    args = zoe_lib.config.get_conf()
45

46 47 48 49 50 51 52
    log_args = {
        'level': logging.DEBUG if args.debug else logging.INFO,
        'format': LOG_FORMAT
    }
    if args.log_file != "stderr":
        log_args['filename'] = args.log_file
    logging.basicConfig(**log_args)
53 54
    logging.getLogger("MARKDOWN").setLevel(logging.WARNING)
    logging.getLogger("tornado").setLevel(logging.WARNING)
55

56
    sql_manager = zoe_lib.state.SQLManager(zoe_lib.config.get_conf())
57
    sql_manager.init_db()
58

59 60
    master_api = zoe_api.master_api.APIManager()
    api_endpoint = zoe_api.api_endpoint.APIEndpoint(master_api, sql_manager)
61

62 63
    app_settings = {
        'static_path': os.path.join(os.path.dirname(__file__), "web", "static"),
64
        'static_url_prefix': args.reverse_proxy_path + '/static/',
65
        'template_path': os.path.join(os.path.dirname(__file__), "web", "templates"),
66
        'cookie_secret': zoe_lib.config.get_conf().cookie_secret,
67
        'login_url': '/login',
68
        'debug': args.debug
69 70
    }
    app = Application(zoe_api.web.web_init(api_endpoint) + zoe_api.rest_api.api_init(api_endpoint), **app_settings)
71
    JinjaApp.init_app(app)
72

73 74 75 76
    log.info("Starting HTTP server...")
    http_server = HTTPServer(app)
    http_server.bind(args.listen_port, args.listen_address)
    http_server.start(num_processes=1)
Daniele Venzano's avatar
Daniele Venzano committed
77

Daniele Venzano's avatar
Daniele Venzano committed
78 79
    runtime_limit_cb = PeriodicCallback(api_endpoint.verify_runtime_limit, 300000)
    runtime_limit_cb.start()
80

81
    try:
82
        IOLoop.current().start()
83 84
    except KeyboardInterrupt:
        print("CTRL-C detected, terminating")
85 86

    return 0