Skip to content

emaginebr/zTools

Repository files navigation

zTools - Developer Utility API Toolkit

.NET License NuGet

Overview

zTools is a RESTful API built with .NET 8 that provides a collection of utility services for common development tasks: ChatGPT/DALL-E integration, email delivery, S3-compatible file storage, Brazilian document validation (CPF/CNPJ), and string manipulation utilities.

The project follows clean architecture with separated layers and is available as a NuGet package for consuming the API from other .NET projects with strongly-typed clients and DTOs.


πŸš€ Features

  • πŸ€– ChatGPT Integration - Send messages, conversations, and custom requests to OpenAI's API
  • 🎨 DALL-E Image Generation - Generate images with basic and advanced options
  • πŸ“§ Email Service - Send emails via MailerSend with email format validation
  • πŸ“ File Storage - Upload and retrieve files from S3-compatible storage (DigitalOcean Spaces)
  • πŸ“„ Document Validation - Validate Brazilian CPF and CNPJ numbers with check digit verification
  • πŸ”€ String Utilities - Slug generation, number extraction, and short unique ID generation
  • πŸ“¦ NuGet Package - Consume the API from other .NET projects with typed clients and DTOs

πŸ› οΈ Technologies Used

Core Framework

  • .NET 8.0 - Cross-platform framework for building web APIs
  • ASP.NET Core - Web framework with Swagger/OpenAPI documentation

Cloud & Storage

  • AWS SDK for S3 (4.0.6.10) - S3-compatible storage (DigitalOcean Spaces, AWS S3)
  • MailerSend API - Email delivery service

AI & Image Processing

  • OpenAI API - ChatGPT and DALL-E integration
  • SixLabors.ImageSharp (3.1.11) - Image processing

Additional Libraries

  • Newtonsoft.Json (13.0.3) - JSON serialization
  • Stripe.NET (48.5.0) - Payment processing
  • Swashbuckle.AspNetCore (9.0.4) - Swagger/OpenAPI docs
  • NoobsMuc.Coinmarketcap.Client (3.1.1) - Cryptocurrency data

Testing

  • xUnit (2.5.3) - Unit testing framework
  • Moq (4.20.70) - Mocking library
  • RichardSzalay.MockHttp (7.0.0) - HTTP client mocking

DevOps

  • Docker - Multi-stage containerization
  • GitHub Actions - CI/CD with automated testing, deployment, and NuGet publishing
  • GitVersion - Semantic versioning

πŸ“ Project Structure

zTools/
β”œβ”€β”€ .github/workflows/           # CI/CD pipelines
β”‚   β”œβ”€β”€ deploy-prod.yml          # Production deployment via SSH + Docker
β”‚   β”œβ”€β”€ publish-nuget.yml        # NuGet package publishing
β”‚   └── version-tag.yml          # Semantic version tagging
β”œβ”€β”€ zTools.API/                  # API Layer
β”‚   β”œβ”€β”€ Controllers/             # 5 REST controllers
β”‚   β”‚   β”œβ”€β”€ ChatGPTController    # AI text & image generation
β”‚   β”‚   β”œβ”€β”€ DocumentController   # CPF/CNPJ validation
β”‚   β”‚   β”œβ”€β”€ FileController       # S3 file operations
β”‚   β”‚   β”œβ”€β”€ MailController       # Email & validation
β”‚   β”‚   └── StringController     # String utilities
β”‚   β”œβ”€β”€ Program.cs               # Host configuration
β”‚   └── Startup.cs               # Middleware & DI setup
β”œβ”€β”€ zTools.Application/          # DI wiring layer
β”‚   └── Initializer.cs           # Service registration
β”œβ”€β”€ zTools.Domain/               # Business logic layer
β”‚   β”œβ”€β”€ Services/                # Service interfaces & implementations
β”‚   β”‚   β”œβ”€β”€ ChatGPTService       # OpenAI API client
β”‚   β”‚   β”œβ”€β”€ FileService          # S3 storage client
β”‚   β”‚   └── MailerSendService    # Email delivery client
β”‚   └── Utils/                   # Static utility classes
β”‚       β”œβ”€β”€ DocumentoUtils       # CPF/CNPJ validation
β”‚       β”œβ”€β”€ EmailValidator       # Email format validation
β”‚       β”œβ”€β”€ ShuffleEx            # Fisher-Yates shuffle extension
β”‚       β”œβ”€β”€ SlugHelper           # URL-friendly slug generation
β”‚       └── StringUtils          # Number extraction & unique IDs
β”œβ”€β”€ zTools.Tests/                # Test suite
β”‚   β”œβ”€β”€ ACL/                     # Anti-corruption layer tests
β”‚   └── Domain/                  # Service & utility tests
β”œβ”€β”€ zTools/                      # NuGet package (ACL + DTO)
β”œβ”€β”€ Dockerfile                   # Multi-stage build
β”œβ”€β”€ docker-compose.yml           # Development environment
β”œβ”€β”€ docker-compose-prod.yml      # Production environment
β”œβ”€β”€ .env.example                 # Environment variable template
└── GitVersion.yml               # Semantic versioning config

Ecosystem

Project Type Package Description
zTools API REST API - This project β€” utility API server
zTools NuGet NuGet ACL clients & DTOs for consuming the API

Dependency Graph

zTools.API (REST API)
β”œβ”€β”€ zTools.Application β†’ DI registration
β”œβ”€β”€ zTools.Domain      β†’ Business logic & services
β”‚   └── zTools         β†’ ACL clients & DTOs (NuGet)
└── zTools             β†’ ACL clients & DTOs (NuGet)

Want to consume this API from another .NET project? Install the zTools package to get strongly-typed clients and DTOs with full IntelliSense support.


βš™οΈ Environment Configuration

1. Copy the environment template

cp .env.example .env

2. Edit the .env file

# MailerSend Configuration
MAILERSEND_MAILSENDER=your-email@example.com
MAILERSEND_APIURL=https://api.mailersend.com/v1/email
MAILERSEND_APITOKEN=your-mailersend-api-token

# ChatGPT Configuration
CHATGPT_APIKEY=your-openai-api-key
CHATGPT_APIURL=https://api.openai.com/v1/chat/completions
CHATGPT_MODEL=gpt-5.2
CHATGPT_IMAGEAPIURL=https://api.openai.com/v1/images/generations

# DigitalOcean Spaces (S3) Configuration
S3_ACCESSKEY=your-digitalocean-spaces-access-key
S3_SECRETKEY=your-digitalocean-spaces-secret-key
S3_ENDPOINT=https://your-space-name.nyc3.digitaloceanspaces.com

⚠️ IMPORTANT:

  • Never commit the .env file with real credentials
  • Only the .env.example should be version controlled
  • Change all default values before deployment

🐳 Docker Setup

Quick Start

# Build and start
docker compose up -d --build

# View logs
docker compose logs -f ztools-api

# Stop
docker compose down

Production Deployment

docker compose -f docker-compose-prod.yml up -d --build

Accessing the API

Service URL
API Base http://localhost:5001
Swagger UI http://localhost:5001/swagger
Health Check http://localhost:5001/

Docker Compose Commands

Action Command
Start services docker compose up -d
Start with rebuild docker compose up -d --build
Stop services docker compose stop
View status docker compose ps
View logs docker compose logs -f
Remove containers docker compose down
Full rebuild (no cache) docker compose build --no-cache && docker compose up -d

πŸ”§ Manual Setup (Without Docker)

Prerequisites

  • .NET 8.0 SDK
  • MailerSend API account
  • OpenAI API account
  • DigitalOcean Spaces (or S3-compatible storage) account

Setup Steps

1. Restore dependencies

dotnet restore

2. Build the project

dotnet build

3. Run the API

dotnet run --project zTools.API

The API will be available at https://localhost:9001/swagger.


πŸ§ͺ Testing

Running Tests

All tests:

dotnet test

With coverage:

dotnet test --configuration Release --collect:"XPlat Code Coverage"

Single test class:

dotnet test --filter "FullyQualifiedName~zTools.Tests.Domain.Utils.SlugHelperTest"

Single test method:

dotnet test --filter "FullyQualifiedName~zTools.Tests.Domain.Utils.SlugHelperTest.TestGenerateSlug"

Test Structure

