Skip to content

one2nc/harshwardhan-sre-bootcamp

Repository files navigation

Student Management API

A simple RESTful API built with Flask for managing student records. This application provides CRUD (Create, Read, Update, Delete) operations for student data with SQLite database support.

Features

  • GET /api/v1/students - Retrieve all students
  • GET /api/v1/students/{id} - Retrieve a specific student by ID
  • POST /api/v1/students - Create a new student
  • PUT /api/v1/students/{id} - Update an existing student
  • DELETE /api/v1/students/{id} - Delete a student
  • GET /healthcheck - API health status

Tech Stack

  • Flask - Web framework
  • SQLAlchemy - ORM for database operations
  • Flask-Migrate - Database migration support
  • SQLite - Database (configurable via environment variables)
  • Python-dotenv - Environment variable management

Installation

Prerequisites

  • Python 3.7 or higher
  • Make (optional, for using Makefile commands)
  • Docker (optional)
  • Docker-compose (optional)
  • Vagrant (optional)
  • VirtualBox (for Vagrant)
  • Minikube (Docker Required)

Setup

  1. Clone the repository:
git clone <repository-url>
cd student-management-api
  1. Create a virtual environment and install dependencies:
make venv/bin/activate

Or manually:

python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
  1. Create a .env file in the root directory(for developement):
DB_URL=postgresql://user:password@host:port/dbname
POSTGRES_DB=mydatabase
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypassword

Usage

Development Mode

make serve-dev

This will:

  • Run database migrations
  • Start the Flask development server on http://localhost:5000

Production Mode

make serve-prod

This will:

  • Run database migrations
  • Start the application using Gunicorn

Docker

If you have Docker.You can use the below commands:

make build
make serve-docker

Vagrant

If you want to use Vagrant.You can use the below commands:

sudo make serve-vagrant

Minikube

If you want to use Minikube.You can use the below commands:

make minikube-setup # this will setup a (1+3) node cluster
make minikube-start # Resume the cluster
make minikube-stop # Pause the cluster

#Once the minikube cluster is up and healthy
make minikube-deploy  # this will deploy the application

Make sure to update DB details in the k8s/scripts/vault_setup.sh For the Minikube approach for now I am using a hack for setting up the permissions on the the hostpath volume as a workaround to that bug. Link to github Issue

Database Schema

Student Model

  • id (Integer, Primary Key) - Auto-generated student ID
  • first_name (String, Required) - Student's first name
  • last_name (String, Required) - Student's last name
  • age (Integer, Required) - Student's age
  • email (String, Required, Unique) - Student's email address

Sample Payload

{
    "first_name": "a",
    "last_name": "b",
    "age": 10,
    "email": "asdc3@gmail.com"
}

Development

Database Migrations

To create a new migration:

make migrate

Running Tests

source venv/bin/activate
python -m pytest tests/ -v

Clean Up

To remove virtual environment and cache files:

make clean

Configuration

The application uses environment variables for configuration:

  • DB_URL - Database connection string (Example: sqlite:///students.db or postgresql://user:password@host:port/dbname)
  • POSTGRES_DB - Database Name (Only if using postgres)
  • POSTGRES_USER - Database Username (Only if using postgres)
  • POSTGRES_PASSWORD - Database Password (Only if using postgres)

Project Structure

student-management-api/
├── app.py              # Main application file
├── requirements.txt    # Python dependencies
├── Makefile           # Build and deployment commands
├── .env               # Environment variables
├── migrations/        # Database migration files
├── tests/            # Unit tests
└── README.md         # This file

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published