This python flask application provides a way to interact with a PostgreSQL database by providing a secure connection using Cloud Foundry environment variables and return the result as JSON to a static website hosted on Pages.
Functions
- Connect to the RDS service instance
- Access the database
- Execute a SQL query
- Fetch data from the database
- Return the result as JSON
There are a few instances in the app.py
and the manifest.yml
file where you will need to inject your own data that is specific to your project/work.
In app.py
Line 11: CORS(app, origins=['https://your-pages-url'], headers=['Content-Type'], methods=['GET'])
when launching the application for the first time this line can be ommitted until you are ready for your Pages site to hit the API endpoint
Line 13: port = int(os.getenv('PORT', {whatever port you wish to expose})
Line 15: aws_rds = app_env.get_service(name='your-db-name')
Optional if you wish to serve from the root URL or not
Line 24: @app.route('/', methods=['GET'])
Line 26: @app.route('/your_sub_route', methods=['GET'])
Line 32: query = 'SELECT * FROM {your_table} LIMIT 15'
In manifest.yml
Line 3: - name: your_app_name
Line 7: - {your_db_name}
- Pages sandbox or existing account.
- Pages site to display the data. the site repository linked is the example site created in conjunction for and with this application, feel free to utilize that one for testing purposes or create your own.
- Cloud Foundry CLI
- Cloud Foundry CLI Service Connection Plugin
- cloud.gov sandbox or existing account with the following security groups applied
trusted_local_networks_egress
andpublic-egress
. - PostgrSQL v15 database within cloud.gov.
- Dataset to
COPY
into the PostgreSQL database *for this example we used a CSV file from data.gov. - Python or download via Homebrew.
- pip
- Login to cloud.gov via the terminal with
cf login -a api.fr.cloud.gov --sso
. - Follow the cloud.gov documentation to provision a
aws-rds
micro-psql
database instance. - Via the CF service connection plugin connect to your databse and create a table with column(s) containing the proper headers according to your CSV file dataset.
- Import the CSV file into the created table.
- Fork this repository and clone it directly to your machine.
- Move into the forked repository with
cd /path/to/forked/repository
. - In whatever IDE of your choice input your own specific data where applicable.
- Run
pip install -r requirements.txt
to install the necessary modules. - Deploy the app into your space with
cf push
. - Verify that the application is in the space with
cf apps
To bind via the CLI instead of using the manifest.yml file
- Bind the application to the RDS service instance previously provisioned with the cf CLI.
- Redeploy the application with
cf restart app-name