A RESTful API for managing orders built with Go. This project demonstrates a production-ready microservice with two implementations: in-memory storage and PostgreSQL persistence.
- Create, read, update, and delete orders
- RESTful API design
- JSON request/response format
- Input validation
- Error handling
- Two implementations (in-memory and PostgreSQL)
- Go (standard library net/http)
- PostgreSQL
- github.com/lib/pq (PostgreSQL driver)
| Method | Endpoint | Description |
|---|---|---|
| GET | /orders |
Get all orders |
| POST | /orders |
Create a new order |
| GET | /orders/{id} |
Get a specific order |
| PUT | /orders/{id} |
Update an order |
| DELETE | /orders/{id} |
Delete an order |
curl -X POST http://localhost:8080/orders \
-H "Content-Type: application/json" \
-d '{"item":"laptop","quantity":1}'curl http://localhost:8080/orderscurl -X PUT http://localhost:8080/orders/1 \
-H "Content-Type: application/json" \
-d '{"item":"gaming laptop","quantity":2}'curl -X DELETE http://localhost:8080/orders/1This repository contains two branches showing the evolution of the project:
- main: Simple implementation with in-memory storage using a Go slice. Perfect for understanding the basic API structure.
- add-postgresql: Production-ready version with PostgreSQL database integration. Data persists across server restarts.
- Go 1.16 or higher
- PostgreSQL (for the database branch)
git checkout main
go run main.goThe server will start at http://localhost:8080
git checkout add-postgresql- Create a PostgreSQL database:
CREATE DATABASE orderdb;- Create the orders table:
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
item VARCHAR(255) NOT NULL,
quantity INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);- Run the application:
go run main.go.
├── main.go # Main application code
├── go.mod # Go module file
├── go.sum # Go module checksum
└── README.md # This file
The API returns appropriate HTTP status codes:
200 OK: Successful request
201 Created: Resource created successfully
204 No Content: Resource deleted successfully
400 Bad Request: Invalid input
404 Not Found: Resource not found
405 Method Not Allowed: HTTP method not supported
500 Internal Server Error: Server-side error
Author
NabeelOG