Esempio n. 1
0
def create_app():
    """Creates a new application instance.

  Returns:
    A configured WSGI application instance.
  """
    # Database connectivity variables.
    db_type = os.environ.get('UWSOLAR_DB_TYPE', 'sqlite')
    db_user = os.environ.get('UWSOLAR_DB_USER', '')
    db_password = os.environ.get('UWSOLAR_DB_PASSWORD', '')
    db_host = os.environ.get('UWSOLAR_DB_HOST', 'sqlite.db')
    db_name = os.environ.get('UWSOLAR_DB_NAME', '')
    db_pool_size = os.environ.get('UWSOLAR_DB_POOL_SIZE', 0)

    # Solar panel connectivity variables.
    panel_metrics_workbook = os.environ.get('UWSOLAR_PANEL_METRICS_WORKBOOK')
    panel_metrics_worksheet_name = os.environ.get(
        'UWSOLAR_PANEL_METRICS_WORKSHEET_NAME', 'Metrics')
    panel_topic_prefix = os.environ.get('UWSOLAR_PANEL_TOPIC_PREFIX')
    panel_host = os.environ.get('UWSOLAR_PANEL_HOST')

    # Initialize database connection.
    db_opts = db_accessor.DatabaseOptions(db_type, db_user, db_password,
                                          db_host, db_name, db_pool_size)
    db_con = db_accessor.DatabaseAccessor(db_opts)

    # Initialize solar panel connection.
    panel_metrics = metrics_builder.build_metrics(
        panel_metrics_workbook, panel_metrics_worksheet_name,
        panel_topic_prefix)
    panel_con = panel_accessor.PanelAccessor(panel_host, panel_metrics)

    # Create application instance.
    return api_server.ApiServer(db_con, panel_con).app()
Esempio n. 2
0
def main():
    """Parses command line arguments and initilizes the server."""
    args = parse_arguments()
    logging.basicConfig(level=logging.getLevelName(args.log_level))

    # Initialize database connection.
    db_opts = db_accessor.DatabaseOptions(args.db_type, args.db_user,
                                          args.db_password, args.db_host,
                                          args.db_name, args.db_pool_size)
    db_con = db_accessor.DatabaseAccessor(db_opts)

    # Initialize solar panel connection.
    panel_metrics = metrics_builder.build_metrics(
        args.panel_metrics_workbook, args.panel_metrics_worksheet_name,
        args.panel_topic_prefix)
    panel_con = panel_accessor.PanelAccessor(args.panel_host, panel_metrics)

    # Initialize and run API server.
    app = api_server.ApiServer(db_con, panel_con).app()
    bottle.run(app=app, host=args.host, port=args.port, debug=args.debug)