Skip to content

Flack74/Students-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽ“ Students API

Go Version License Build Status Coverage Security

A lightweight, RESTful API for managing student records built with Go and SQLite.

Features β€’ Quick Start β€’ API Documentation β€’ Architecture β€’ Contributing


πŸ“‹ Table of Contents

✨ Features

  • βœ… 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

πŸ› οΈ Tech Stack

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

πŸ“ Project Structure

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

πŸ—οΈ Architecture Layers

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         HTTP Handlers               β”‚  ← Request/Response handling
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Validation & Sanitization        β”‚  ← Security layer
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         Storage Interface           β”‚  ← Abstraction layer
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      SQLite Implementation          β”‚  ← Database operations
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Go 1.24.6 or higher
  • Git

Installation

  1. Clone the repository

    git clone https://github.com/Flack74/Students-API.git
    cd Students-API
  2. Install dependencies

    go mod download
  3. Create configuration file

    # Copy example config to local config
    cp config/example.yaml config/local.yaml
    
    # Edit config/local.yaml with your settings
  4. 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
  5. Verify it's running

    curl http://localhost:8082/api/students

🐳 Docker (Coming Soon)

docker build -t students-api .
docker run -p 8082:8082 students-api

βš™οΈ Configuration

Configuration is managed via YAML files.

Setup Configuration

  1. Copy the example config:

    cp config/example.yaml config/local.yaml
  2. Edit config/local.yaml with your settings:

    env: "dev"                          # Environment: dev, staging, prod
    storage_path: "storage/storage.db"  # SQLite database path
    http_server:
      address: "localhost:8082"         # Server address

Important Notes

  • βœ… config/example.yaml - Template file (committed to git)
  • ❌ config/local.yaml - Your local config (gitignored, not committed)
  • πŸ”’ Never commit config/local.yaml with sensitive data

Environment Variables

You can override config file location using:

export Config_Path=/path/to/config.yaml

Configuration Options

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

πŸ“š API Documentation

Base URL

http://localhost:8082/api

Endpoints

Create Student

POST /api/students
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com",
  "age": 20
}

Response (201 Created)

{
  "id": 1
}

Get Student by ID

GET /api/students/{id}

Response (200 OK)

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "age": 20
}

Get All Students

GET /api/students

Response (200 OK)

[
  {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com",
    "age": 20
  },
  {
    "id": 2,
    "name": "Jane Smith",
    "email": "jane@example.com",
    "age": 22
  }
]

Update Student

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 Student

DELETE /api/students/{id}

Response (200 OK)

{
  "message": "student deleted successfully"
}

Error Responses

Validation Error (400 Bad Request)

{
  "status": "Error",
  "error": "field Name is required field, field Email is required field"
}

Not Found (404 Not Found)

{
  "status": "Error",
  "error": "student with id 123 not found"
}

Invalid Input (400 Bad Request)

{
  "status": "Error",
  "error": "invalid student id"
}

Database Error (500 Internal Server Error)

{
  "status": "Error",
  "error": "database operation failed"
}

Request Validation Rules

Field Type Required Validation
name string βœ… Required, 2-50 chars, XSS sanitized
email string βœ… Required, valid email format, XSS sanitized
age integer βœ… Required, 1-120

πŸ›οΈ Architecture

Design Principles

  1. Clean Architecture - Separation of concerns with clear boundaries
  2. Dependency Injection - Storage interface injected into handlers
  3. Interface-Based Design - Easy to swap implementations
  4. Single Responsibility - Each package has one clear purpose

Data Flow

HTTP Request
    ↓
Handler (JSON Decode)
    ↓
Validation (go-playground/validator)
    ↓
Sanitization (bluemonday)
    ↓
Storage Interface
    ↓
SQLite Implementation (Prepared Statements)
    ↓
Database
    ↓
Response (JSON Encode)

Key Components

1. Handlers (internal/http/handlers/student/)

  • HTTP request/response handling
  • Input validation
  • Error handling
  • Response formatting

2. Storage Interface (internal/storage/)

  • Defines contract for data operations
  • Allows multiple implementations (SQLite, PostgreSQL, etc.)

3. SQLite Implementation (internal/storage/sqlite/)

  • Concrete implementation of Storage interface
  • Database connection management
  • SQL query execution

4. Types (internal/types/)

  • Domain models
  • Data structures

5. Utils (internal/utils/)

  • Response helpers (JSON encoding, error formatting)
  • Input sanitization (XSS protection with bluemonday)
  • Common utilities

6. App (internal/app/)

  • Application initialization
  • Dependency injection
  • Server lifecycle management

πŸ’» Development

Building

# Build binary
go build -o bin/students-api cmd/students-api/main.go

# Run binary
./bin/students-api -config=config/local.yaml

Testing

# Run tests (coming soon)
go test ./...

# Run tests with coverage
go test -cover ./...

# Run tests with race detector
go test -race ./...

Linting

# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Run linter
golangci-lint run

Database Management

The SQLite database is automatically created on first run. To reset:

rm storage/storage.db
# Restart the application

Example Requests

# 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

πŸ—ΊοΈ Roadmap

Phase 1 - Security & Stability βœ…

  • 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

Phase 2 - Features

  • Pagination and filtering
  • Search functionality
  • PATCH endpoint for partial updates
  • Bulk operations

Phase 3 - Production Ready

  • Authentication & authorization
  • Rate limiting
  • Caching layer
  • Metrics and monitoring
  • Docker support
  • CI/CD pipeline

Phase 4 - Advanced

  • GraphQL API
  • WebSocket support
  • Multi-tenancy
  • Advanced analytics

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow Go best practices and idioms
  • Write tests for new features
  • Update documentation
  • Run linters before committing
  • Keep commits atomic and descriptive

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

Flack74

πŸ™ Acknowledgments

πŸ“ž Support

If you have any questions or need help, please:

  • Open an issue on GitHub
  • Check existing issues for solutions

Made with ❀️ by Flack

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages