def configure(config="DefaultConfig", config_file=None): configStr = 'ga4gh.serverconfig:{0}'.format(config) app.config.from_object(configStr) if os.environ.get('GA4GH_CONFIGURATION') is not None: app.config.from_envvar('GA4GH_CONFIGURATION') if config_file is not None: app.config.from_pyfile(args.config_file) cors.CORS(app, allow_headers='Content-Type')
def boot(application, config): if config and config['origins']: try: from flask.ext import cors for i in config.keys(): application.config['CORS_%s' % i.upper()] = config[i] cors.CORS(application) except Exception as e: raise Exception('Failed to init cors support %s' % e)
def create(): app = flask.Flask(__name__, static_folder=None) app.logger.setLevel(logging.DEBUG) cors.CORS(app) db_url = 'sqlite:///{}/buckit.sqlite'.format(os.getcwd()) app.config['SQLALCHEMY_DATABASE_URI'] = db_url app.db = SQLAlchemy(app) buckit.routes.initialize(app) return app
def configure(configFile=None, baseConfig="ProductionConfig", extraConfig={}): """ TODO Document this critical function! What does it do? What does it assume? """ configStr = 'ga4gh.serverconfig:{0}'.format(baseConfig) app.config.from_object(configStr) if os.environ.get('GA4GH_CONFIGURATION') is not None: app.config.from_envvar('GA4GH_CONFIGURATION') if configFile is not None: app.config.from_pyfile(configFile) app.config.update(extraConfig.items()) # Setup CORS cors.CORS(app, allow_headers='Content-Type') app.serverStatus = ServerStatus() # Allocate the backend # TODO is this a good way to determine what type of backend we should # instantiate? We should think carefully about this. The approach of # using the special strings __SIMULATED__ and __EMPTY__ seems OK for # now, but is certainly not ideal. dataSource = app.config["DATA_SOURCE"] if dataSource == "__SIMULATED__": randomSeed = app.config["SIMULATED_BACKEND_RANDOM_SEED"] numCalls = app.config["SIMULATED_BACKEND_NUM_CALLS"] variantDensity = app.config["SIMULATED_BACKEND_VARIANT_DENSITY"] numVariantSets = app.config["SIMULATED_BACKEND_NUM_VARIANT_SETS"] theBackend = backend.SimulatedBackend(randomSeed, numCalls, variantDensity, numVariantSets) elif dataSource == "__EMPTY__": theBackend = backend.EmptyBackend() else: theBackend = backend.FileSystemBackend(dataSource) theBackend.setRequestValidation(app.config["REQUEST_VALIDATION"]) theBackend.setResponseValidation(app.config["RESPONSE_VALIDATION"]) theBackend.setDefaultPageSize(app.config["DEFAULT_PAGE_SIZE"]) theBackend.setMaxResponseLength(app.config["MAX_RESPONSE_LENGTH"]) app.backend = theBackend
app = Flask(__name__) app.debug = True app.config['SQLALCHEMY_DATABASE_URI'] = sqla_conn_string() app.config['APISPEC_FORMAT_RESPONSE'] = None app.config['SQLALCHEMY_REPLICA_TASKS'] = [ 'webservices.tasks.download.export_query', ] app.config['SQLALCHEMY_FOLLOWERS'] = [ sa.create_engine(follower.strip()) for follower in env.get_credential('SQLA_FOLLOWERS', '').split(',') if follower.strip() ] # app.config['SQLALCHEMY_ECHO'] = True db.init_app(app) cors.CORS(app) class FlaskRestParser(FlaskParser): def handle_error(self, error): message = error.messages status_code = getattr(error, 'status_code', 422) raise exceptions.ApiError(message, status_code) parser = FlaskRestParser() app.config['APISPEC_WEBARGS_PARSER'] = parser v1 = Blueprint('v1', __name__, url_prefix='/v1') api = restful.Api(v1)
def configure(configFile=None, baseConfig="ProductionConfig", port=8000, extraConfig={}): """ TODO Document this critical function! What does it do? What does it assume? """ file_handler = StreamHandler() file_handler.setLevel(logging.WARNING) app.logger.addHandler(file_handler) configStr = 'ga4gh.server.serverconfig:{0}'.format(baseConfig) app.config.from_object(configStr) if os.environ.get('GA4GH_CONFIGURATION') is not None: app.config.from_envvar('GA4GH_CONFIGURATION') if configFile is not None: app.config.from_pyfile(configFile) app.config.update(extraConfig.items()) # Setup file handle cache max size datamodel.fileHandleCache.setMaxCacheSize( app.config["FILE_HANDLE_CACHE_MAX_SIZE"]) # Setup CORS try: cors.CORS(app, allow_headers='Content-Type') except AssertionError: pass app.serverStatus = ServerStatus() app.backend = _configure_backend(app) if app.config.get('SECRET_KEY'): app.secret_key = app.config['SECRET_KEY'] elif app.config.get('OIDC_PROVIDER'): raise exceptions.ConfigurationException( 'OIDC configuration requires a secret key') if app.config.get('CACHE_DIRECTORY'): app.cache_dir = app.config['CACHE_DIRECTORY'] else: app.cache_dir = '/tmp/ga4gh' app.cache = FileSystemCache( app.cache_dir, threshold=5000, default_timeout=600, mode=384) app.oidcClient = None app.myPort = port if app.config.get('AUTH0_ENABLED'): emails = app.config.get('AUTH0_AUTHORIZED_EMAILS', '').split(',') [auth.authorize_email(e, app.cache) for e in emails] if "OIDC_PROVIDER" in app.config: # The oic client. If we're testing, we don't want to verify # SSL certificates app.oidcClient = oic.oic.Client( verify_ssl=('TESTING' not in app.config)) try: app.oidcClient.provider_config(app.config['OIDC_PROVIDER']) except requests.exceptions.ConnectionError: configResponse = message.ProviderConfigurationResponse( issuer=app.config['OIDC_PROVIDER'], authorization_endpoint=app.config['OIDC_AUTHZ_ENDPOINT'], token_endpoint=app.config['OIDC_TOKEN_ENDPOINT'], revocation_endpoint=app.config['OIDC_TOKEN_REV_ENDPOINT']) app.oidcClient.handle_provider_config(configResponse, app.config['OIDC_PROVIDER']) # The redirect URI comes from the configuration. # If we are testing, then we allow the automatic creation of a # redirect uri if none is configured redirectUri = app.config.get('OIDC_REDIRECT_URI') if redirectUri is None and app.config.get('TESTING'): redirectUri = 'https://{0}:{1}/oauth2callback'.format( socket.gethostname(), app.myPort) app.oidcClient.redirect_uris = [redirectUri] if redirectUri is []: raise exceptions.ConfigurationException( 'OIDC configuration requires a redirect uri') # We only support dynamic registration while testing. if ('registration_endpoint' in app.oidcClient.provider_info and app.config.get('TESTING')): app.oidcClient.register( app.oidcClient.provider_info["registration_endpoint"], redirect_uris=[redirectUri]) else: response = message.RegistrationResponse( client_id=app.config['OIDC_CLIENT_ID'], client_secret=app.config['OIDC_CLIENT_SECRET'], redirect_uris=[redirectUri], verify_ssl=False) app.oidcClient.store_registration_info(response)
def configure(configFile=None, baseConfig="ProductionConfig", port=8000, extraConfig={}): """ TODO Document this critical function! What does it do? What does it assume? """ configStr = 'ga4gh.serverconfig:{0}'.format(baseConfig) app.config.from_object(configStr) if os.environ.get('GA4GH_CONFIGURATION') is not None: app.config.from_envvar('GA4GH_CONFIGURATION') if configFile is not None: app.config.from_pyfile(configFile) app.config.update(extraConfig.items()) # Setup file handle cache max size datamodel.fileHandleCache.setMaxCacheSize( app.config["FILE_HANDLE_CACHE_MAX_SIZE"]) # Setup CORS cors.CORS(app, allow_headers='Content-Type') app.serverStatus = ServerStatus() # Allocate the backend # We use URLs to specify the backend. Currently we have file:// URLs (or # URLs with no scheme) for the FileSystemBackend, and special empty:// and # simulated:// URLs for empty or simulated data sources. dataSource = urlparse.urlparse(app.config["DATA_SOURCE"], "file") if dataSource.scheme == "simulated": # Ignore the query string randomSeed = app.config["SIMULATED_BACKEND_RANDOM_SEED"] numCalls = app.config["SIMULATED_BACKEND_NUM_CALLS"] variantDensity = app.config["SIMULATED_BACKEND_VARIANT_DENSITY"] numVariantSets = app.config["SIMULATED_BACKEND_NUM_VARIANT_SETS"] numReferenceSets = app.config[ "SIMULATED_BACKEND_NUM_REFERENCE_SETS"] numReferencesPerReferenceSet = app.config[ "SIMULATED_BACKEND_NUM_REFERENCES_PER_REFERENCE_SET"] numAlignmentsPerReadGroup = app.config[ "SIMULATED_BACKEND_NUM_ALIGNMENTS_PER_READ_GROUP"] dataRepository = datarepo.SimulatedDataRepository( randomSeed=randomSeed, numCalls=numCalls, variantDensity=variantDensity, numVariantSets=numVariantSets, numReferenceSets=numReferenceSets, numReferencesPerReferenceSet=numReferencesPerReferenceSet, numAlignments=numAlignmentsPerReadGroup) elif dataSource.scheme == "empty": dataRepository = datarepo.EmptyDataRepository() elif dataSource.scheme == "file": dataRepository = datarepo.FileSystemDataRepository(os.path.join( dataSource.netloc, dataSource.path)) dataRepository.checkConsistency() else: raise exceptions.ConfigurationException( "Unsupported data source scheme: " + dataSource.scheme) theBackend = backend.Backend(dataRepository) theBackend.setRequestValidation(app.config["REQUEST_VALIDATION"]) theBackend.setResponseValidation(app.config["RESPONSE_VALIDATION"]) theBackend.setDefaultPageSize(app.config["DEFAULT_PAGE_SIZE"]) theBackend.setMaxResponseLength(app.config["MAX_RESPONSE_LENGTH"]) app.backend = theBackend app.secret_key = os.urandom(SECRET_KEY_LENGTH) app.oidcClient = None app.tokenMap = None app.myPort = port if "OIDC_PROVIDER" in app.config: # The oic client. If we're testing, we don't want to verify # SSL certificates app.oidcClient = oic.oic.Client( verify_ssl=('TESTING' not in app.config)) app.tokenMap = {} try: app.oidcClient.provider_config(app.config['OIDC_PROVIDER']) except requests.exceptions.ConnectionError: configResponse = message.ProviderConfigurationResponse( issuer=app.config['OIDC_PROVIDER'], authorization_endpoint=app.config['OIDC_AUTHZ_ENDPOINT'], token_endpoint=app.config['OIDC_TOKEN_ENDPOINT'], revocation_endpoint=app.config['OIDC_TOKEN_REV_ENDPOINT']) app.oidcClient.handle_provider_config(configResponse, app.config['OIDC_PROVIDER']) # The redirect URI comes from the configuration. # If we are testing, then we allow the automatic creation of a # redirect uri if none is configured redirectUri = app.config.get('OIDC_REDIRECT_URI') if redirectUri is None and 'TESTING' in app.config: redirectUri = 'https://{0}:{1}/oauth2callback'.format( socket.gethostname(), app.myPort) app.oidcClient.redirect_uris = [redirectUri] if redirectUri is []: raise exceptions.ConfigurationException( 'OIDC configuration requires a redirect uri') # We only support dynamic registration while testing. if ('registration_endpoint' in app.oidcClient.provider_info and 'TESTING' in app.config): app.oidcClient.register( app.oidcClient.provider_info["registration_endpoint"], redirect_uris=[redirectUri]) else: response = message.RegistrationResponse( client_id=app.config['OIDC_CLIENT_ID'], client_secret=app.config['OIDC_CLIENT_SECRET'], redirect_uris=[redirectUri], verify_ssl=False) app.oidcClient.store_registration_info(response)
def configure(configFile=None, baseConfig="ProductionConfig", port=8000, extraConfig={}): """ TODO Document this critical function! What does it do? What does it assume? """ configStr = 'ga4gh.serverconfig:{0}'.format(baseConfig) app.config.from_object(configStr) if os.environ.get('GA4GH_CONFIGURATION') is not None: app.config.from_envvar('GA4GH_CONFIGURATION') if configFile is not None: app.config.from_pyfile(configFile) app.config.update(extraConfig.items()) # Setup file handle cache max size datamodel.fileHandleCache.setMaxCacheSize( app.config["FILE_HANDLE_CACHE_MAX_SIZE"]) # Setup CORS cors.CORS(app, allow_headers='Content-Type') app.serverStatus = ServerStatus() # Allocate the backend # TODO is this a good way to determine what type of backend we should # instantiate? We should think carefully about this. The approach of # using the special strings __SIMULATED__ and __EMPTY__ seems OK for # now, but is certainly not ideal. dataSource = app.config["DATA_SOURCE"] if dataSource == "__SIMULATED__": randomSeed = app.config["SIMULATED_BACKEND_RANDOM_SEED"] numCalls = app.config["SIMULATED_BACKEND_NUM_CALLS"] variantDensity = app.config["SIMULATED_BACKEND_VARIANT_DENSITY"] numVariantSets = app.config["SIMULATED_BACKEND_NUM_VARIANT_SETS"] numReferenceSets = app.config["SIMULATED_BACKEND_NUM_REFERENCE_SETS"] numReferencesPerReferenceSet = app.config[ "SIMULATED_BACKEND_NUM_REFERENCES_PER_REFERENCE_SET"] numAlignments = app.config[ "SIMULATED_BACKEND_NUM_ALIGNMENTS_PER_READ_GROUP"] theBackend = backend.SimulatedBackend(randomSeed, numCalls, variantDensity, numVariantSets, numReferenceSets, numReferencesPerReferenceSet, numAlignments) elif dataSource == "__EMPTY__": theBackend = backend.EmptyBackend() else: theBackend = backend.FileSystemBackend(dataSource) theBackend.setRequestValidation(app.config["REQUEST_VALIDATION"]) theBackend.setResponseValidation(app.config["RESPONSE_VALIDATION"]) theBackend.setDefaultPageSize(app.config["DEFAULT_PAGE_SIZE"]) theBackend.setMaxResponseLength(app.config["MAX_RESPONSE_LENGTH"]) app.backend = theBackend app.secret_key = os.urandom(SECRET_KEY_LENGTH) app.oidcClient = None app.tokenMap = None app.myPort = port if "OIDC_PROVIDER" in app.config: # The oic client. If we're testing, we don't want to verify # SSL certificates app.oidcClient = oic.oic.Client( verify_ssl=('TESTING' not in app.config)) app.tokenMap = {} try: app.oidcClient.provider_config(app.config['OIDC_PROVIDER']) except requests.exceptions.ConnectionError: configResponse = message.ProviderConfigurationResponse( issuer=app.config['OIDC_PROVIDER'], authorization_endpoint=app.config['OIDC_AUTHZ_ENDPOINT'], token_endpoint=app.config['OIDC_TOKEN_ENDPOINT'], revocation_endpoint=app.config['OIDC_TOKEN_REV_ENDPOINT']) app.oidcClient.handle_provider_config(configResponse, app.config['OIDC_PROVIDER']) # The redirect URI comes from the configuration. # If we are testing, then we allow the automatic creation of a # redirect uri if none is configured redirectUri = app.config.get('OIDC_REDIRECT_URI') if redirectUri is None and 'TESTING' in app.config: redirectUri = 'https://{0}:{1}/oauth2callback'.format( socket.gethostname(), app.myPort) app.oidcClient.redirect_uris = [redirectUri] if redirectUri is []: raise exceptions.ConfigurationException( 'OIDC configuration requires a redirect uri') # We only support dynamic registration while testing. if ('registration_endpoint' in app.oidcClient.provider_info and 'TESTING' in app.config): app.oidcClient.register( app.oidcClient.provider_info["registration_endpoint"], redirect_uris=[redirectUri]) else: response = message.RegistrationResponse( client_id=app.config['OIDC_CLIENT_ID'], client_secret=app.config['OIDC_CLIENT_SECRET'], redirect_uris=[redirectUri], verify_ssl=False) app.oidcClient.store_registration_info(response)
""" from __future__ import division from __future__ import print_function from __future__ import unicode_literals import os import flask import flask.ext.api as api import flask.ext.cors as cors app = api.FlaskAPI(__name__) app.config.from_object('ga4gh.server.config:DefaultConfig') if os.environ.get('GA4GH_CONFIGURATION') is not None: app.config.from_envvar('GA4GH_CONFIGURATION') cors.CORS(app, allow_headers='Content-Type') def handleHTTPPost(request, endpoint): """ Handles the specified HTTP POST request, which maps to the specified protocol handler handpoint and protocol request class. """ mimetype = "application/json" if request.mimetype != mimetype: raise api.exceptions.UnsupportedMediaType() responseStr = endpoint(request.get_data()) return flask.Response(responseStr, status=200, mimetype=mimetype) def handleHTTPOptions():
import flask_sqlalchemy import datetime import sqlalchemy import flask_potion import flask_potion.fields as fields import flask import flask.ext.cors as cors import calendar app = flask.Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = flask_sqlalchemy.SQLAlchemy(app) api = flask_potion.Api(app) cors.CORS(app, supports_credentials=True, headers=['Content-Type'], expose_headers=['Link', 'X-Total-Count']) class Client(db.Model): __table_args__ = (sqlalchemy.UniqueConstraint('name'), ) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) projects = sqlalchemy.orm.relationship( "Project", cascade="delete", backref=sqlalchemy.orm.backref('client'), ) workentries = sqlalchemy.orm.relationship( "WorkEntry",