zTools.Tests/
β”œβ”€β”€ ACL/                    # Anti-corruption layer client tests
└── Domain/
    β”œβ”€β”€ Services/           # ChatGPT, FileService, MailerSend tests
    └── Utils/              # SlugHelper, StringUtils, DocumentUtils,
                            # EmailValidator, ShuffleEx tests

Test patterns: [Fact] for single cases, [Theory] with [InlineData] for parameterized tests. Uses Moq for dependency mocking and RichardSzalay.MockHttp for HTTP client testing.


πŸ“š API Documentation

Interactive documentation is available via Swagger UI at /swagger when running the API.

ChatGPT Controller (/ChatGPT)

Method Endpoint Description
POST /ChatGPT/sendMessage Send a single message to ChatGPT
POST /ChatGPT/sendConversation Send multi-message conversation
POST /ChatGPT/sendRequest Send custom request with full parameters (model, temperature, max tokens)
POST /ChatGPT/generateImage Generate image with DALL-E
POST /ChatGPT/generateImageAdvanced Generate image with advanced DALL-E options

Example β€” Send Message:

POST /ChatGPT/sendMessage
{
  "message": "What is the capital of France?"
}
// Response: "The capital of France is Paris."

Example β€” Send Conversation:

POST /ChatGPT/sendConversation
[
  { "role": "system", "content": "You are a helpful assistant." },
  { "role": "user", "content": "Explain REST APIs" }
]

Example β€” Custom Request:

POST /ChatGPT/sendRequest
{
  "model": "gpt-4o",
  "messages": [
    { "role": "user", "content": "Hello" }
  ],
  "temperature": 0.7,
  "maxCompletionTokens": 500
}

Mail Controller (/Mail)

Method Endpoint Description
POST /Mail/sendMail Send email via MailerSend
GET /Mail/isValidEmail/{email} Validate email format

Example β€” Send Email:

POST /Mail/sendMail
{
  "to": "recipient@example.com",
  "subject": "Test Email",
  "from": "sender@example.com",
  "html": "<h1>Hello World</h1>"
}

File Controller (/File)

Method Endpoint Description
GET /File/{bucketName}/getFileUrl/{fileName} Get public URL of a stored file
POST /File/{bucketName}/uploadFile Upload file to S3 bucket (100MB limit)

Example β€” Upload File:

POST /File/my-bucket/uploadFile
Content-Type: multipart/form-data
file: [binary data]

Document Controller (/Document)

Method Endpoint Description
GET /Document/validarCpfOuCnpj/{cpfCnpj} Validate Brazilian CPF or CNPJ

Example:

GET /Document/validarCpfOuCnpj/12345678900
// Response: true/false

String Controller (/String)

Method Endpoint Description
GET /String/generateSlug/{name} Convert text to URL-friendly slug
GET /String/onlyNumbers/{input} Extract only numeric characters
GET /String/generateShortUniqueString Generate short unique Base62 ID

Examples:

GET /String/generateSlug/Hello World 123     β†’ "hello-world-123"
GET /String/onlyNumbers/abc123def456         β†’ "123456"
GET /String/generateShortUniqueString        β†’ "x4k9p2"

πŸ”„ CI/CD

GitHub Actions Workflows

Deploy to Production (deploy-prod.yml)

  • Triggers: Push to main, manual dispatch
  • Steps:
    1. Run tests (restore β†’ build β†’ test)
    2. SSH into production server
    3. Pull latest code
    4. Inject secrets from GitHub Secrets
    5. Build and deploy with Docker Compose

Version Tagging (version-tag.yml)

  • Triggers: Push to main
  • Steps: Runs GitVersion to create semantic version tags

Publish NuGet (publish-nuget.yml)

  • Triggers: After successful version tagging
  • Steps: Build, pack, and publish zTools package to NuGet.org

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Make your changes
  4. Run tests (dotnet test)
  5. Commit your changes (git commit -m 'Add some AmazingFeature')
  6. Push to the branch (git push origin feature/AmazingFeature)
  7. Open a Pull Request

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

Developed by Rodrigo Landim


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ“ž Support


⭐ If you find this project useful, please consider giving it a star!

About

microservices tools

Resources

License

Stars

Watchers

Forks

Contributors

Languages