Skip to content

javydevx/quote-book

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Quote Book REST API (Go)

A simple REST API to manage quotes using Go standard library only.


πŸ“¦ How to Run

# Install dependencies
go mod tidy

# Start the server
go run main.go

Server runs at: http://localhost:8080


πŸ“‘ Swagger UI (OpenAPI Docs)

This project supports auto-generated Swagger documentation using swaggo/swag.

πŸ› οΈ Setup (One-Time)

Install swag CLI tool if not already:

go install github.com/swaggo/swag/cmd/swag@latest

Generate docs:

swag init --generalInfo cmd/main.go --output docs

🧼 Clean & Re-generate Docs

If you need to regenerate Swagger docs from scratch:

rm -rf docs
swag init --generalInfo cmd/main.go --output docs

🌐 View in Browser

After running the server, open:

http://localhost:8080/swagger/index.html

Interact with the API and explore endpoints.

πŸš€ CI/CD: GitHub Actions

This project includes a GitHub Actions workflow that runs on every push and pull request to the main branch.

πŸ”„ What it does:

  • Runs tests to ensure the code works correctly.
  • Generates the Swagger documentation using swag init.
  • Uploads the generated Swagger docs as an artifact named swagger-docs.

You can find the workflow config here:

.github/workflows/ci.yml

After the workflow completes, the generated Swagger documentation is available in the Actions tab as a downloadable artifact.


πŸ”Œ API Endpoints

βž• Add a New Quote

curl -X POST http://localhost:8080/quotes \
  -H "Content-Type: application/json" \
  -d '{"author":"Confucius", "quote":"Life is simple, but we insist on making it complicated."}'
  • Method: POST
  • URL: /quotes
  • Body:
    {
      "author": "Confucius",
      "quote": "Life is simple, but we insist on making it complicated."
    }
  • Response: 201 Created + JSON of saved quote

πŸ“ƒ Get All Quotes

curl http://localhost:8080/quotes
  • Method: GET
  • URL: /quotes
  • Response: List of all quotes

🎲 Get a Random Quote

curl http://localhost:8080/quotes/random
  • Method: GET
  • URL: /quotes/random
  • Response: A single random quote

πŸ” Filter Quotes by Author

curl http://localhost:8080/quotes?author=Confucius
  • Method: GET
  • URL: /quotes?author=Confucius
  • Response: List of quotes by the given author

❌ Delete Quote by ID

curl -X DELETE http://localhost:8080/quotes/1
  • Method: DELETE
  • URL: /quotes/{id}
  • Response: 204 No Content if deleted, 404 Not Found if not found

πŸ§ͺ Running Tests

Unit tests are placed next to their corresponding source files.

▢️ Run All Tests Verbosely

go test ./... -v

βœ… Run with Coverage Report

go test ./... -cover

πŸ“Š Generate HTML Coverage Report

go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out

This opens an HTML page in your browser with detailed line-by-line coverage.


πŸ§ͺ VS Code REST Client – requests.http

This project includes a requests.http file for use with the REST Client extension.

πŸ“₯ Install Extension

  • Open VS Code
  • Go to Extensions (Ctrl+Shift+X)
  • Search for: REST Client
  • Install by humao

πŸ“„ requests.http

### Add a new quote
POST http://localhost:8080/quotes
Content-Type: application/json

{
  "author": "Confucius",
  "quote": "Life is simple, but we insist on making it complicated."
}

### Get all quotes
GET http://localhost:8080/quotes

### Get quotes by author
GET http://localhost:8080/quotes?author=Confucius

### Get a random quote
GET http://localhost:8080/quotes/random

### Delete a quote by ID
DELETE http://localhost:8080/quotes/1

Open this file in VS Code and click "Send Request" above each block to test.


πŸ“‚ Project Structure

quote-book/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml   
β”œβ”€β”€ cmd/
β”‚   └── main.go
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ docs.go
β”‚   β”œβ”€β”€ swagger.json
β”‚   └── swagger.yaml
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ handler/
β”‚   β”‚   β”œβ”€β”€ handler_test.go
β”‚   β”‚   └── handler.go
β”‚   β”œβ”€β”€ model/
β”‚   β”‚   └── quote.go
β”‚   └── storage/
β”‚       β”œβ”€β”€ memory_test.go
β”‚       └── memory.go
β”œβ”€β”€ coverage.out
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ README.md
└── requests.http

🧰 Tech Stack

  • 🧠 Go (Standard Library)
  • πŸ“š Swagger + Swaggo for API Docs
  • πŸ§ͺ REST Client (VS Code extension)
  • 🧡 In-memory store using sync.Mutex

✨ Future Ideas

  • πŸ’Ύ Persistent database support (e.g. PostgreSQL, SQLite)
  • 🧩 Categorization/tagging of quotes
  • 🧹 Sorting & pagination
  • 🐳 Docker support

πŸ‘¨β€πŸ’» Author

Made with ❀️ by javy99

About

A simple REST API to manage quotes using Go standard library.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages