| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. |
| # The ASF licenses this file to You 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 ConfigParser |
| import importlib |
| import logging |
| import sys |
| import os |
| from functools import partial |
| |
| import pkg_resources |
| import tornado.web |
| from tornado.options import define, options, parse_command_line |
| |
| import webservice.algorithms_spark.NexusCalcSparkHandler |
| from nexustiles.nexustiles import NexusTileService |
| from webservice import NexusHandler |
| from webservice.nexus_tornado.request.handlers import NexusRequestHandler |
| from nexus_tornado.request.handlers import NexusHandlerManager |
| |
| |
| def inject_args_in_config(args, config): |
| """ |
| Takes command argparse arguments and push them in the config |
| with syntax args.<section>-<option> |
| """ |
| log = logging.getLogger(__name__) |
| |
| for t_opt in args._options.values(): |
| n = t_opt.name |
| first_ = n.find('_') |
| if first_ > 0: |
| s, o = n[:first_], n[first_ + 1:] |
| v = t_opt.value() |
| log.info('inject argument {} = {} in configuration section {}, option {}'.format(n, v, s, o)) |
| if not config.has_section(s): |
| config.add_section(s) |
| config.set(s, o, v) |
| return config |
| |
| |
| if __name__ == "__main__": |
| |
| logging.basicConfig( |
| level=logging.DEBUG, |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| datefmt="%Y-%m-%dT%H:%M:%S", stream=sys.stdout) |
| |
| log = logging.getLogger(__name__) |
| |
| webconfig = ConfigParser.RawConfigParser() |
| webconfig.readfp(pkg_resources.resource_stream(__name__, "config/web.ini"), filename='web.ini') |
| |
| algorithm_config = ConfigParser.RawConfigParser() |
| algorithm_config.readfp(pkg_resources.resource_stream(__name__, "config/algorithms.ini"), filename='algorithms.ini') |
| |
| define("debug", default=False, help="run in debug mode") |
| define("port", default=webconfig.get("global", "server.socket_port"), help="run on the given port", type=int) |
| define("address", default=webconfig.get("global", "server.socket_host"), help="Bind to the given address") |
| define('solr_time_out', default=60, |
| help='time out for solr requests in seconds, default (60) is ok for most deployments' |
| ' when solr performances are not good this might need to be increased') |
| define('solr_host', help='solr host and port') |
| define('cassandra_host', help='cassandra host') |
| define('cassandra_username', help='cassandra username') |
| define('cassandra_password', help='cassandra password') |
| |
| parse_command_line() |
| algorithm_config = inject_args_in_config(options, algorithm_config) |
| |
| module_dirs = webconfig.get("modules", "module_dirs").split(",") |
| |
| staticDir = webconfig.get("static", "static_dir") |
| staticEnabled = webconfig.get("static", "static_enabled") == "true" |
| |
| log.info("Initializing on host address '%s'" % options.address) |
| log.info("Initializing on port '%s'" % options.port) |
| log.info("Starting web server in debug mode: %s" % options.debug) |
| if staticEnabled: |
| log.info("Using static root path '%s'" % staticDir) |
| else: |
| log.info("Static resources disabled") |
| |
| max_request_threads = webconfig.getint("global", "server.max_simultaneous_requests") |
| |
| tile_service_factory = partial(NexusTileService, False, False, algorithm_config) |
| job_pool = {} |
| |
| nexus_handler_manager = NexusHandlerManager(module_dirs, |
| algorithm_config, tile_service_factory, |
| max_request_threads=max_request_threads, |
| static_dir=staticDir) |
| handlers = nexus_handler_manager.get_handlers() |
| |
| app = tornado.web.Application( |
| handlers, |
| default_host=options.address, |
| debug=options.debug |
| ) |
| app.listen(options.port) |
| |
| log.info("Starting HTTP listener...") |
| tornado.ioloop.IOLoop.current().start() |