(config=None)
return app
Create the Flask app instance that is used throughout the application.

    Args:
        config: Path to configuration file as a string or an object with config
        directives.

    Returns:
        Application object (instance of flask.Flask).
Create the Flask app instance that is used throughout the application.
Create the Flask app instance that is used throughout the application .
def create_app(config=None):
    """Create the Flask app instance that is used throughout the application.

    Args:
        config: Path to configuration file as a string or an object with config
        directives.

    Returns:
        Application object (instance of flask.Flask).
    """
    template_folder = 'frontend/dist'
    static_folder = 'frontend/dist'

    app = Flask(
        __name__,
        template_folder=template_folder,
        static_folder=static_folder
    )

    if not config:
        # Where to find the config file
        default_path = '/etc/timesketch/timesketch.conf'
        # Fall back to legacy location of the config file
        legacy_path = '/etc/timesketch.conf'
        if os.path.isfile(default_path):
            config = default_path
        else:
            config = legacy_path

    if isinstance(config, six.text_type):
        os.environ['TIMESKETCH_SETTINGS'] = config
        try:
            app.config.from_envvar('TIMESKETCH_SETTINGS')

            if 'EMAIL_USER_WHITELIST' in app.config:
                sys.stderr.write(
                    'Warning, EMAIL_USER_WHITELIST has been deprecated. '
                    'Please update timesketch.conf.')
        except IOError:
            sys.stderr.write(
                'Config file {0} does not exist.\n'.format(config))
            sys.exit()
    else:
        app.config.from_object(config)

    # Make sure that SECRET_KEY is configured.
    if not app.config['SECRET_KEY']:
        sys.stderr.write('ERROR: Secret key not present. '
                         'Please update your configuration.\n'
                         'To generate a key you can use openssl:\n\n'
                         '$ openssl rand -base64 32\n\n')
        sys.exit()

    # Support old style config using Elasticsearch as backend.
    # TODO: Deprecate the old ELASTIC_* config in 2023.
    if not app.config.get('OPENSEARCH_HOST'):
        sys.stderr.write('Deprecated config field found: ELASTIC_HOST. '
                         'Update your config to use OPENSEARCH_HOST.\n')
        app.config['OPENSEARCH_HOST'] = app.config.get('ELASTIC_HOST')

    if not app.config.get('OPENSEARCH_PORT'):
        sys.stderr.write('Deprecated config field found: ELASTIC_PORT. '
                         'Update your config to use OPENSEARCH_PORT.\n')
        app.config['OPENSEARCH_PORT'] = app.config.get('ELASTIC_PORT')

    # Plaso version that we support
    if app.config['UPLOAD_ENABLED']:
        try:
            # pylint: disable=import-outside-toplevel
            from plaso import __version__ as plaso_version
            app.config['PLASO_VERSION'] = plaso_version
        except ImportError:
            pass

    # Setup the database.
    configure_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    db = init_db()

    # Alembic migration support:
    # http://alembic.zzzcomputing.com/en/latest/
    migrate = Migrate()
    migrate.init_app(app, db)

    # Register blueprints. Blueprints are a way to organize your Flask
    # Flask application. See this for more information:
    # http://flask.pocoo.org/docs/latest/blueprints/
    app.register_blueprint(spa_views)
    app.register_blueprint(auth_views)

    # Setup URL routes for the API.
    api_v1 = Api(app, prefix='/api/v1')
    for route in V1_API_ROUTES:
        api_v1.add_resource(*route)

    # Register error handlers
    # pylint: disable=unused-variable
    @app.errorhandler(ApiHTTPError)
    def handle_api_http_error(error):
        """Error handler for API HTTP errors.

        Returns:
            HTTP response object (instance of flask.wrappers.Response)
        """
        return error.build_response()

    # Setup the login manager.
    login_manager = LoginManager()
    login_manager.init_app(app)
    login_manager.login_view = 'user_views.login'

    # This is used by the flask_login extension.
    # pylint: disable=unused-variable
    @login_manager.user_loader
    def load_user(user_id):
        """Based on a user_id (database primary key for a user) this function
        loads a user from the database. It is used by the Flask-Login extension
        to setup up the session for the user.

        Args:
            user_id: Integer primary key for the user.

        Returns:
            A user object (Instance of timesketch.models.user.User).
        """
        return User.query.get(user_id)

    # Setup CSRF protection for the whole application
    CSRFProtect(app)

    return app
