A simple REST API for managing student records, built with Go and SQLite.
StudentApi is a lightweight RESTful API that allows you to perform CRUD (Create, Read, Update, Delete) operations on student data. It uses SQLite as the database backend and is designed with a clean architecture using Go's standard library and minimal external dependencies.
- Create Student: Add new student records
- Read Students: Retrieve individual students by ID or get a list of all students
- Update Student: Modify existing student information
- Delete Student: Remove student records
- Input Validation: Validates required fields (name, email, age)
- Graceful Shutdown: Handles server shutdown gracefully
- Structured Logging: Uses slog for logging
.
├── cmd/
│ └── studentApi/
│ └── main.go # Application entry point
├── config/
│ └── local.yaml # Configuration file
├── internal/
│ ├── config/
│ │ └── config.go # Configuration loading
│ ├── http/
│ │ └── handlers/
│ │ └── student/
│ │ └── student.go # HTTP handlers for student operations
│ ├── storage/
│ │ ├── storage.go # Storage interface
│ │ └── sqlite/
│ │ └── sqlite.go # SQLite implementation
│ ├── types/
│ │ └── types.go # Data types
│ └── utils/
│ └── response/
│ └── response.go # Response utilities
├── storage/
│ └── storage.db # SQLite database file (auto-created)
├── go.mod # Go module file
├── go.sum # Go dependencies
└── README.md # This file
- Go 1.25.6 or later
- SQLite (automatically handled by the application)
-
Clone the repository:
git clone https://github.com/iShinzoo/studentApi.git cd studentApi -
Install dependencies:
go mod tidy
-
(Optional) Build the application:
go build -o bin/studentApi cmd/studentApi/main.go
The application uses a YAML configuration file. A sample configuration is provided in config/local.yaml:
env: "dev"
storage_path: "storage/storage.db"
http_server:
address: "localhost:8082"You can create different configuration files for different environments (e.g., config/prod.yaml).
Run the application with the configuration file:
go run cmd/studentApi/main.go -config config/local.yamlOr if you built the binary:
./bin/studentApi -config config/local.yamlThe server will start on the address specified in the configuration (default: localhost:8082).
- POST
/api/students - Body: JSON
{ "name": "John Doe", "email": "john.doe@example.com", "age": 20 } - Response:
{"id": 1}
- GET
/api/students/{id} - Response: Student object or error
- GET
/api/students - Response: Array of student objects
- PUT
/api/students/{id} - Body: JSON (same as create)
- Response:
{"message": "student updated successfully"}
- DELETE
/api/students/{id} - Response:
{"message": "student deleted successfully"}
type Student struct {
Id int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}All fields except id are required for create/update operations.
github.com/go-playground/validator/v10: Input validationgithub.com/ilyakaznacheev/cleanenv: Configuration loadingmodernc.org/sqlite: SQLite driver- Standard library packages for HTTP, JSON, logging, etc.
The project follows Go best practices with:
- Clean architecture separation
- Interface-based design for storage layer
- Structured error handling
- Input validation
- Graceful server shutdown
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request