def load_config_file(): try: options.parse_config_file(local('config.cfg'), final=False) except Exception as e: logging.error(str(e)) options.parse_command_line()
def main(): # Defines define("port", default=8082, help="run on the given port", type=int) define("log_level", default="INFO", type=str, help="[NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL]") define("dbconf", default="sqlite:///vanzilla.db", type=str, help="sqlalchemy db config string") define("mail_server", default="localhost", help="server mail", type=str) define("mail_from", default="", help="sender address for mail error reports", type=str) define("mail_to", default="", help="recipient addresses for mail error reports", type=str) define("root_ips", default=[], help="IPs with global access", type=list) define("promiscuous_load", default=False, help="load all apps, whether they are enabled or not", type=bool) options.parse_config_file("vanzilla.conf") options.parse_command_line() logging.debug(options.logging) # Initalising applications apps = AppsLoader(options) # Starting tornado server http_server = tornado.httpserver.HTTPServer(apps) http_server.listen(options.port) ioloop = tornado.ioloop.IOLoop.instance() tornado.autoreload.add_reload_hook(apps.reload_tasks) tornado.autoreload.start(ioloop) ioloop.start()
def main(): if os.path.isfile("config/server.conf"): options.parse_config_file("config/server.conf") else: logging.warning("server.conf is not found.") options.parse_command_line() url = urlparse(options.ws_url) if options.origin: pass elif url.scheme == "wss": options.origin = urlunparse(("https", url.netloc, "", "", "", "")) else: options.origin = urlunparse(("http", url.netloc, "", "", "", "")) app = web.Application([ (urlparse(options.ws_url).path, WebSocketHandler), (r"/(index\.html)?", IndexHandler), (r"/(.*)", web.StaticFileHandler, { "path": os.path.join(os.getcwd(), options.static_dir) }), ]) if options.ssl_key: server = httpserver.HTTPServer(app, ssl_options={ "certfile": options.ssl_crt, "keyfile": options.ssl_key, }) else: server = httpserver.HTTPServer(app) server.listen(options.port) ioloop.IOLoop.instance().start()
def config_app(config, default_options_file, replace_options): init_env() app_log.set_logging_level(app_common_const.LOG_LEVEL) with open(default_options_file, 'r') as f: default_options = json.load(f) if default_options: for option in default_options.get('global_option', {}): if option: define(option[0], default=option[1], help=option[2], type=get_builtin_type(option[3])) options.parse_config_file(config) if not check_server_config(): raise EnvironmentError('app server config') if app_common_const.OPTION_LOG_PATH in options: app_log.define_file_logging() options.template_path = str(os.path.join(app_common_const.PROJECT_BASE_FOLDER, options.template_path)) options.static_path = str(os.path.join(app_common_const.PROJECT_BASE_FOLDER, options.static_path)) if replace_options and replace_options['port']: options.port = int(replace_options['port']) config_jinja2()
def parse_config(): options.parse_command_line() if options.config != "": logger.info("parse config from config file: {config}".format( config=options.config)) options.parse_config_file(options.config) if options.slack_token == "": logger.error("slack token is required!!") sys.exit(1) if options.models_path == "": logger.error("models path is required!!") sys.exit(1) if options.db_path == "": logger.info( "path of database is not set, use the default db path: {path}". format(path=DEFAULT_DB_PATH)) options.db_path = DEFAULT_DB_PATH mg.MODELS_PATH = options.models_path alert.SLACK_TOKEN = options.slack_token job.REPORT_ADDRESS = options.address logger.info("config: {config}".format(config=options.items()))
def boreas(): from tornado.options import define, options from boreas import server define("config", help="Configuration module", type=str) define("debug", default=True, help="Enable debugging urls", type=bool) define("api_port", default=8001, help="API port", type=int) define("api_host", default='127.0.0.1', help="API host", type=str) define("ws_port", default=8002, help="Websocket port", type=int) define("ws_host", default='127.0.0.1', help="Websocket host", type=str) define("token_provider", default='boreas.utils.tokens:no_tokens', help="Function providing initial tokens", type=str) define("require_auth", default=True, help="Flag indicating if user has to authenticate", type=bool) options.parse_command_line() if options.config is None: # assume boreas.conf in working directory conf_file = 'boreas.conf' try: options.parse_config_file(conf_file) except IOError: pass # just use defaults else: conf_file = options.config options.parse_config_file(conf_file) server.run(options)
def parse_command_line(extra_args_func=None): """ Parse command line arguments for any Pyaiot application. """ if not hasattr(options, "config"): define("config", default=None, help="Config file") if not hasattr(options, "broker_host"): define("broker_host", default="localhost", help="Broker host") if not hasattr(options, "broker_port"): define("broker_port", default=8002, help="Broker websocket port") if not hasattr(options, "key_file"): define("key_file", default=DEFAULT_KEY_FILENAME, help="Secret and private keys filename.") if not hasattr(options, "debug"): define("debug", default=False, help="Enable debug mode.") if extra_args_func is not None: extra_args_func() options.parse_command_line() if options.config: options.parse_config_file(options.config) # Parse the command line a second time to override config file options options.parse_command_line()
def get_app(self): self.app = Application s_file = os.path.abspath( os.path.join(os.path.dirname(__file__), "settings.conf")) options.parse_config_file(s_file) return self.app()
def parse_config_file(conf_file): conf_file = os.path.join( config.uhphome ,"uhpweb","etc","uhpweb.conf" ) if os.path.exists(conf_file): options.parse_config_file(conf_file) gen_log.info("Load config file from %s" %(conf_file)) else: gen_log.error("Cant load the config file from %s " % (conf_file))
def __init__(self): """ Constructor """ options.parse_command_line() if options.config_name: config_to_read = '%s/%s' % (os.getcwd(), options.config_name) else: config_to_read = '%s/%s' % (os.getcwd(), 'config.py') logger.debug('path to config: %s' % config_to_read) options.parse_config_file(config_to_read) # override options from config with command line options options.parse_command_line() self.__db_engine = None # engine for db_session self.__db_session = None # singleton of SQLAlchemy connections pool self.__redis_session = None # singleton of Redis connections pool self.__redis_scheme = redis_scheme # Redis key mapping self.__thread_pool = None # thread pool for doing some jobs in background self.__custom_response_headers = dict() # custom headers, which will be mixed in every response self.__endpoints = list() # list of Tornado routes with handler classes, permissions self.__handler_docs = dict() # all docstrings of all methods of all routes self.__handler_validators = dict() # all validators (UrlParser instance) of all methods of all routes self.__started = False # make singleton of thread pool self.__make_thread_pool() # init singleton of DB connections pool self.__make_db_session() # init singleton of Redis connections pool self.__make_redis_session()
def parse_command_line(): """Parse command line arguments for CoAP gateway application.""" if not hasattr(options, "config"): define("config", default=None, help="Config file") if not hasattr(options, "broker_host"): define("broker_host", default="localhost", help="Pyaiot broker host") if not hasattr(options, "broker_port"): define("broker_port", default=8000, help="Pyaiot broker port") if not hasattr(options, "mqtt_host"): define("mqtt_host", default=MQTT_HOST, help="Gateway MQTT broker host") if not hasattr(options, "mqtt_port"): define("mqtt_port", default=MQTT_PORT, help="Gateway MQTT broker port") if not hasattr(options, "max_time"): define("max_time", default=MAX_TIME, help="Maximum retention time (in s) for MQTT dead nodes") if not hasattr(options, "key_file"): define("key_file", default=DEFAULT_KEY_FILENAME, help="Secret and private keys filename.") if not hasattr(options, "debug"): define("debug", default=False, help="Enable debug mode.") options.parse_command_line() if options.config: options.parse_config_file(options.config) # Parse the command line a second time to override config file options options.parse_command_line()
def main(): options.parse_config_file(config_file, final=False) options.parse_command_line() sanity_check(options) settings = dict( compress_response=True, cookie_secret=options.cookie_secret, debug=options.debug, login_url=options.login_url, static_handler_class=web.JsonAwareStaticFileHandler, static_path='static/', xheaders=True, ) routes = initialize(options.database) application = Application(routes, **settings) server = HTTPServer(application, xheaders=True) server.bind(options.port) server.start(1) # Forks multiple sub-processes logging.info('Configured for "%s"' % environment) logging.info('Running on port %s' % options.port) IOLoop.current().start()
def _load_conf(): options.parse_command_line() _update_collon_with_cwd() if pathlib.Path(options.conf).is_file(): options.parse_config_file(options.conf) logging.info("load conf from '{}'.".format(options.conf)) elif EXAMPLE_CONF.is_file(): options.parse_config_file(EXAMPLE_CONF) logging.warning("load example conf from '{}'.".format(EXAMPLE_CONF)) else: logging.error("example conf '{}' is not found.".format(EXAMPLE_CONF)) _update_collon_with_cwd() options.parse_command_line() _update_collon_with_cwd() if pathlib.Path(options.mail_body_file).is_file(): with open(options.mail_body_file) as f: options.mail_body = f.read() logging.info("load mail body from '{}'.".format(options.mail_body_file)) elif pathlib.Path(EXAMPLE_MAIL_BODY_FILE).is_file(): with open(EXAMPLE_MAIL_BODY_FILE) as f: options.mail_body = f.read() logging.warning("load example mail body from '{}'.".format(EXAMPLE_MAIL_BODY_FILE)) else: logging.error("example mail body file '{}' is not found.".format(EXAMPLE_MAIL_BODY_FILE))
def load_options(self): options.logging = None try: options.parse_config_file(self.config_file, final=False) except FileNotFoundError as e: print("Error, config file {} not found".format(self.config_file)) sys.exit(1) options.parse_command_line()
def __init__(self): options.parse_config_file('settings.py') handlers = [('/', ChatHandler)] factory = make_session_factory(options.DB_URL) super().__init__(session_factory=factory, handlers=handlers, debug=True)
def main(): options.parse_config_file("webssh.conf") parse_command_line() django.setup() server = tornado.httpserver.HTTPServer(Application()) server.listen(options.port) IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()
def main(): init_config() options.parse_config_file(os.path.join(BASE_DIR, "webssh.conf")) http_server = httpserver.HTTPServer(Application()) http_server.listen(options.port, address="0.0.0.0") IOLoop.instance().start() welcome(options.port) ioloop.IOLoop.instance().start()
def connect(cls, dbname): db = None config.init_config() options.parse_config_file('pacado.conf') if dbname == 'meta': db = MongoClient(options.mongo_meta_uri)['metadata'] if dbname == 'cnyb': db = MongoClient(options.mongo_cnyb_uri)['cnyb'] return db
def load_options(self): options.logging = None options.parse_command_line(final=False) try: options.parse_config_file(options.config_file) except FileNotFoundError as e: application_log.critical( f"Error, config file {options.config_file} not found") sys.exit(1)
def main(): options.parse_config_file("server.ini", final=False) options.parse_command_line() log.setup() init() make_app().listen(8888) IOLoop.current().start()
def init_options(): # maybe some options will be use before load config file options.parse_command_line() options.parse_config_file(options.cfg_file) if not options.log_root_path or not options.port: options.print_help() _usage() options.log_root_path = _check_dir_tail(options.log_root_path) options.log_path = '%s/%d' % (options.log_root_path, options.port) _mkdir(options.log_path)
def main(): if os.path.isfile(CONFIG_FILE_PATH): options.parse_config_file(CONFIG_FILE_PATH, False) options.parse_command_line() if options.help: return start_worker() start_web()
def main(): options.parse_config_file("server.ini", final=False) options.parse_command_line() session_factory = make_session_factory(options.database_url) m.DeclarativeBase.metadata.create_all(session_factory.engine) session = session_factory.make_session() fix_user_create_at(session) fix_queston_create_at(session) fix_job_create_at(session) session.commit() session.close()
def set_configs(self): """ Name (define) the configuration items and load their values from configuration file """ for config_item in ['tag_creds', 'encrypt_tag']: define(config_item) # Get configuration values from configuration filescript_name = path.basename(__file__) script_path = path.abspath(path.realpath(__file__)) script_dir = path.split(script_path)[0] config_path = path.abspath( path.join(script_dir, 'model/EmsgEncrypt.conf')) options.parse_config_file(config_path)
def parse_config_file(conf_file): if not conf_file: conf_file = "/etc/uhp/uhpweb.conf" conf_file_other = "etc/uhpweb.conf" if os.path.exists(conf_file): options.parse_config_file(conf_file) gen_log.info("Load config file from %s" %(conf_file)) elif os.path.exists(conf_file_other): options.parse_config_file(conf_file_other) gen_log.info("Load config file from %s" %(conf_file_other)) else: gen_log.error("Cant load the config file from %s or %s" % (conf_file, conf_file_other))
def main(): options.parse_config_file(config_file, final=False) options.parse_command_line() sanity_check(options) settings = dict( compress_response=True, cookie_secret=options.cookie_secret, debug=options.debug, login_url=options.login_url, static_handler_class=web.JsonAwareStaticFileHandler, static_path='static/', xheaders=True, ) routes = initialize(options.database) application = Application(routes, **settings) try: server_https = HTTPServer( application, xheaders=True, ssl_options={ "certfile": "/etc/letsencrypt/live/api-usability.teco.edu/cert.pem", "keyfile": "/etc/letsencrypt/live/api-usability.teco.edu/privkey.pem", }) server_https.bind(options.port) server_https.start(1) # Forks multiple sub-processes except ValueError as exc: logging.error('HTTPS server couldn\'t be started %s', exc) logging.info('HTTP server started') server_https = HTTPServer(application, xheaders=True) logging.info('Configured HTTPS for "%s"' % environment) logging.info('Running HTTPS server on port %s' % options.port) server_http = HTTPServer( Application([(r"/(.*)", StaticFileHandler, { "path": './static/templates/', "default_filename": "https_required.html" })], **settings)) server_http.bind(80) server_http.start(1) logging.info('Configured HTTP for "%s"' % environment) logging.info('Running HTTP server on port %d' % 80) IOLoop.current().start()
def parse_config_file(conf_file): if not conf_file: conf_file = "/etc/uhp/uhpweb.conf" conf_file_other = "etc/uhpweb.conf" if os.path.exists(conf_file): options.parse_config_file(conf_file) gen_log.info("Load config file from %s" % (conf_file)) elif os.path.exists(conf_file_other): options.parse_config_file(conf_file_other) gen_log.info("Load config file from %s" % (conf_file_other)) else: gen_log.error("Cant load the config file from %s or %s" % (conf_file, conf_file_other))
def main(): parse_command_line() options.parse_config_file(join(app_root, "conf/%s/server.conf" % options.env)) http_server = tornado.httpserver.HTTPServer(app.HelloWorldApplication(), xheaders=True) http_server.listen(options.port, "0.0.0.0") print("Starting Tornado with config {0} on http://localhost:{1}/".format(options.env, options.port)) try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: sys.exit(0)
def parse_options_config(path): """从配置文件读取配置参数. :parameter path: 配置文件目录 """ options.parse_command_line() if options.config: machine = os.getenv("service_host") if machine: options.config = "%s.%s" % (options.config, machine) app_log.info("初始化配置文件:%s" % options.config) options.parse_config_file(os.path.join(path, 'web_config.conf'))
def init_options(): # maybe some options will be use before load config file options.parse_command_line() options.cfg_file = os.path.abspath(options.cfg_file) options.parse_config_file(options.cfg_file) if not options.log_path or not options.port: options.print_help() _usage() options.log_path = os.path.abspath(options.log_path) if not os.path.exists(options.log_path): os.makedirs(options.log_path)
def parse_config(): options.parse_command_line() if options.config != "": logger.info("parse config from config file: {config}".format( config=options.config)) options.parse_config_file(options.config) if options.slack_token == "": logger.error("slack token is required!!") sys.exit(1) alert.SLACK_TOKEN = options.slack_token logger.info("config: {config}".format(config=options.items()))
def main(): # Load config. options.parse_config_file(opengb.config.CONFIG_FILE) # Initialize database. ODB.initialize(options.db_file) # Initialize printer queues. to_printer = multiprocessing.Queue() from_printer = multiprocessing.Queue() # Initialize printer using queue callbacks. printer_callbacks = opengb.printer.QueuedPrinterCallbacks(from_printer) printer_type = getattr(opengb.printer, options.printer) printer = printer_type(to_printer, printer_callbacks, baud_rate=options.baud_rate, port=options.serial_port) printer.daemon = True printer.start() # Initialize web server. install_dir = resource_filename(Requirement.parse('openGB'), 'opengb') static_dir = os.path.join(install_dir, 'static') handlers = [ (r"/ws", WebSocketHandler, {"to_printer": to_printer}), (r"/api/status", StatusHandler), (r"/fonts/(.*)", StaticFileHandler, {"path": os.path.join(static_dir, "fonts")}), (r"/views/(.*)", StaticFileHandler, {"path": os.path.join(static_dir, "views")}), (r"/images/(.*)", StaticFileHandler, {"path": os.path.join(static_dir, "images")}), (r"/scripts/(.*)", StaticFileHandler, {"path": os.path.join(static_dir, "scripts")}), (r"/styles/(.*)", StaticFileHandler, {"path": os.path.join(static_dir, "styles")}), (r"/(.*)", StaticFileHandler, {"path": os.path.join(static_dir, "index.html")}), ] app = Application(handlers=handlers, debug=options.debug) httpServer = tornado.httpserver.HTTPServer(app) httpServer.listen(options.http_port) # Create event loop and periodic callbacks main_loop = tornado.ioloop.IOLoop.instance() printer_event_processor = tornado.ioloop.PeriodicCallback( lambda: process_printer_events(from_printer), 10, io_loop=main_loop) counter_updater = tornado.ioloop.PeriodicCallback( lambda: update_counters(), 60000) # TODO: ioloop for watchdog # TODO: ioloop for camera # Rock and roll. printer_event_processor.start() counter_updater.start() main_loop.start() return(os.EX_OK)
def getTornadoUrl(): import socket from tornado.options import options ip = socket.gethostbyname(socket.gethostname()) # Check config file SERVER_CONFIG = '/etc/domoweb.cfg' if not os.path.isfile(SERVER_CONFIG): sys.stderr.write("Error: Can't find the file '%s'\n" % SERVER_CONFIG) sys.exit(1) options.define("port", default=40404, help="Launch on the given port", type=int) options.parse_config_file(SERVER_CONFIG) return "http://%s:%s/" % (ip, options.port)
def main(): options.parse_command_line() if not options.config_file is None: options.parse_config_file(options.config_file) options.parse_command_line() if options.client_secret: settings["cookie_secret"] = options.client_secret logging.info("Starting Tornado web server on http://localhost:%s" % options.port) logging.info("--data_path=%s" % options.data_path) logging.info("--client_host=%s" % options.client_host) logging.info("--authorized_users=%s" % options.authorized_users) logging.info("--mongo_storage_uri=%s" % options.mongo_storage_uri) logging.info("--mongo_storage_db=%s" % options.mongo_storage_db) logging.info("--mongo_rows_limit=%s" % options.mongo_rows_limit) if not options.config_file is None: logging.info("--config_file=%s" % options.config_file) if not options.github_repo_api_url is None: logging.info("--github_repo_api_url=%s" % options.github_repo_api_url) logging.info("--github_project_root=%s" % options.github_project_root) logging.info("--github_branches_root=%s" % options.github_branches_root) logging.info("--github_postproc_cmd=%s" % options.github_postproc_cmd) logging.info("--github_git_cmd=%s" % options.github_git_cmd) logging.info("--github_branches_json_path=%s" % options.github_branches_json_path) logging.info("Starting GitHub Web Hook at http://localhost:%s/gitWebHook" % options.port) MongoDbQueryHandler.datastores = parse_datastore_configuration() TabixLookupHandler.tabix_file_map = parse_tabix_lookup_configuration() application = tornado.web.Application([ (r"/", MainHandler), (r"/auth/signin/google", GoogleOAuth2Handler), (r"/auth/signin/google/oauth2_callback", GoogleOAuth2Handler), (r"/auth/signout/google", GoogleSignoutHandler), (r"/auth/whoami", WhoamiHandler), (r"/auth/providers", AuthProvidersHandler), (r"/datastores", MongoDbQueryHandler), (r"/datastores/(.*)", MongoDbQueryHandler), (r"/data?(.*)", LocalFileHandler), (r"/storage/(.*)", MongoDbStorageHandler), (r"/collections/(.*)", MongoDbCollectionsHandler), (r"/tabix/(\w+)/(X|Y|M|\d{1,2})/(\d+)", TabixLookupHandler), (r"/tabix/(\w+)/(X|Y|M|\d{1,2})/(\d+)/(\d+)", TabixLookupHandler), (r"/gitWebHook?(.*)", GitWebHookHandler) ], **settings) application.listen(options.port, **server_settings) tornado.ioloop.IOLoop.instance().start()
def service_core_running(): import os BASE_DIRS = os.path.dirname(__file__) config_file = os.path.join(BASE_DIRS, "./config.py") # try: # options.parse_command_line() options.parse_config_file(config_file) http_server = HTTPServer(DF_Tornado_Application()) # http_server.listen(options.SERVER_PORT, options.SERVER_HOST) http_server.bind(config.options.SERVER_PORT, config.options.SERVER_HOST) print("[NOTICE] Development server is running at http://%s:%s" % (config.options.SERVER_HOST, config.options.SERVER_PORT)) print("[NOTICE] Quit the server with Control-C") IOLoop.instance().start()
def main(): # Load config. options.parse_config_file(opengb.config.CONFIG_FILE) # Initialize database. OGD.initialize(options.db_file) # Initialize printer queues. to_printer = multiprocessing.Queue() from_printer = multiprocessing.Queue() # Initialize printer using queue callbacks. printer_callbacks = opengb.printer.QueuedPrinterCallbacks(from_printer) printer_type = getattr(opengb.printer, options.printer) printer = printer_type(to_printer, printer_callbacks, baud_rate=options.baud_rate, port=options.serial_port, lcd_gpio_pin=options.lcd_gpio_pin) printer.daemon = True printer.start() # Initialize web server. # Backend handler is always required. handlers = [(r"/ws", WebSocketHandler, {"to_printer": to_printer})] # Frontend-specfic handlers added if required. try: handlers += get_frontend_handlers(options.frontend) except IOError as e: LOGGER.exception(e) LOGGER.warn('No frontend will be served.') app = Application(handlers=handlers, debug=options.debug) httpServer = tornado.httpserver.HTTPServer(app) httpServer.listen(options.http_port) # Create event loop and periodic callbacks main_loop = tornado.ioloop.IOLoop.instance() printer_event_processor = tornado.ioloop.PeriodicCallback( lambda: process_printer_events(from_printer), 10, io_loop=main_loop) counter_updater = tornado.ioloop.PeriodicCallback( lambda: update_counters(), 60000) # TODO: ioloop for watchdog # TODO: ioloop for camera # Rock and roll. printer_event_processor.start() counter_updater.start() main_loop.start() return (os.EX_OK)
async def main(): tornado.options.parse_command_line() options.parse_config_file(os.path.join(APP_ROOT, "config/app.conf")) s = "s" if options.ssl_enabled else "" log.info(f"Starting Tornado on http{s}://localhost:{options.port}/") try: Database = databases.database_types[options.db_type.lower()] except KeyError: log.execption("ERROR: Invalid db_type specified. Check valid database types in the docs.") return log.info("Connecting to database...") async with Database() as db: app = Application(db) db.app = app if not await db.is_initialized(): log.info("Database not initialized, creating roles and Admin user...") roles = [] roles.append(Role.partial(name="Admin", permissions=Permissions.all(), state=app)) permissions = Permissions.default() permissions.manage_files = True permissions.manage_invites = True roles.append(Role.partial(name="Trusted", permissions=permissions, state=app)) roles.append(Role.partial(name="User", permissions=Permissions.default(), state=app)) available_chars = string.ascii_letters + string.digits password = "".join(random.choice(available_chars) for i in range(5)) hashed_password = await User.hash_password(password) user = User.partial(username="******", hashed_password=hashed_password, role_id=None, state=app) await db.initialize(roles, user) log.info(f"Your new admin username is Admin and your password is {password}.") global http_server http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) log.info("Ready to go.") await shutdown_event.wait()
def main(): parse_command_line() options.parse_config_file( join(app_root, "conf/%s/server.conf" % options.env)) http_server = tornado.httpserver.HTTPServer(app.HelloWorldApplication(), xheaders=True) http_server.listen(options.port, "0.0.0.0") print("Starting Tornado with config {0} on http://localhost:{1}/".format( options.env, options.port)) try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: sys.exit(0)
def parse_config_options(): ''' Load up all of the cli and config file options ''' app_root = os.path.abspath(__file__) os.chdir(os.path.dirname(app_root)) tornado.log.enable_pretty_logging() try: options.parse_command_line() if os.path.isfile(options.config): logging.debug("Parsing config file `%s`", os.path.abspath(options.config)) options.parse_config_file(options.config) options.parse_command_line() # CLI takes precedence except Error as error: logging.critical(str(error)) sys.exit()
def load_config(): """ Use default.conf as the definition of options with default values using tornado.options.define. Then overrides the values from: local.conf. This mapping allows to access the application configuration across the application. NOTE: logging in load_config() is not going to work because logging is configured only when tornado.options.parse_command_line(final=True) """ define_options() local_conf = os.path.join(CONF_DIR, 'local.conf') if os.path.isfile(local_conf): options.parse_config_file(local_conf, final=False)
def run(args): define_global_options() options.parse_command_line(['server'] + args, final=False) config_file = path.expanduser(options.config) if path.isfile(config_file): options.parse_config_file(config_file, final=False) _set_x_display(options) options.run_parse_callbacks() try: chmod(options.tmp_dir, 0o755) except (NotImplementedError, OSError, IOError) as e: gen_log.warn('Unable to chmod tmp dir: {}'.format(e)) if path.isfile(config_file): gen_log.info('Config loaded from {}'.format(config_file)) build_app() IOLoop.instance().start()
def bootstrap(config_file=None): options.define('config', config_file or tinyurld.default_config, type=str, help='Config file path') options.define('host', '0.0.0.0', type=str, help='Ip address for bind') options.define('port', 8888, type=int, help='application port') options.define('autoreload', False, type=bool, help='Autoreload application after change files', group='application') options.define('debug', False, type=bool, help='Debug mode', group='application') options.define('mongo_host', type=str, help='MongoDB host IP', group='mongodb') options.define('mongo_port', 27017, type=int, help='MongoDB port', group='mongodb') options.define('mongo_user', None, type=str, help='MongoDB user', group='mongodb') options.define('mongo_password', None, type=str, help='MongoDB user password', group='mongodb') options.parse_command_line() options.parse_config_file(options.config) # override options from config file with command line args options.parse_command_line() tornado.log.app_log.info('Read config: {}'.format(options.config))
def start(prefix, settings, modules, routes, known_exceptions, **kwargs): """starts the tornado application. :param prefix: the url prefix :param settings: the user defined settings :param modules: the modules to load :param handlers: the list of url routes (url, handler) :param known_exceptions: the mapping of known exceptions to HTTP codes :param kwargs: the tornado application arguments """ from tornado.options import options options.define("config", type=str, help="path to config file", callback=lambda p: options.parse_config_file(p, final=False)) options.define("port", default=8000, help="listening port", type=int) options.define("address", default='127.0.0.1', help="listening address") options.add_parse_callback(log.patch_logger) loop = _get_event_loop() modules_registry = ModulesRegistry(loop.asyncio_loop, log.gen_log) for module in modules: modules_registry.lazy_load(module, options) for opt in settings: options.define(**opt) options.parse_command_line(final=True) if not prefix.endswith('/'): prefix += '/' kwargs.update(options.group_dict('application')) kwargs.setdefault('default_handler_class', handler.DefaultHandler) # prevent override this option kwargs['known_exceptions'] = known_exceptions kwargs['modules'] = modules_registry handlers = [] for uri, methods in routes: log.app_log.info("add resource: %s", uri) handlers.append((_concat_url(prefix, uri), compile_handler(methods))) app = web.Application(handlers, **kwargs) app.listen(options.port, options.address, xheaders=True) signal.signal(signal.SIGTERM, lambda *x: loop.stop()) log.app_log.info("start listening on %s:%d", options.address, options.port or 80) try: loop.start() except (KeyboardInterrupt, SystemExit): pass loop.close() log.app_log.info("gracefully shutdown.")
def main(): options.define('environment') options.define('templatesPath') options.define('staticPath') options.define('locale') options.define('port', type=int) options.define('login_url') options.define('attachmentPath') options.define('avatarPath') options.define('repositoryPath') options.define('debug', type=bool) options.define('cache_enabled', type=bool) options.define('redis', type=dict) options.define('smtp', type=dict) options.define('sqlalchemy_engine') options.define('sqlalchemy_kwargs', type=dict) options.define('allowImageFileType', type=set) options.define('allowDocumentFileType', type=dict) options.define('salt') options.define('domain') options.define('jsonFilter', type=set) options.define('assets_path') options.parse_config_file("conf/config.conf") ui_modules = { 'uimodle' : UIModule, } settings = dict( cookie_secret = "__TODO:_Generate_your_own_random_value_here__", template_path = os.path.join(os.path.dirname(__file__), options.templatesPath), static_path = os.path.join(os.path.dirname(__file__), options.staticPath), assets_path = os.path.join(os.path.dirname(__file__), options.assets_path), ui_methods = ui_methods, xsrf_cookies = False, autoescape = None, debug = True, login_url = options.login_url, ui_modules = ui_modules, ) app = Application(settings) app.listen(options.port) tornado.ioloop.IOLoop.instance().start()
def init_options(self): define("port", type=int, help="The port on which this app will listen.") define("static_path", help="Location of static assets.") define("template_path", help="Location of template files.") define("db_name", help="Name of database.") define("db_path", help="Path to mongodb instance.") define("cookie_secret", help="Cookie secret key") define("xsrf_cookies", default=True) define("config", help="Path to config file", callback=lambda path: options.parse_config_file(path, final=False)) options.parse_command_line()
def main(): if os.path.isfile(SERVER_CONF_NAME): options.parse_config_file(SERVER_CONF_NAME) options.parse_command_line() if options.login_password is None: print('please configure login password first') sys.exit(-1) app = App() server = HTTPServer(app, xheaders=True) server.listen(options.port) try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: print('Exit by KeyboardInterrupt')
define('test_time', default=60, help='Time of test running in seconds') define('clients_number', default=100, help='Number of clients for concurrent testing') define('queries_number', default=60, help='Number of queries to database per client') define('conf_file', default=None, help='Path to configuration file') define('rw_ratio', default=0.2, help='Ratio read_req/write_req') define('debug', default=False, help='Show debug messages') define('log_suffix', default=None, help='Suffix for log file') define('log_params', default=True, help='Add run params to log name [test_time, clients_number, queries_number]') define('truncate', default=True, help='Remove all data from table before start') define('init_records', default=10000, help='Initiate test table with number of records') logger = logging.getLogger() options.parse_command_line() if options.conf_file is not None and os.path.exists(options.conf_file): options.parse_config_file(options.conf_file) if options.debug: logger.setLevel(logging.DEBUG) csv_file = None chunc_start = 0 def log_func(func_tp): def innter(func_): def wrapper(*args, **kwargs): start_time = datetime.now() success = True try: func_(*args, **kwargs)
from tornado.options import options from tornado.testing import AsyncHTTPTestCase from tornado.escape import json_encode, json_decode from todotxt_web import server from todotxt_web import todo_txt SECRET_FILE = 'test/secret' PASSWORD_FILE = 'test/password' PASSWORD = "******" server.update_password(PASSWORD_FILE, PASSWORD, server.get_secret(SECRET_FILE)) APP = server.make_app(secret_file=SECRET_FILE, password_file=PASSWORD_FILE) options.parse_config_file(os.path.join('test', 'config.py')) class TestHandlerBase(AsyncHTTPTestCase): """TestCase with initialized app""" def get_app(self): return APP # this is the global app that we created above. class TestTodoHandler(TestHandlerBase): def setUp(self): super(TestTodoHandler, self).setUp() self.todo = todo_txt.TodoTxt(options.todo_file) self.todo.add('The first task')
def run(): options.parse_command_line() if options.config: options.parse_config_file(options.config) options.storage = os.path.abspath(options.storage) if os.getuid() == 0 and options.user: pw = pwd.getpwnam(options.user) uid, gid = pw.pw_uid, pw.pw_gid log.info("Changind user to %s [%s:%s]", options.user, uid, gid) os.setgid(uid) os.setuid(uid) try: if not all(f(options.storage) for f in (os.path.exists, os.path.isdir)): log.info('Creating new package storage directory: "%s"', options.storage) os.makedirs(options.storage) def on_interrupt(*args): log.warning("Receiving interrupt signal. Application will be stopped.") exit(errno.EINTR) log.debug("Preparing signal handling") for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT): signal.signal(sig, on_interrupt) def handle_pdb(sig, frame): import pdb pdb.Pdb().set_trace(frame) if options.debug: signal.signal(signal.SIGUSR2, handle_pdb) log.debug("Creating application instance") app = create_app( options.debug, options.secret, options.gzip, ) log.debug("Creating IOLoop instance.") io_loop = IOLoop.current() io_loop.run_sync(lambda: init_db(options.database)) if not (os.path.exists(options.cache_dir) and os.path.isdir(options.cache_dir)): os.makedirs(options.cache_dir) Cache.CACHE_DIR = options.cache_dir log.info("Init thread pool with %d threads", options.pool_size) handlers.base.BaseHandler.THREAD_POOL = futures.ThreadPoolExecutor(options.pool_size) AsyncHTTPClient.configure(None, max_clients=options.max_http_clients) PYPIClient.configure( options.pypi_server, handlers.base.BaseHandler.THREAD_POOL ) pypi_updater = PeriodicCallback(PYPIClient.packages, HOUR * 1000, io_loop) io_loop.add_callback(PYPIClient.packages) io_loop.add_callback(pypi_updater.start) log.info("Starting server http://%s:%d/", options.address, options.port) http_server = HTTPServer(app, xheaders=options.proxy_mode) http_server.listen(options.port, address=options.address) log.debug('Setting "%s" as storage', options.storage) PackageFile.set_storage(options.storage) log.debug("Starting main loop") io_loop.start() except Exception as e: log.fatal("Exception on main loop:") log.exception(e) exit(1) else: exit(0)
from config_center import make_app from config_center.handler import IndexHandler,CreateHandler,ShowHandler,EditHandler,DeleteHandler router = [ (r'/', IndexHandler), (r'/create',CreateHandler), (r'/edit', EditHandler), (r'/show',ShowHandler), (r'/delete',DeleteHandler), ] if __name__ == '__main__': if os.path.exists('/etc/confcenter-api.conf'): options.parse_config_file('/etc/confcenter-api.conf') if os.path.exists('./confcenter.conf'): options.parse_config_file('./confcenter.conf') options.parse_command_line() template_path = os.path.join(os.path.dirname(__file__), "templates") static_path = os.path.join(os.path.dirname(__file__), "static") app = make_app(router,template_path=template_path,static_path=static_path,debug=True) app.listen(options.port,address=options.bind) try: app.zk.start() IOLoop.current().start() except KeyboardInterrupt: app.zk.stop() IOLoop.current().stop()
login_url = "/auth/signin", cookie_secret = options.secret_key, xsrf_cookie = True ) self.database = DatabaseManager(options.db_host, options.db_user, options.db_pass, options.db_dbbs, charset = "cp1250") def validate_option(dict, key): try: value = dict[key] if value == None: return False except KeyError: return False return True if __name__ == "__main__": options.parse_command_line() if options.config is not None: options.parse_config_file(options.config) # wymagana konfiguracja aplikacji opt = options.as_dict() assert validate_option(opt, "db_host") assert validate_option(opt, "db_user") assert validate_option(opt, "db_pass") assert validate_option(opt, "db_dbbs") app = ClassRegisterApplication() app.listen(options.port) IOLoop.instance().start()
setting = { 'debug' : options.debug, 'gzip' : True } tornado.web.Application.__init__(self,**setting) self.add_handlers(host,urls) if __name__ == '__main__': define('port',default=8500) define('debug',default=False) define('t',default=1) define('db', default=0) define('host', multiple=True) options.parse_config_file('./setting.conf') options.parse_command_line() if options.t == 1: app = App() http_server = tornado.httpserver.HTTPServer(app) http_server.bind(options.port) http_server.start() tornado.ioloop.IOLoop.instance().start() elif options.t == 2: setting = { 'debug' : options.debug } app = tornado.web.Application([ ('/test',testHandler) ],**setting)
def main(): options.parse_command_line() options.parse_config_file('settings.conf') http_server = tornado.httpserver.HTTPServer(Application(options)) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
pairs.insert(pairs.index(_pair), (period, graph_types_all)) pairs.remove(_pair) define('server') define('sites', default=[], type=list) define('rrd_file', default=None, callback=None) define('rrd_enabled', default=False, callback=None) define('graphics', default={'S':(['6h'],[MON_GRAPH_CPU_MAX, MON_GRAPH_BYTES_MAX, MON_GRAPH_DURATION_MAX, MON_GRAPH_DESKTOP_MAX, MON_GRAPH_TRANS_MAX, MON_GRAPH_TRANS_UNIQUE_MAX,])}, callback=_verify_graph) options.parse_config_file("mynotes.conf", final=False) _server=options.server _known_instances = options.sites _instances = [] if _known_instances and options.server: for (_server, _port) in _known_instances: if _server == options.server and _port not in _instances: _instances.append(_port) for _port in _instances: if not (options.rrd_enabled and RRD): break instance_conf = os.path.extsep.join(((os.path.join('instance', str(_port))),'conf')) if instance_conf and os.access(instance_conf, os.F_OK):
define('port', default=8888, help="run on the given port", type=int) define('address', default='192.168.X.X', help="run on the address", type=str) class IndexHandler(RequestHandler): def get(self, room): self.render('index.html') def make_app(): sock_router = SockJSRouter(WebSocketHandler, '/websocket') return Application( sock_router.urls + [ (r'/static/(.*)', StaticFileHandler, {'path': 'static'}), url(r'/(.*)', IndexHandler), ] ) def main(): app = make_app() app.listen(options.port) # , options.address) IOLoop.current().start() if __name__ == '__main__': options.parse_config_file('server.conf') main()
define('server', default='TornadoServer/{}'.format(tornado.version), help='The Server header returned with HTTP responses.') define('port', type=int, default=8888, help='The port on which this app will listen.') define('address', type=str, help='The address on which this app will listen') define('login_url', default='/login', help='Path to login form') define('template_path', help='Location of template files.') define('static_path', help='Location of static files.') define('cookie_secret', help='Cookie secret key') define('xsrf_cookies', default=True) define('x_frame_options', default='DENY') define('x_xss_protection', default=1) define('x_content_type_options', default='nosniff') define('debug', default=True) define('config', help='Path to config file', callback=lambda path: options.parse_config_file(path, final=False)) class Application(tornado.web.Application): def __init__(self): self.init_handlers() self.init_signal_handlers() settings = self.init_settings() tornado.web.Application.__init__(self, self.handlers, **settings) def init_settings(self): settings = { 'debug': options.debug,