Example #1
0
def run_server(args=None, debug=False):
    if args is None:
        args = parse_args()

    if "ERT_STORAGE_TOKEN" in os.environ:
        authtoken = os.environ["ERT_STORAGE_TOKEN"]
    else:
        authtoken = generate_authtoken()
        os.environ["ERT_STORAGE_TOKEN"] = authtoken

    lockfile = Path.cwd() / "storage_server.json"
    if lockfile.exists():
        sys.exit("'storage_server.json' already exists")

    config_args = {}
    if args.debug or debug:
        config_args.update(reload=True,
                           reload_dirs=[os.path.dirname(ert_shared_path)])
        os.environ["ERT_STORAGE_DEBUG"] = "1"

    _, _, sock = port_handler.find_available_port(custom_host=args.host)

    connection_info = _create_connection_info(sock, authtoken)

    # Appropriated from uvicorn.main:run
    os.environ["ERT_STORAGE_NO_TOKEN"] = "1"
    if args.enable_new_storage:
        args.database_url = "sqlite:///ert.db"
    if args.database_url:
        os.environ["ERT_STORAGE_DATABASE_URL"] = args.database_url
        config = uvicorn.Config("ert_storage.app:app", **config_args)
    else:
        # Dark Storage imports from ERT Storage, which connects to the database
        # at startup. We set the database URL to an SQLite in-memory database so
        # that the import succeeds.
        os.environ["ERT_STORAGE_DATABASE_URL"] = "sqlite://"
        os.environ["ERT_STORAGE_RES_CONFIG"] = args.config or find_ert_config()
        config = uvicorn.Config("ert_shared.dark_storage.app:app",
                                **config_args)
    server = Server(config, json.dumps(connection_info), lockfile)

    print("Storage server is ready to accept requests. Listening on:")
    for url in connection_info["urls"]:
        print(f"  {url}")
        print(f"\nOpenAPI Docs: {url}/docs", file=sys.stderr)

    if args.debug or debug:
        print("\tRunning in NON-SECURE debug mode.\n")
        os.environ["ERT_STORAGE_NO_TOKEN"] = "1"
    else:
        print("\tUsername: __token__")
        print(f"\tPassword: {connection_info['authtoken']}\n")

    if config.should_reload:
        supervisor = ChangeReload(config, target=server.run, sockets=[sock])
        supervisor.run()
    else:
        server.run(sockets=[sock])
Example #2
0
File: main.py Project: ManInFez/ert
def run_server(args=None, debug=False):
    if args is None:
        args = parse_args()

    if "ERT_STORAGE_TOKEN" in os.environ:
        authtoken = os.environ["ERT_STORAGE_TOKEN"]
    else:
        authtoken = generate_authtoken()
        os.environ["ERT_STORAGE_TOKEN"] = authtoken

    # Use sqlite in cwd if nothing else is specified
    if "ERT_STORAGE_DATABASE_URL" not in os.environ:
        os.environ["ERT_STORAGE_DATABASE_URL"] = "sqlite:///ert.db"

    lockfile = Path.cwd() / "storage_server.json"
    if lockfile.exists():
        sys.exit("'storage_server.json' already exists")

    config_args = {}
    if args.debug or debug:
        config_args.update(reload=True,
                           reload_dirs=[os.path.dirname(ert_shared_path)])
        os.environ["ERT_STORAGE_DEBUG"] = "1"

    sock = bind_open_socket(args.host)
    connection_info = {
        "urls": [
            f"http://{host}:{sock.getsockname()[1]}" for host in (
                sock.getsockname()[0],
                socket.gethostname(),
                socket.getfqdn(),
            )
        ],
        "authtoken":
        authtoken,
    }

    # Appropriated from uvicorn.main:run
    config = uvicorn.Config("ert_storage.app:app", **config_args)
    server = Server(config, json.dumps(connection_info), lockfile)

    print("Storage server is ready to accept requests. Listening on:")
    for url in connection_info["urls"]:
        print(f"  {url}")

    print(f"\nOpenAPI Docs: {url}/docs", file=sys.stderr)
    if args.debug or debug:
        print("\tRunning in NON-SECURE debug mode.\n")
    else:
        print(f"\tUsername: __token__")
        print(f"\tPassword: {connection_info['authtoken']}\n")

    if config.should_reload:
        supervisor = ChangeReload(config, target=server.run, sockets=[sock])
        supervisor.run()
    else:
        server.run(sockets=[sock])
