Build a basic Flask app.
This is a basic Python HTTP server to track flight tickets and their prices. Your interviewer will explain the question to you; this README just contains usage hints.
The main libraries used in this server are Flask, an HTTP web framework, and psycopg2, a PostgreSQL client.
Getting started:
- Start the database with:
bin/start_db.sh - In a separate window, install dependencies, run migrations, and start the server:
source bin/install_deps.sh./migrate.py./app.py
- The server starts by default on
localhost:8080. - To test further changes, you can use:
curl localhost:8080/flights/1 - Finally, you can get a PostgreSQL shell with:
bin/attach_db.sh
Migrations are run via the yoyo package. You can add new migrations in the migrations directory with a number.name.sql filename, and you can run migrations with the ./migrate.py command.
Implement a basic Flask app with:
- A ping endpoint that returns a JSON object with
message: pong. - A single flight endpoint at
/flights/<int:id>. - Fetch the flight object from the database using
flights.getFlight(db, id). - If an exception occurs, log it and return a 500 response.
- If the flight is not found, return a 404 response with a JSON message.
- If found, return the flight data as JSON.
- Start the app by listening on the configured server port.
This problem asks you to build a small Flask service for flight-booking data. The key tasks are to expose a health-check endpoint returning pong and a parameterized /flights/<int:id> endpoint that looks up one flight from PostgreSQL through the provided data-access layer. You need to handle failures with a 500 response, return 404 when no flight is found, and serialize the flight record as JSON when the lookup succeeds. The main focus is on Flask routing, database access, and clean API response handling.