richardcase/ergo-test

★ 0Forks 0GoGitHub ↗Compare

README

ergo-test

A customer management API built with Go and the Ergo framework. This project demonstrates actor-based concurrency using Ergo to manage customer data through a RESTful API.

Features

  • Actor-based Architecture: Uses the Ergo framework for actor-based concurrency
  • RESTful API: Full CRUD operations for customer management
  • Persistent Storage: Uses bbolt for persistent customer data storage
  • Concurrent Request Handling: Actor pattern ensures safe concurrent access to customer data

Prerequisites

  • Go 1.21 or higher
  • Basic understanding of REST APIs

Installation

Clone the repository and build the application:

git clone https://github.com/richardcase/ergo-test.git
cd ergo-test
go mod download
go build -o customer-api

Running the Application

Start the server:

./customer-api

The server will start on port 8080. You should see output similar to:

Ergo node started: 'customer-api@localhost'
Customer actor started with bbolt database: customers.db
HTTP server listening on :8080
API Endpoints:
  GET    /api/customers      - List all customers
  GET    /api/customers/{id} - Get a customer by ID
  POST   /api/customers      - Create a new customer
  PUT    /api/customers/{id} - Update a customer
  DELETE /api/customers/{id} - Delete a customer
  GET    /health             - Health check

Customer data is persisted in the customers.db file.

API Endpoints

Health Check

GET /health

List All Customers

GET /api/customers

Get a Specific Customer

GET /api/customers/{id}

Create a New Customer

POST /api/customers
Content-Type: application/json

{
  "id": "1",
  "name": "John Doe",
  "email": "[email protected]",
  "phone": "555-1234"
}

Update a Customer

PUT /api/customers/{id}
Content-Type: application/json

{
  "name": "John Updated",
  "email": "[email protected]",
  "phone": "555-9999"
}

Delete a Customer

DELETE /api/customers/{id}

Example Usage

Using cURL

# Create a customer
curl -X POST http://localhost:8080/api/customers \
  -H 'Content-Type: application/json' \
  -d '{"id":"1","name":"John Doe","email":"[email protected]","phone":"555-1234"}'

# List all customers
curl http://localhost:8080/api/customers

# Get a specific customer
curl http://localhost:8080/api/customers/1

# Update a customer
curl -X PUT http://localhost:8080/api/customers/1 \
  -H 'Content-Type: application/json' \
  -d '{"name":"John Updated","email":"[email protected]","phone":"555-9999"}'

# Delete a customer
curl -X DELETE http://localhost:8080/api/customers/1

Using the Demo Script

A demo script is provided to showcase all API operations:

./demo.sh

This script demonstrates:

  1. Health check
  2. Listing empty customers
  3. Creating customers
  4. Listing all customers
  5. Getting a specific customer
  6. Updating a customer
  7. Deleting a customer
  8. Error handling for non-existent customers

Architecture

The application uses an actor-based architecture powered by the Ergo framework:

  • Ergo Node: Provides the actor system runtime
  • Customer Actor: A goroutine-based actor that handles all customer operations through channels
  • API Handler: HTTP handlers that communicate with the Customer Actor
  • bbolt Store: Persistent customer data storage using bbolt (a key-value database)

Flow Diagram

HTTP Request → API Handler → Customer Actor → bbolt Store
                    ↓
HTTP Response ← API Handler ← Customer Actor ← bbolt Store

The Customer Actor pattern ensures:

  • Thread-safe access to customer data
  • Sequential processing of requests
  • Isolation of business logic from HTTP concerns

Project Structure

.
├── main.go          # Application entry point and Ergo node setup
├── customer.go      # Customer actor and data models
├── api.go           # HTTP API handlers
├── demo.sh          # Demo script showcasing all API operations
├── go.mod           # Go module dependencies
├── go.sum           # Go module checksums
├── .gitignore       # Git ignore file
└── README.md        # This file

Dependencies

License

This project is for demonstration purposes.

Contributors

Copilotrichardcase

Issues