Example #3
0
def run(app: typing.Union[ASGIApplication, str], **kwargs: typing.Any) -> None:
    app_dir = kwargs.pop("app_dir", None)
    if app_dir is not None:
        sys.path.insert(0, app_dir)

    config = Config(app, **kwargs)
    server = Server(config=config)

    if (config.reload or config.workers > 1) and not isinstance(app, str):
        logger = logging.getLogger("uvicorn.error")
        logger.warning(
            "You must pass the application as an import string to enable 'reload' or "
            "'workers'.")
        sys.exit(1)

    if config.should_reload:
        sock = config.bind_socket()
        ChangeReload(config, target=server.run, sockets=[sock]).run()
    elif config.workers > 1:
        sock = config.bind_socket()
        Multiprocess(config, target=server.run, sockets=[sock]).run()
    else:
        server.run()
    if config.uds:
        os.remove(config.uds)  # pragma: py-win32

    if not server.started and not config.should_reload and config.workers == 1:
        sys.exit(STARTUP_FAILURE)
def dev(ctx):
    environ.update(DEV_ENV)
    port = environ.get("PORT", 8000)
    host = "0.0.0.0"  # nosec, it's not a mistake

    config = uvicorn.Config(app="main:app",
                            host=host,
                            port=int(port),
                            debug=True)
    server = uvicorn.Server(config)

    from app.log import logger  # noqa, must be imported before running supervisor

    supervisor = ChangeReload(config,
                              target=server.run,
                              sockets=[config.bind_socket()])
    supervisor.run()
Example #5
0
def run_server(args=None, debug=False):
    if args is None:
        args = parse_args()

    if "ERT_AUTHTOKEN" in os.environ:
        authtoken = os.environ["ERT_AUTHTOKEN"]
    else:
        authtoken = generate_authtoken()
        os.environ["ERT_AUTHTOKEN"] = authtoken

    lockfile = Path.cwd() / "storage_server.json"
    if lockfile.exists():
        sys.exit("'storage_server.json' already exists")

    config_args = {}
    if debug:
        config_args.update(reload=True,
                           reload_dirs=[os.path.dirname(ert_shared_path)])

    sock = bind_open_socket(args.host)
    connection_info = json.dumps({
        "urls": [
            f"http://{host}:{sock.getsockname()[1]}" for host in (
                sock.getsockname()[0],
                socket.gethostname(),
                socket.getfqdn(),
            )
        ],
        "authtoken":
        authtoken,
    })

    # Appropriated from uvicorn.main:run
    config = uvicorn.Config("ert_shared.storage.app:app", **config_args)
    server = Server(config, connection_info, lockfile)

    print(connection_info)
    if config.should_reload:
        supervisor = ChangeReload(config, target=server.run, sockets=[sock])
        supervisor.run()
    else:
        server.run(sockets=[sock])
Example #6
0
def dev(log_level="debug"):
    environ["FIRESTORE_EMULATOR_HOST"] = "127.0.0.1:8686"
    config = uvicorn.Config(
        app="saatja.app:app",
        host="0.0.0.0",  # nosec 0.0.0.0 is not a mistake
        port=conf.PORT,
        log_level=log_level,
        reload=False,
    )
    server = uvicorn.Server(config)

    # Activate logging configuration
    from saatja.log import logger

    _ = logger

    supervisor = ChangeReload(config,
                              target=server.run,
                              sockets=[config.bind_socket()])
    supervisor.run()
