A simple REST API to manage quotes using Go standard library only.
# Install dependencies
go mod tidy
# Start the server
go run main.goServer runs at: http://localhost:8080
This project supports auto-generated Swagger documentation using swaggo/swag.
Install swag CLI tool if not already:
go install github.com/swaggo/swag/cmd/swag@latestGenerate docs:
swag init --generalInfo cmd/main.go --output docsIf you need to regenerate Swagger docs from scratch:
rm -rf docs
swag init --generalInfo cmd/main.go --output docsAfter running the server, open:
http://localhost:8080/swagger/index.html
Interact with the API and explore endpoints.
This project includes a GitHub Actions workflow that runs on every push and pull request to the main branch.
- 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.
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
curl http://localhost:8080/quotes- Method:
GET - URL:
/quotes - Response: List of all quotes
curl http://localhost:8080/quotes/random- Method:
GET - URL:
/quotes/random - Response: A single random quote
curl http://localhost:8080/quotes?author=Confucius- Method:
GET - URL:
/quotes?author=Confucius - Response: List of quotes by the given author
curl -X DELETE http://localhost:8080/quotes/1- Method:
DELETE - URL:
/quotes/{id} - Response:
204 No Contentif deleted,404 Not Foundif not found
Unit tests are placed next to their corresponding source files.
go test ./... -vgo test ./... -covergo test ./... -coverprofile=coverage.out
go tool cover -html=coverage.outThis opens an HTML page in your browser with detailed line-by-line coverage.
This project includes a requests.http file for use with the REST Client extension.
- Open VS Code
- Go to Extensions (
Ctrl+Shift+X) - Search for:
REST Client - Install by
humao
### 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/1Open this file in VS Code and click "Send Request" above each block to test.
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
- π§ Go (Standard Library)
- π Swagger + Swaggo for API Docs
- π§ͺ REST Client (VS Code extension)
- π§΅ In-memory store using sync.Mutex
- πΎ Persistent database support (e.g. PostgreSQL, SQLite)
- π§© Categorization/tagging of quotes
- π§Ή Sorting & pagination
- π³ Docker support
Made with β€οΈ by javy99