A lightweight, RESTful API for managing student records built with Go and SQLite.
Features β’ Quick Start β’ API Documentation β’ Architecture β’ Contributing
- Features
- Tech Stack
- Project Structure
- Quick Start
- Configuration
- API Documentation
- Architecture
- Development
- Roadmap
- Contributing
- License
- β CRUD Operations - Create, Read, Update, Delete student records
- β RESTful Design - Clean and intuitive API endpoints
- β Input Validation - Request validation using go-playground/validator
- β Input Sanitization - XSS protection using bluemonday
- β SQL Injection Protection - Parameterized queries with prepared statements
- β Graceful Shutdown - Proper signal handling and cleanup
- β Structured Logging - JSON-based logging with slog
- β Configuration Management - YAML-based config with environment overrides
- β SQLite Database - Lightweight, embedded database
- β Clean Architecture - Separation of concerns with layered design
| Category | Technology |
|---|---|
| Language | Go 1.24.6 |
| Database | SQLite 3 |
| Router | net/http (stdlib) |
| Validation | go-playground/validator/v10 |
| Sanitization | bluemonday |
| Config | cleanenv |
| Logging | log/slog |
Students-API/
βββ cmd/
β βββ students-api/
β βββ main.go # Application entry point
βββ config/
β βββ local.yaml # Configuration file
βββ internal/
β βββ app/
β β βββ dependencies.go # Dependency injection
β β βββ server.go # Server lifecycle
β βββ config/
β β βββ config.go # Config loader
β βββ errors/
β β βββ errors.go # Custom error types
β βββ http/
β β βββ handlers/
β β βββ router/
β β β βββ router.go # Route definitions
β β βββ student/
β β βββ student.go # HTTP handlers
β βββ storage/
β β βββ storage.go # Storage interface
β β βββ sqlite/
β β βββ sqlite.go # SQLite implementation
β βββ types/
β β βββ types.go # Domain models
β βββ utils/
β βββ response/
β β βββ response.go # Response helpers
β βββ sanitize/
β βββ sanitize.go # Input sanitization
βββ storage/
β βββ storage.db # SQLite database file
βββ go.mod
βββ go.sum
βββ README.md
βββββββββββββββββββββββββββββββββββββββ
β HTTP Handlers β β Request/Response handling
βββββββββββββββββββββββββββββββββββββββ€
β Validation & Sanitization β β Security layer
βββββββββββββββββββββββββββββββββββββββ€
β Storage Interface β β Abstraction layer
βββββββββββββββββββββββββββββββββββββββ€
β SQLite Implementation β β Database operations
βββββββββββββββββββββββββββββββββββββββ
- Go 1.24.6 or higher
- Git
-
Clone the repository
git clone https://github.com/Flack74/Students-API.git cd Students-API -
Install dependencies
go mod download
-
Create configuration file
# Copy example config to local config cp config/example.yaml config/local.yaml # Edit config/local.yaml with your settings
-
Run the application
# Using config file path go run cmd/students-api/main.go -config=config/local.yaml # Or using environment variable export Config_Path=config/local.yaml go run cmd/students-api/main.go
-
Verify it's running
curl http://localhost:8082/api/students
docker build -t students-api .
docker run -p 8082:8082 students-apiConfiguration is managed via YAML files.
-
Copy the example config:
cp config/example.yaml config/local.yaml
-
Edit
config/local.yamlwith your settings:env: "dev" # Environment: dev, staging, prod storage_path: "storage/storage.db" # SQLite database path http_server: address: "localhost:8082" # Server address
- β
config/example.yaml- Template file (committed to git) - β
config/local.yaml- Your local config (gitignored, not committed) - π Never commit
config/local.yamlwith sensitive data
You can override config file location using:
export Config_Path=/path/to/config.yaml| Option | Description | Example |
|---|---|---|
env |
Environment name | dev, staging, prod |
storage_path |
SQLite database file path | storage/storage.db |
http_server.address |
Server bind address | localhost:8082 |
http://localhost:8082/api
POST /api/students
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"age": 20
}Response (201 Created)
{
"id": 1
}GET /api/students/{id}Response (200 OK)
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"age": 20
}GET /api/studentsResponse (200 OK)
[
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"age": 20
},
{
"id": 2,
"name": "Jane Smith",
"email": "jane@example.com",
"age": 22
}
]PUT /api/students/{id}
Content-Type: application/json
{
"name": "John Updated",
"email": "john.updated@example.com",
"age": 21
}Response (201 Created)
{
"message": "student upated successfully"
}DELETE /api/students/{id}Response (200 OK)
{
"message": "student deleted successfully"
}{
"status": "Error",
"error": "field Name is required field, field Email is required field"
}{
"status": "Error",
"error": "student with id 123 not found"
}{
"status": "Error",
"error": "invalid student id"
}{
"status": "Error",
"error": "database operation failed"
}| Field | Type | Required | Validation |
|---|---|---|---|
| name | string | β | Required, 2-50 chars, XSS sanitized |
| string | β | Required, valid email format, XSS sanitized | |
| age | integer | β | Required, 1-120 |
- Clean Architecture - Separation of concerns with clear boundaries
- Dependency Injection - Storage interface injected into handlers
- Interface-Based Design - Easy to swap implementations
- Single Responsibility - Each package has one clear purpose
HTTP Request
β
Handler (JSON Decode)
β
Validation (go-playground/validator)
β
Sanitization (bluemonday)
β
Storage Interface
β
SQLite Implementation (Prepared Statements)
β
Database
β
Response (JSON Encode)
- HTTP request/response handling
- Input validation
- Error handling
- Response formatting
- Defines contract for data operations
- Allows multiple implementations (SQLite, PostgreSQL, etc.)
- Concrete implementation of Storage interface
- Database connection management
- SQL query execution
- Domain models
- Data structures
- Response helpers (JSON encoding, error formatting)
- Input sanitization (XSS protection with bluemonday)
- Common utilities
- Application initialization
- Dependency injection
- Server lifecycle management
# Build binary
go build -o bin/students-api cmd/students-api/main.go
# Run binary
./bin/students-api -config=config/local.yaml# Run tests (coming soon)
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with race detector
go test -race ./...# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Run linter
golangci-lint runThe SQLite database is automatically created on first run. To reset:
rm storage/storage.db
# Restart the application# Create a student
curl -X POST http://localhost:8082/api/students \
-H "Content-Type: application/json" \
-d '{"name":"Alice Johnson","email":"alice@example.com","age":19}'
# Get all students
curl http://localhost:8082/api/students
# Get student by ID
curl http://localhost:8082/api/students/1
# Update student
curl -X PUT http://localhost:8082/api/students/1 \
-H "Content-Type: application/json" \
-d '{"name":"Alice Updated","email":"alice.new@example.com","age":20}'
# Delete student
curl -X DELETE http://localhost:8082/api/students/1- Fix XSS vulnerabilities (bluemonday implemented)
- Input sanitization (name, email, ID parameters)
- SQL injection protection (prepared statements)
- Improve error handling (custom error types, centralized handling)
- Add database connection pooling
- Pagination and filtering
- Search functionality
- PATCH endpoint for partial updates
- Bulk operations
- Authentication & authorization
- Rate limiting
- Caching layer
- Metrics and monitoring
- Docker support
- CI/CD pipeline
- GraphQL API
- WebSocket support
- Multi-tenancy
- Advanced analytics
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow Go best practices and idioms
- Write tests for new features
- Update documentation
- Run linters before committing
- Keep commits atomic and descriptive
This project is licensed under the MIT License - see the LICENSE file for details.
Flack74
- GitHub: @Flack74
- cleanenv - Configuration management
- validator - Input validation
- bluemonday - HTML sanitization and XSS protection
- go-sqlite3 - SQLite driver
If you have any questions or need help, please:
- Open an issue on GitHub
- Check existing issues for solutions
Made with β€οΈ by Flack