Example #7
0
def main(
    bento_identifier: str = "",
    bind: str = "",
    working_dir: t.Optional[str] = None,
    reload: bool = False,
    reload_delay: t.Optional[float] = None,
    backlog: int = 2048,
):
    import uvicorn  # type: ignore

    from ...configuration import get_debug_mode

    ServiceContext.component_name_var.set("dev_api_server")

    parsed = urlparse(bind)

    if parsed.scheme == "fd":
        fd = int(parsed.netloc)
        sock = socket.socket(fileno=fd)
        log_level = "debug" if get_debug_mode() else "info"
        svc = load(bento_identifier,
                   working_dir=working_dir,
                   change_global_cwd=True)
        uvicorn_options = {
            "log_level": log_level,
            "backlog": backlog,
            "reload": reload,
            "reload_delay": reload_delay,
            "log_config": LOGGING_CONFIG,
            "workers": 1,
        }

        if reload:
            # When reload=True, the app parameter in uvicorn.run(app) must be the import str
            asgi_app_import_str = f"{svc._import_str}.asgi_app"  # type: ignore[reportPrivateUsage]
            # TODO: use svc.build_args.include/exclude as default files to watch
            # TODO: watch changes in model store when "latest" model tag is used
            config = uvicorn.Config(asgi_app_import_str, **uvicorn_options)
            server = uvicorn.Server(config)

            from uvicorn.supervisors import ChangeReload  # type: ignore

            ChangeReload(config, target=server.run, sockets=[sock]).run()
        else:
            config = uvicorn.Config(svc.asgi_app, **uvicorn_options)
            uvicorn.Server(config).run(sockets=[sock])
    else:
        raise ValueError(f"Unsupported bind scheme: {bind}")
Example #8
0
def run(app: typing.Union[ASGIApplication, str], **kwargs: typing.Any) -> None:
    config = Config(app, **kwargs)
    server = Server(config=config)

    if (config.reload or config.workers > 1) and not isinstance(app, str):
        logger = logging.getLogger("uvicorn.error")
        logger.warning(
            "You must pass the application as an import string to enable 'reload' or "
            "'workers'.")
        sys.exit(1)

    if config.should_reload:
        sock = config.bind_socket()
        ChangeReload(config, target=server.run, sockets=[sock]).run()
    elif config.workers > 1:
        sock = config.bind_socket()
        Multiprocess(config, target=server.run, sockets=[sock]).run()
    else:
        server.run()
Example #9
0
    def run(self):
        log("[api] Starting service")

        try:
            app = FastAPI(exception_handlers={APIException: http_exception_handler})
            app.camera_manager = self.camera_manager

            protected = Depends(HTTPHeaderAuthentication())
            app.include_router(camera.router, prefix="/api", dependencies=[protected])
            app.include_router(camera.public_router, prefix="/api")
            app.include_router(clips.router, prefix="/api", dependencies=[protected])
            app.include_router(clips.public_router, prefix="/api")
            app.include_router(auth.router, prefix="/api")
            app.include_router(system.router, prefix="/api")
            app.include_router(timelapse.router, prefix="/api", dependencies=[protected])
            app.include_router(timelapse.public_router, prefix="/api")

            app.mount("/", StaticFiles(directory="../web/dist", html=True), name="public")

            app.add_middleware(
                CORSMiddleware,
                allow_origins=["*"],
                allow_credentials=True,
                allow_methods=["*"],
                allow_headers=["*"],
            )

            config = Config(app, host=os.environ["API_SERVER_HOST"], port=int(os.environ["API_SERVER_PORT"]))
            self.server = Server(config=config)

            if config.should_reload:
                sock = config.bind_socket()
                supervisor = ChangeReload(config, target=self.server.run, sockets=[sock])
                supervisor.run()
            elif config.workers > 1:
                sock = config.bind_socket()
                supervisor = Multiprocess(config, target=self.server.run, sockets=[sock])
                supervisor.run()
            else:
                self.server.run()

        except KeyboardInterrupt:
            pass