def create_app ( config = None ) : template_folder = 'frontend/dist' static_folder = 'frontend/dist' app = Flask ( __name__ , template_folder = template_folder , static_folder = static_folder ) if not config : # Where to find the config file default_path = '/etc/timesketch/timesketch.conf' # Fall back to legacy location of the config file legacy_path = '/etc/timesketch.conf' if os . path . isfile ( default_path ) : config = default_path else : config = legacy_path if isinstance ( config , six . text_type ) : os . environ [ 'TIMESKETCH_SETTINGS' ] = config try : app . config . from_envvar ( 'TIMESKETCH_SETTINGS' ) if 'EMAIL_USER_WHITELIST' in app . config : sys . stderr . write ( 'Warning, EMAIL_USER_WHITELIST has been deprecated. ' 'Please update timesketch.conf.' ) except IOError : sys . stderr . write ( 'Config file {0} does not exist.\n' . format ( config ) ) sys . exit ( ) else : app . config . from_object ( config ) # Make sure that SECRET_KEY is configured. if not app . config [ 'SECRET_KEY' ] : sys . stderr . write ( 'ERROR: Secret key not present. ' 'Please update your configuration.\n' 'To generate a key you can use openssl:\n\n' '$ openssl rand -base64 32\n\n' ) sys . exit ( ) # Support old style config using Elasticsearch as backend. # TODO: Deprecate the old ELASTIC_* config in 2023. if not app . config . get ( 'OPENSEARCH_HOST' ) : sys . stderr . write ( 'Deprecated config field found: ELASTIC_HOST. ' 'Update your config to use OPENSEARCH_HOST.\n' ) app . config [ 'OPENSEARCH_HOST' ] = app . config . get ( 'ELASTIC_HOST' ) if not app . config . get ( 'OPENSEARCH_PORT' ) : sys . stderr . write ( 'Deprecated config field found: ELASTIC_PORT. ' 'Update your config to use OPENSEARCH_PORT.\n' ) app . config [ 'OPENSEARCH_PORT' ] = app . config . get ( 'ELASTIC_PORT' ) # Plaso version that we support if app . config [ 'UPLOAD_ENABLED' ] : try : # pylint: disable=import-outside-toplevel from plaso import __version__ as plaso_version app . config [ 'PLASO_VERSION' ] = plaso_version except ImportError : pass # Setup the database. configure_engine ( app . config [ 'SQLALCHEMY_DATABASE_URI' ] ) db = init_db ( ) # Alembic migration support: # http://alembic.zzzcomputing.com/en/latest/ migrate = Migrate ( ) migrate . init_app ( app , db ) # Register blueprints. Blueprints are a way to organize your Flask # Flask application. See this for more information: # http://flask.pocoo.org/docs/latest/blueprints/ app . register_blueprint ( spa_views ) app . register_blueprint ( auth_views ) # Setup URL routes for the API. api_v1 = Api ( app , prefix = '/api/v1' ) for route in V1_API_ROUTES : api_v1 . add_resource ( * route ) # Register error handlers # pylint: disable=unused-variable @ app . errorhandler ( ApiHTTPError ) def handle_api_http_error ( error ) : """Error handler for API HTTP errors.

        Returns:
            HTTP response object (instance of flask.wrappers.Response)
        """ return error . build_response ( ) # Setup the login manager. login_manager = LoginManager ( ) login_manager . init_app ( app ) login_manager . login_view = 'user_views.login' # This is used by the flask_login extension. # pylint: disable=unused-variable @ login_manager . user_loader def load_user ( user_id ) : """Based on a user_id (database primary key for a user) this function
        loads a user from the database. It is used by the Flask-Login extension
        to setup up the session for the user.

        Args:
            user_id: Integer primary key for the user.

        Returns:
            A user object (Instance of timesketch.models.user.User).
        """ return User . query . get ( user_id ) # Setup CSRF protection for the whole application CSRFProtect ( app ) return app