def app(config): kwargs = RestHandlerSetup(config) kwargs.update({'prom_configs': config['prom_config']}) logging.info('services available:') for service in kwargs['prom_configs']: logging.info(' %s', service) server = RestServer() server.add_route(r'/', AllConfigs, kwargs) server.add_route(r'/(?P<service>[\w\-]+)', ServiceConfig, kwargs) server.add_route(r'/(?P<service>[\w\-]+)/(?P<component>[\w\-]+)', ComponentConfig, kwargs) return server
class rest_api(module.module): """ Run the REST API module, which handles REST API endpoints. Endpoints live in individual modules in `iceprod.server.rest_api.*`. """ def __init__(self,*args,**kwargs): super(rest_api,self).__init__(*args,**kwargs) # set up the REST API routes, args = setup_rest(self.cfg, module=self) self.server = RestServer(**args) for r in routes: self.server.add_route(*r) kwargs = {} if 'rest_api' in self.cfg: if 'address' in self.cfg['rest_api']: kwargs['address'] = self.cfg['rest_api']['address'] if 'port' in self.cfg['rest_api']: kwargs['port'] = self.cfg['rest_api']['port'] self.server.startup(**kwargs)
def create_server(config): static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'static') kwargs = { 'debug': config['DEBUG'], 'auth_username': config['AUTH_USERNAME'], 'auth_password': config['AUTH_PASSWORD'], 'docs_path': config['DOCS_PATH'], } server = RestServer(static_path=static_path, debug=config['DEBUG'], template_loader=templates, xsrf_cookies=True, cookie_secret=config['COOKIE_SECRET']) server.add_route('/upload', UploadHandler, kwargs) server.add_route('/api/upload', UploadAPIHandler, kwargs) server.startup(address=config['HOST'], port=config['PORT']) return server
def __init__(self,*args,**kwargs): super(rest_api,self).__init__(*args,**kwargs) # set up the REST API routes, args = setup_rest(self.cfg, module=self) self.server = RestServer(**args) for r in routes: self.server.add_route(*r) kwargs = {} if 'rest_api' in self.cfg: if 'address' in self.cfg['rest_api']: kwargs['address'] = self.cfg['rest_api']['address'] if 'port' in self.cfg['rest_api']: kwargs['port'] = self.cfg['rest_api']['port'] self.server.startup(**kwargs)
def main() -> None: """Establish and run REST server.""" logging.basicConfig(level=logging.DEBUG) args = RestHandlerSetup({"auth": {"secret": "secret"}, "debug": True}) args["fruit"] = {} # this could be a DB, but a dict works for now server = RestServer(debug=True) server.add_route("/api/fruits", FruitsHanlder, args) server.startup(address="localhost", port=8080) asyncio.get_event_loop().run_forever()
def start(debug: bool = False) -> RestServer: """Start a LTA DB service.""" config = from_environment(EXPECTED_CONFIG) # logger = logging.getLogger('lta.rest') for name in config: if name not in LOGGING_DENY_LIST: logging.info(f"{name} = {config[name]}") else: logging.info(f"{name} = REDACTED") for name in ["OTEL_EXPORTER_OTLP_ENDPOINT", "WIPACTEL_EXPORT_STDOUT"]: if name in os.environ: logging.info(f"{name} = {os.environ[name]}") else: logging.info(f"{name} = NOT SPECIFIED") args = RestHandlerSetup({ # type: ignore 'auth': { 'secret': config['LTA_AUTH_SECRET'], 'issuer': config['LTA_AUTH_ISSUER'], 'algorithm': config['LTA_AUTH_ALGORITHM'], }, 'debug': debug }) args['check_claims'] = CheckClaims(int(config['LTA_MAX_CLAIM_AGE_HOURS'])) # configure access to MongoDB as a backing store mongo_user = quote_plus(cast(str, config["LTA_MONGODB_AUTH_USER"])) mongo_pass = quote_plus(cast(str, config["LTA_MONGODB_AUTH_PASS"])) mongo_host = config["LTA_MONGODB_HOST"] mongo_port = int(config["LTA_MONGODB_PORT"]) mongo_db = cast(str, config["LTA_MONGODB_DATABASE_NAME"]) lta_mongodb_url = f"mongodb://{mongo_host}:{mongo_port}/{mongo_db}" if mongo_user and mongo_pass: lta_mongodb_url = f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/{mongo_db}" ensure_mongo_indexes(lta_mongodb_url, mongo_db) motor_client = MotorClient(lta_mongodb_url) args['db'] = motor_client[mongo_db] # See: https://github.com/WIPACrepo/rest-tools/issues/2 max_body_size = int(config["LTA_MAX_BODY_SIZE"]) server = RestServer( debug=debug, max_body_size=max_body_size) # type: ignore[no-untyped-call] server.add_route(r'/', MainHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Bundles', BundlesHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Bundles/actions/bulk_create', BundlesActionsBulkCreateHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Bundles/actions/bulk_delete', BundlesActionsBulkDeleteHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Bundles/actions/bulk_update', BundlesActionsBulkUpdateHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Bundles/actions/pop', BundlesActionsPopHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Bundles/(?P<bundle_id>\w+)', BundlesSingleHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Metadata', MetadataHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Metadata/actions/bulk_create', MetadataActionsBulkCreateHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Metadata/actions/bulk_delete', MetadataActionsBulkDeleteHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/Metadata/(?P<metadata_id>\w+)', MetadataSingleHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/TransferRequests', TransferRequestsHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/TransferRequests/(?P<request_id>\w+)', TransferRequestSingleHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/TransferRequests/actions/pop', TransferRequestActionsPopHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/status', StatusHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/status/nersc', StatusNerscHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/status/(?P<component>\w+)', StatusComponentHandler, args) # type: ignore[no-untyped-call] server.add_route(r'/status/(?P<component>\w+)/count', StatusComponentCountHandler, args) # type: ignore[no-untyped-call] server.startup( address=config['LTA_REST_HOST'], port=int(config['LTA_REST_PORT'])) # type: ignore[no-untyped-call] return server
def create_server(): static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') default_config = { 'HOST': 'localhost', 'PORT': 8080, 'DEBUG': False, 'KEYCLOAK_URL': None, 'KEYCLOAK_REALM': 'IceCube', 'KEYCLOAK_CLIENT_ID': 'rest-access', 'KEYCLOAK_CLIENT_SECRET': None, 'DB_URL': 'mongodb://localhost/keycloak_user_mgmt', } config = from_environment(default_config) rest_config = { 'debug': config['DEBUG'], 'auth': { 'openid_url': f'{config["KEYCLOAK_URL"]}/auth/realms/{config["KEYCLOAK_REALM"]}' } } kwargs = RestHandlerSetup(rest_config) logging.info(f'DB: {config["DB_URL"]}') db_url, db_name = config['DB_URL'].rsplit('/', 1) db = motor.motor_asyncio.AsyncIOMotorClient(db_url) logging.info(f'DB name: {db_name}') kwargs['db'] = db[db_name] logging.info(f'Keycloak client: {config["KEYCLOAK_CLIENT_ID"]}') token_func = partial(krs.token.get_token, config["KEYCLOAK_URL"], client_id=config['KEYCLOAK_CLIENT_ID'], client_secret=config['KEYCLOAK_CLIENT_SECRET'], ) kwargs['krs_client'] = RestClient( f'{config["KEYCLOAK_URL"]}/auth/admin/realms/{config["KEYCLOAK_REALM"]}', token=token_func, timeout=10, ) main_args = { 'keycloak_url': config['KEYCLOAK_URL'], 'keycloak_realm': config['KEYCLOAK_REALM'], } server = RestServer(static_path=static_path, template_path=static_path, debug=config['DEBUG']) server.add_route('/api/all-experiments', AllExperiments, kwargs) server.add_route('/api/experiments', Experiments, kwargs) server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions', MultiInstitutions, kwargs) server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)', Institution, kwargs) server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)/users', InstitutionMultiUsers, kwargs) server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)/users/(?P<username>\w+)', InstitutionUser, kwargs) server.add_route('/api/inst_approvals', InstApprovals, kwargs) server.add_route(r'/api/inst_approvals/(?P<approval_id>\w+)/actions/approve', InstApprovalsActionApprove, kwargs) server.add_route(r'/api/inst_approvals/(?P<approval_id>\w+)/actions/deny', InstApprovalsActionDeny, kwargs) server.add_route('/api/groups', MultiGroups, kwargs) server.add_route(r'/api/groups/(?P<group_id>[\w\-]+)', Group, kwargs) server.add_route(r'/api/groups/(?P<group_id>[\w\-]+)/(?P<username>\w+)', GroupUser, kwargs) server.add_route('/api/group_approvals', GroupApprovals, kwargs) server.add_route(r'/api/group_approvals/(?P<approval_id>\w+)/actions/approve', GroupApprovalsActionApprove, kwargs) server.add_route(r'/api/group_approvals/(?P<approval_id>\w+)/actions/deny', GroupApprovalsActionDeny, kwargs) server.add_route(r'/api/(.*)', Error) server.add_route(r'/(.*)', Main, main_args) server.startup(address=config['HOST'], port=config['PORT']) return server
def start(debug: bool = False) -> RestServer: """Start a Mad Dash REST service.""" config = from_environment(EXPECTED_CONFIG) for name in config: logging.info(f"{name} = {config[name]}") args = RestHandlerSetup( { "auth": { "secret": config["MAD_DASH_AUTH_SECRET"], "issuer": config["MAD_DASH_AUTH_ISSUER"], "algorithm": config["MAD_DASH_AUTH_ALGORITHM"], }, "debug": debug, } ) # configure access to MongoDB as a backing store mongo_user = quote_plus(config["MAD_DASH_MONGODB_AUTH_USER"]) mongo_pass = quote_plus(config["MAD_DASH_MONGODB_AUTH_PASS"]) mongo_host = config["MAD_DASH_MONGODB_HOST"] mongo_port = int(config["MAD_DASH_MONGODB_PORT"]) mongodb_url = f"mongodb://{mongo_host}:{mongo_port}" if mongo_user and mongo_pass: mongodb_url = f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}" # ensure indexes md_mc = MadDashMotorClient(MotorClient(mongodb_url)) asyncio.get_event_loop().run_until_complete(md_mc.ensure_all_databases_indexes()) args["motor_client"] = MotorClient(mongodb_url) # configure REST routes server = RestServer(debug=debug) server.add_route(r"/$", MainHandler, args) server.add_route( r"/databases/names$", DatabasesNamesHandler, args ) # get database names server.add_route( r"/collections/names$", CollectionsNamesHandler, args ) # get collection names server.add_route( r"/collections/histograms/names$", CollectionsHistogramsNamesHandler, args ) # get all histogram names in collection server.add_route( r"/collections/histograms$", CollectionsHistogramsHandler, args ) # get all histogram objects in collection server.add_route(r"/histogram$", HistogramHandler, args) # get histogram object server.add_route(r"/files/names$", FileNamesHandler, args) # get file names server.startup( address=config["MAD_DASH_REST_HOST"], port=int(config["MAD_DASH_REST_PORT"]) ) return server
@role_authorization(roles=['admin', 'user']) async def get(self): """Write existing fruits""" logging.info('fruits: %r', self.fruit) self.write(self.fruit) @role_authorization(roles=['admin']) async def post(self): """Handle a new fruit""" body = json_decode(self.request.body) logging.info('body: %r', body) self.fruit[body['name']] = body self.write({}) logging.basicConfig(level=logging.DEBUG) args = RestHandlerSetup({ 'auth': { 'secret': 'secret', }, 'debug': True }) args['fruit'] = {} # this could be a DB, but a dict works for now server = RestServer(debug=True) server.add_route('/api/fruits', Fruits, args) server.startup(address='localhost', port=8080) asyncio.get_event_loop().run_forever()
def app(config): kwargs = RestHandlerSetup(config) kwargs.update({'s3': PresignedURL(**config['s3'])}) server = RestServer() server.add_route(r'/(?P<bucket>[^\?]+)/(?P<key>[^\?]+)', S3Object, kwargs) return server
def create_server(): static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') default_config = { 'HOST': 'localhost', 'PORT': 8080, 'DEBUG': False, 'DB_URL': 'mongodb://localhost/pub_db', 'COOKIE_SECRET': binascii.hexlify(b'secret').decode('utf-8'), 'BASIC_AUTH': '', # user:pass,user:pass } config = from_environment(default_config) logging.info(f'DB: {config["DB_URL"]}') db_url, db_name = config['DB_URL'].rsplit('/', 1) logging.info(f'DB name: {db_name}') db = motor.motor_asyncio.AsyncIOMotorClient(db_url) create_indexes(db_url, db_name) users = {v.split(':')[0]: v.split(':')[1] for v in config['BASIC_AUTH'].split(',') if v} logging.info(f'BASIC_AUTH users: {users.keys()}') main_args = { 'debug': config['DEBUG'], 'db': db[db_name], 'basic_auth': users, } server = RestServer(static_path=static_path, template_path=template_path, cookie_secret=config['COOKIE_SECRET'], xsrf_cookies=True, debug=config['DEBUG']) server.add_route(r'/', Main, main_args) server.add_route(r'/csv', CSV, main_args) server.add_route(r'/manage', Manage, main_args) server.add_route(r'/api/publications', APIPubs, main_args) server.add_route(r'/api/publications/count', APIPubsCount, main_args) server.add_route(r'/api/filter_defaults', APIFilterDefaults, main_args) server.add_route(r'/api/types', APITypes, main_args) server.add_route(r'/api/projects', APIProjects, main_args) server.startup(address=config['HOST'], port=config['PORT']) return server