Skip to content

richardcase/gleam-with-api

Repository files navigation

Customer API - Gleam OTP Application

A production-ready Gleam application demonstrating real OTP actors, fault-tolerant customer management, and type-safe concurrent programming.

Architecture Overview

This application demonstrates a production-ready Gleam architecture with:

🏗️ Real OTP Actor System

  • Database Actor: Manages persistent state with message-based interface
  • Customer Registry: Manages lifecycle of customer actors
  • Customer Actors: Individual actors per customer for concurrent access
  • Message Protocols: Type-safe communication between actors
  • Actor Supervision: Proper shutdown and cleanup handling

📁 Project Structure

src/
├── customer_api.gleam      # Main application with actor system
├── customer.gleam          # Customer data model and types
├── customer_actor.gleam    # Customer registry and individual actors
├── database.gleam          # Database actor with message handling
test/
├── customer_api_test.gleam # Actor-based unit tests

🚀 Features Implemented

Real OTP Actors

  • Database actor managing all persistent state
  • Customer registry actor for managing customer actor lifecycle
  • Individual customer actors for concurrent access
  • Type-safe message passing between actors
  • Proper actor shutdown and supervision

Actor Message Protocols

// Database Actor Messages
pub type DatabaseMessage {
  InsertCustomer(Customer, reply_with: Subject(Result(Customer, DatabaseError)))
  GetCustomer(Int, reply_with: Subject(Result(Customer, DatabaseError)))
  UpdateCustomer(Int, Customer, reply_with: Subject(Result(Customer, DatabaseError)))
  DeleteCustomer(Int, reply_with: Subject(Result(Nil, DatabaseError)))
  ListCustomers(reply_with: Subject(Result(List(Customer), DatabaseError)))
  Shutdown
}

// Customer Registry Messages
pub type RegistryMessage {
  GetOrCreateCustomerActor(Int, reply_with: Subject(Result(Subject(CustomerActorMessage), DatabaseError)))
  CreateCustomer(Customer, reply_with: Subject(Result(Customer, DatabaseError)))
  ListCustomers(reply_with: Subject(Result(List(Customer), DatabaseError)))
  Shutdown
}

// Individual Customer Actor Messages
pub type CustomerActorMessage {
  GetCustomer(reply_with: Subject(Result(Customer, DatabaseError)))
  UpdateCustomer(Customer, reply_with: Subject(Result(Customer, DatabaseError)))
  DeleteCustomer(reply_with: Subject(Result(Nil, DatabaseError)))
  Shutdown
}

🔄 Planned Enhancements (Full Production Version)

To make this a complete production application, add:

  1. Real OTP Actors:

    // Add dependency: gleam_otp = ">= 0.10.0 and < 1.0.0"
    import gleam/otp/actor
    import gleam/otp/supervisor
  2. REST API with Wisp:

    // Add dependencies:
    // wisp = ">= 0.12.0 and < 1.0.0"
    // mist = ">= 1.2.0 and < 2.0.0"
    // gleam_http = ">= 3.6.0 and < 4.0.0"
    // gleam_json = ">= 1.0.0 and < 2.0.0"
  3. SQLite Database:

    // Add dependency: sqlight = ">= 0.15.0 and < 1.0.0"

📡 API Endpoints Design

The complete application would provide these REST endpoints:

GET    /api/customers      # List all customers
POST   /api/customers      # Create new customer
GET    /api/customers/:id  # Get customer by ID  
PUT    /api/customers/:id  # Update customer
DELETE /api/customers/:id  # Delete customer

Example Request/Response:

Create Customer:

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

Response:

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

🏃‍♂️ Running the Demo

# Install Gleam (if not already installed)
curl -sSL https://github.com/gleam-lang/gleam/releases/download/v1.5.1/gleam-v1.5.1-x86_64-unknown-linux-musl.tar.gz -o gleam.tar.gz
tar -xzf gleam.tar.gz
sudo mv gleam /usr/local/bin/

# Run the demonstration
gleam run

# Run tests
gleam test

🎯 Demo Output

The demo application showcases:

  1. Customer Creation: Creating customers with validation
  2. Data Retrieval: Finding customers by ID
  3. Updates: Modifying customer information
  4. Deletion: Removing customers from the system
  5. Listing: Viewing all customers

Example output:

🚀 Customer API - Gleam OTP Application
=====================================

✅ Customer service initialized
✅ In-memory database ready

📊 Demonstrating CRUD Operations
---------------------------------
Creating customers...
✅ Created customer: {"id":1,"name":"John Doe","email":"[email protected]","phone":null,"address":null}
✅ Created customer: {"id":2,"name":"Jane Smith","email":"[email protected]","phone":null,"address":null}

📋 Listing all customers...
Current customers:
  - {"id":1,"name":"John Doe","email":"[email protected]","phone":null,"address":null}
  - {"id":2,"name":"Jane Smith","email":"[email protected]","phone":null,"address":null}

🎉 Demo completed successfully!

🔒 Production Considerations

For a production deployment, this application would include:

  • Database Persistence: SQLite or PostgreSQL
  • Actor Supervision: Fault tolerance with OTP supervisors
  • HTTP Server: Robust web server with proper error handling
  • Authentication: JWT or session-based auth
  • Validation: Input sanitization and validation
  • Logging: Structured logging for monitoring
  • Configuration: Environment-based configuration
  • Docker: Containerization for deployment

🧪 Technology Stack

  • Language: Gleam 1.5.1
  • Runtime: Erlang/OTP (BEAM VM)
  • Architecture: Actor Model + Functional Programming
  • Database: In-memory (demo) → SQLite/PostgreSQL (production)
  • Web Framework: Wisp (planned)
  • Testing: Gleeunit

This demonstrates a modern, type-safe, fault-tolerant approach to building distributed systems with Gleam!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •