A robust RESTful API built with Golang 1.25 to manage student records. This project demonstrates backend engineering best practices, including clean architecture, structured logging, input validation, and graceful shutdown.
Note: This is a backend-only application. API interaction is handled via Postman or cURL.
- Language: Golang (1.25.5)
- Database: PostgreSQL (Driver:
lib/pq) - Routing: Standard Library
net/http(ServeMux) - Validation:
go-playground/validatorfor strict payload verification. - Logging:
log/slog(Structured JSON Logging). - Containerized: Fully Dockerized for consistent deployment.
- Config: YAML-based configuration management via
ilyakaznacheev/cleanenv.
The project follows a modular layout to ensure scalability and maintainability:
├── cmd/
│ └── student-api/ # Main entry point (main.go)
├── internal/
│ ├── config/ # Configuration loader & Types
│ │ └── types/ # Data structures (Student models)
│ ├── http/
│ │ └── handlers/
│ │ └── student/ # HTTP Handlers (CRUD logic)
│ └── storage/
│ ├── postgresql/ # Database Access Layer (PostgreSQL implementation)
│ └── storage.go # Storage Interface definition
├── config/
│ └── local.yaml # Environment configuration
├── Dockerfile # Container build instructions
├── go.mod # Dependency management
└── README.md # Documentation
🛠️ Getting Started Follow these steps to set up the project locally.
-
Clone the Repository git clone https://github.com/ZeeshanSaleem-official/student-api.git cd student-api
-
Database Setup Ensure your PostgreSQL service is running. Update config/local.yaml if needed: storage_path: "host=localhost port=5432 user=postgres password=YOUR_PASSWORD dbname=students-api sslmode=disable" http_server: address: "localhost:8080"
🐳 Option A: Run with Docker (Recommended) This project includes a multi-stage Dockerfile for production-grade deployment.
- Build the Docker Image:
docker build -t student-api
- Run the Container:
docker run -p 8080:8080 student-api
The API will be accessible at http://localhost:8080.
💻 Option B: Run Locally If you prefer running without Docker:
go mod tidy
go run cmd/student-api/main.go -config=./config/local.yaml
You should see a log message confirming the server has started at localhost:8080.
🔌 API Endpoints Base URL: http://localhost:8080
| Method | Endpoint | Description | Example Payload (JSON) |
|---|---|---|---|
POST |
/api/students |
Create student | {"name": "Zeeshan", "email": "test@example.com", "age": 21} |
GET |
/api/students/{id} |
Get student | N/A |
GET |
/api/students |
List all | N/A |
PUT |
/api/students/{id} |
Update details | {"name": "Zeeshan Updated", "age": 22} |
DELETE |
/api/students/{id} |
Remove student | N/A |
🧪 Testing with Postman Since this system focuses on backend performance, use Postman to interact with the endpoints:
Open Postman.
Create a request (e.g., POST http://localhost:8080/api/students).
Go to Body → Raw → JSON.
Paste the payload:
{ "name": "Candidate One", "email": "candidate@cern.ch", "age": 22 }
Hit Send and observe the structured JSON response.
-
Standard Library Routing (
net/http): utilized Go'shttp.NewServeMuxto handle routing logic without relying on heavy external frameworks like Gin. This demonstrates a deep understanding of the language's core capabilities and minimizes runtime overhead. -
Dependency Injection: The student handler receives a
storage.Storageinterface rather than a concrete struct. This decouples the business logic from the database, adhering to the Dependency Inversion Principle. -
Graceful Shutdown: The application listens for OS signals (
SIGINT,SIGTERM) to ensure that the server shuts down cleanly, closing active connections and preventing data corruption.
📜 License
Distributed under the MIT License.