Classroom API is a simple Go-based API for managing students, classes, enrollments, and comments. The project uses MySQL as its database and is containerized using Docker and Docker Compose.
- Manage Students
- Manage Classes
- Manage Enrollments
- Manage Comments
- Seed initial data, including fetching comments from an external API
- Docker installed
- Docker Compose installed
.
├── docker-compose.yml # Docker Compose file for services
├── Dockerfile # Dockerfile for building the API service
├── go.mod # Go module dependencies
├── go.sum # Go module checksum
├── main.go # Entry point for the API
├── models.go # Model definitions for database
├── db.go # Database connection and initialization
├── seed.go # Data seeding logic
git clone <repository-url>
cd classroom-api
Run the following command to start the MySQL and API services:
docker-compose up --build
This will:
- Start a MySQL container with a
classroom_db
database. - Build and run the API container.
The API will be available at http://localhost:8080
.
The following environment variables are used:
Variable | Description | Default Value |
---|---|---|
DB_DSN |
Database connection string | root:password@tcp(mysql:3306)/classroom_db?charset=utf8mb4&parseTime=True&loc=Local |
- GET
/students
- Retrieve all students
- GET
/classes
- Retrieve all classes
- GET
/enrollments
- Retrieve all enrollments
- POST
/comments
- Create a new comment - GET
/comments
- Retrieve all comments - GET
/comments/:id
- Retrieve a specific comment by ID - DELETE
/comments/:id
- Delete a comment by ID
The application seeds initial data for classes, students, enrollments, and comments:
- Initial data for classes and students is defined in
seed.go
. - Comments are fetched from JSONPlaceholder.
- MySQL container has a health check to ensure it's running before starting the API service.
You can run the API locally without Docker by setting up the environment:
-
Install Go (1.23.5 or higher).
-
Install MySQL and set up the
classroom_db
database. -
Set the
DB_DSN
environment variable in your shell:export DB_DSN="root:password@tcp(127.0.0.1:3306)/classroom_db?charset=utf8mb4&parseTime=True&loc=Local"
-
Run the application:
go run .
Use tools like Postman or curl to test the endpoints:
Example:
curl http://localhost:8080/students
You can find the complete API documentation here:
👉 Published API Documentation on Postman
This task is completed with the help of chatgpt