67 lines
1.4 KiB
Markdown
67 lines
1.4 KiB
Markdown
# Go Book API Example
|
|
|
|
This is a simple Go web service that provides a basic API for retrieving book information. It's built using the `chi` router and serves hardcoded book data from memory.
|
|
|
|
## Project Structure
|
|
|
|
- `main.go`: Application entry point, sets up the HTTP server and defines the `/books/{id}` endpoint.
|
|
- `internal/handlers/book_handler.go`: Contains the `BookHandler` responsible for processing requests to the `/books/{id}` endpoint. It uses an in-memory list of books.
|
|
- `internal/models/book.go`: Defines the `Book` struct used to represent book data.
|
|
|
|
## Getting Started
|
|
|
|
### Prerequisites
|
|
|
|
- Go (version 1.16 or higher recommended)
|
|
|
|
### Running the Application
|
|
|
|
1. **Clone the repository:**
|
|
```bash
|
|
git clone <repository_url>
|
|
cd testing-go-example
|
|
```
|
|
(Note: Replace `<repository_url>` with the actual repository URL if this project were hosted.)
|
|
|
|
2. **Run the server:**
|
|
```bash
|
|
go run main.go
|
|
```
|
|
The server will start on `http://localhost:3000`.
|
|
|
|
### API Endpoints
|
|
|
|
#### `GET /books/{id}`
|
|
|
|
Retrieves details for a single book by its ID.
|
|
|
|
**Example Request:**
|
|
|
|
```bash
|
|
curl http://localhost:3000/books/1
|
|
```
|
|
|
|
**Example Response:**
|
|
|
|
```json
|
|
{
|
|
"id": "1",
|
|
"title": "The Hitchhiker's Guide to the Galaxy",
|
|
"author": "Douglas Adams"
|
|
}
|
|
```
|
|
|
|
```bash
|
|
curl http://localhost:3000/books/2
|
|
```
|
|
|
|
**Example Response:**
|
|
|
|
```json
|
|
{
|
|
"id": "2",
|
|
"title": "The Restaurant at the End of the Universe",
|
|
"author": "Douglas Adams"
|
|
}
|
|
```
|