A simple RESTful API for managing books using Go and SQLite.
- CRUD operations for books
- SQLite database
- RESTful API design
- Middleware for logging and error handling
- Graceful shutdown
- Input validation
- JSON response formatting
- Go 1.16 or higher
- SQLite3
- Clone the repository:
git clone https://github.com/yourusername/bookstore-api
cd bookstore-api- Install dependencies:
go mod download- Run the application:
go run cmd/api/main.go| Method | Path | Description |
|--------|--------------- |--------------------|
| POST | /api/v1/books | Create a new book |
| GET | /api/v1/books | Get all books |
| GET | /api/v1/books/{id} | Get a book by ID |
| PUT | /api/v1/books/{id} | Update a book |
| DELETE | /api/v1/books/{id} | Delete a book |curl -X POST -H "Content-Type: application/json" \
-d '{"id":"1", "title":"The Go Programming Language", "author":"Alan A. A. Donovan", "price":49.99}' \
http://localhost:8080/api/v1/bookscurl http://localhost:8080/api/v1/bookscurl http://localhost:8080/api/v1/books/1curl -X PUT -H "Content-Type: application/json" \
-d '{"title":"The Go Programming Language", "author":"Alan A. A. Donovan", "price":39.99}' \
http://localhost:8080/api/v1/books/1curl -X DELETE http://localhost:8080/api/v1/books/1bookstore-api/
├── cmd/
│ └── api/
│ └── main.go # Application entry point
├── internal/
│ ├── config/
│ │ └── database.go # Database configuration
│ ├── handlers/
│ │ └── book_handler.go # HTTP handlers
│ ├── middleware/
│ │ ├── logging.go # Logging middleware
│ │ └── error_handler.go # Error handling middleware
│ ├── models/
│ │ └── book.go # Data models
│ └── repository/
│ └── book_repository.go # Database operations
├── pkg/
│ ├── response/
│ │ └── json.go # Response helpers
│ └── validator/
│ └── validator.go # Input validation
└── README.mdThe API returns errors in the following format:
{
"success": false,
"error": "Error message"
}Successful responses are returned in the following format:
{
"success": true,
"data": {
// Response data
}
}