Example #10
0
def run(
    app: typing.Union[ASGIApplication, str],
    *,
    host: str = "127.0.0.1",
    port: int = 8000,
    uds: typing.Optional[str] = None,
    fd: typing.Optional[int] = None,
    loop: LoopSetupType = "auto",
    http: HTTPProtocolType = "auto",
    ws: WSProtocolType = "auto",
    ws_max_size: int = 16777216,
    ws_ping_interval: float = 20.0,
    ws_ping_timeout: float = 20.0,
    ws_per_message_deflate: bool = True,
    lifespan: LifespanType = "auto",
    interface: InterfaceType = "auto",
    debug: bool = False,
    reload: bool = False,
    reload_dirs: typing.Optional[typing.List[str]] = None,
    reload_includes: typing.Optional[typing.List[str]] = None,
    reload_excludes: typing.Optional[typing.List[str]] = None,
    reload_delay: float = 0.25,
    workers: typing.Optional[int] = None,
    env_file: typing.Optional[str] = None,
    log_config: typing.Optional[typing.Union[dict, str]] = None,
    log_level: typing.Optional[str] = None,
    access_log: bool = True,
    proxy_headers: bool = True,
    server_header: bool = True,
    date_header: bool = True,
    forwarded_allow_ips: typing.Optional[str] = None,
    root_path: str = "",
    limit_concurrency: typing.Optional[int] = None,
    backlog: int = 2048,
    limit_max_requests: typing.Optional[int] = None,
    timeout_keep_alive: int = 5,
    ssl_keyfile: typing.Optional[str] = None,
    ssl_certfile: typing.Optional[str] = None,
    ssl_keyfile_password: typing.Optional[str] = None,
    ssl_version: int = int(SSL_PROTOCOL_VERSION),
    ssl_cert_reqs: int = int(ssl.CERT_NONE),
    ssl_ca_certs: typing.Optional[str] = None,
    ssl_ciphers: str = "TLSv1",
    headers: typing.Optional[typing.List[typing.Tuple[str, str]]] = None,
    use_colors: typing.Optional[bool] = None,
    app_dir: typing.Optional[str] = None,
    factory: bool = False,
) -> None:
    if app_dir is not None:
        sys.path.insert(0, app_dir)

    config = Config(
        app,
        host=host,
        port=port,
        uds=uds,
        fd=fd,
        loop=loop,
        http=http,
        ws=ws,
        ws_max_size=ws_max_size,
        ws_ping_interval=ws_ping_interval,
        ws_ping_timeout=ws_ping_timeout,
        ws_per_message_deflate=ws_per_message_deflate,
        lifespan=lifespan,
        interface=interface,
        debug=debug,
        reload=reload,
        reload_dirs=reload_dirs,
        reload_includes=reload_includes,
        reload_excludes=reload_excludes,
        reload_delay=reload_delay,
        workers=workers,
        env_file=env_file,
        log_config=log_config,
        log_level=log_level,
        access_log=access_log,
        proxy_headers=proxy_headers,
        server_header=server_header,
        date_header=date_header,
        forwarded_allow_ips=forwarded_allow_ips,
        root_path=root_path,
        limit_concurrency=limit_concurrency,
        backlog=backlog,
        limit_max_requests=limit_max_requests,
        timeout_keep_alive=timeout_keep_alive,
        ssl_keyfile=ssl_keyfile,
        ssl_certfile=ssl_certfile,
        ssl_keyfile_password=ssl_keyfile_password,
        ssl_version=ssl_version,
        ssl_cert_reqs=ssl_cert_reqs,
        ssl_ca_certs=ssl_ca_certs,
        ssl_ciphers=ssl_ciphers,
        headers=headers,
        use_colors=use_colors,
        factory=factory,
    )
    server = Server(config=config)

    if (config.reload or config.workers > 1) and not isinstance(app, str):
        logger = logging.getLogger("uvicorn.error")
        logger.warning(
            "You must pass the application as an import string to enable 'reload' or "
            "'workers'.")
        sys.exit(1)

    if config.should_reload:
        sock = config.bind_socket()
        ChangeReload(config, target=server.run, sockets=[sock]).run()
    elif config.workers > 1:
        sock = config.bind_socket()
        Multiprocess(config, target=server.run, sockets=[sock]).run()
    else:
        server.run()
    if config.uds:
        os.remove(config.uds)  # pragma: py-win32

    if not server.started and not config.should_reload and config.workers == 1:
        sys.exit(STARTUP_FAILURE)