Skip to content

cloud-gov/pages-example-website-api

Repository files navigation

Pages

Static Badge Static Badge

API Application

Intro

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

Where to input your own data

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}

Set Up

Prequesites

Implementation

  1. Login to cloud.gov via the terminal with cf login -a api.fr.cloud.gov --sso.
  2. Follow the cloud.gov documentation to provision a aws-rds micro-psql database instance.
  3. 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.
  4. Import the CSV file into the created table.
  5. Fork this repository and clone it directly to your machine.
  6. Move into the forked repository with cd /path/to/forked/repository.
  7. In whatever IDE of your choice input your own specific data where applicable.
  8. Run pip install -r requirements.txt to install the necessary modules.
  9. Deploy the app into your space with cf push.
  10. Verify that the application is in the space with cf apps

To bind via the CLI instead of using the manifest.yml file

  1. Bind the application to the RDS service instance previously provisioned with the cf CLI.
  2. Redeploy the application with cf restart app-name

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages