A RESTful API for managing student records, built with Go using SQLite for persistence.
- ✅ Create new students
- ✅ Retrieve all students or a specific student by ID
- ✅ Update student information (partial updates)
- ✅ Delete students
- ✅ Input validation with detailed error messages
- ✅ Structured JSON logging
- ✅ Graceful server shutdown
- ✅ Configuration management via YAML and environment variables
- Go 1.25.3 or higher
- SQLite3
student-api/
├── cmd/
│ └── student-api/
│ └── main.go # Application entry point
├── config/
│ └── local.yml # Configuration file
├── internal/
│ ├── config/
│ │ └── config.go # Configuration loading
│ ├── http/
│ │ └── handlers/
│ │ └── student/
│ │ └── student.go # HTTP handlers
│ ├── storage/
│ │ ├── storage.go # Storage interface
│ │ └── sqlite/
│ │ └── sqlite.go # SQLite implementation
│ ├── types/
│ │ └── types.go # Data structures
│ └── utils/
│ └── response/
│ └── response.go # Response utilities
├── storage/ # SQLite database file (created at runtime)
├── go.mod # Go module dependencies
└── .gitignore # Git ignore rules
- Clone the repository:
git clone https://github.com/gourav224/student-api.git
cd student-api- Install dependencies:
go mod downloadConfiguration can be loaded from:
- Environment variable:
CONFIG_PATH - Command-line flag:
--config
Example configuration in config/local.yml:
env: "dev"
storage_path: "storage/sqlite.db"
http_server:
address: "localhost:8000"CONFIG_PATH: Path to the configuration fileHTTP_SERVER_ADDR: HTTP server address (default::8080)STORAGE_PATH: SQLite database file path (required)ENV: Environment name (required)
CONFIG_PATH=config/local.yml go run cmd/student-api/main.goOr with the config flag:
go run cmd/student-api/main.go --config=config/local.ymlThe server will start on localhost:8000 (as per config/local.yml).
POST /api/students
Request body:
{
"name": "John Doe",
"email": "[email protected]",
"age": 20
}Response (201 Created):
{
"status": "success",
"message": "student created successfully",
"data": 1
}GET /api/students
Response (200 OK):
{
"status": "success",
"message": "students fetched successfully",
"data": [
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"age": 20
}
]
}GET /api/students/{id}
Response (200 OK):
{
"status": "success",
"message": "student fetched successfully",
"data": {
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"age": 20
}
}PATCH /api/students/{id}
Request body (all fields optional):
{
"name": "Jane Doe",
"age": 21
}Response (200 OK):
{
"status": "success",
"message": "student updated successfully",
"data": {
"id": 1,
"name": "Jane Doe",
"email": "[email protected]",
"age": 21
}
}DELETE /api/students/{id}
Response (200 OK):
{
"status": "success",
"message": "student deleted successfully",
"data": 1
}The following validation rules are enforced:
- name: Required, must be a non-empty string
- email: Required, must be a valid email address
- age: Required, must be an integer between 1 and 120
github.com/go-playground/validator/v10- Input validationgithub.com/ilyakaznacheev/cleanenv- Configuration managementgithub.com/mattn/go-sqlite3- SQLite3 driver
The API returns appropriate HTTP status codes:
200 OK- Successful GET/PATCH/DELETE201 Created- Successful POST400 Bad Request- Invalid input or malformed request500 Internal Server Error- Database or server errors
All error responses follow this format:
{
"status": "error",
"error": "error message"
}go build -o student-api cmd/student-api/main.gogo test ./...This project is open source and available under the MIT License.