Skip to main content

Hexagonal runtime switchboard for config-driven microservices

Project description

HexSwitch Logo

HexSwitch

Hexagonal runtime switchboard for config-driven microservices.

Description

HexSwitch is a runtime system designed to orchestrate microservices using a hexagonal architecture pattern. It provides a configuration-driven approach to wiring together inbound and outbound adapters, enabling flexible and maintainable service communication.

Status: Core runtime functionality is implemented, including adapter framework, HTTP inbound adapter, and runtime orchestration. The system can now run microservices with config-driven adapter wiring.

Installation

Install HexSwitch in development mode:

pip install -e ".[dev]"

This installs the package and all development dependencies (linting, testing, type checking).

Running Tests

Run the test suite:

pytest

Run with coverage report:

pytest --cov=src/hexswitch --cov-report=html

Run only unit tests:

pytest tests/unit/

Run only integration tests:

pytest tests/integration/

Run Docker integration tests:

pytest tests/integration/test_docker.py -m docker

Run multi-container integration tests:

pytest tests/integration/test_multi_container.py -v

Or use the test scripts:

# Linux/Mac
./scripts/test-docker.sh

# Windows PowerShell
.\scripts\test-docker.ps1

Multi-Container Testing

Test interactions between multiple HexSwitch instances:

# Start multi-container setup
docker compose -f docker-compose.multi-test.yml up -d

# Run tests
pytest tests/integration/test_multi_container.py -v

# Stop and cleanup
docker compose -f docker-compose.multi-test.yml down -v

For detailed development instructions, see Development Guide.

Running the CLI

After installation, you can run HexSwitch from the command line:

Available Commands

Show version:

hexswitch version
# or
hexswitch --version

Create example configuration:

hexswitch init

Create configuration from template:

hexswitch init --template hex-config.http-only
hexswitch init --list-templates  # List available templates

Configuration Templates

HexSwitch supports Jinja2 templates for configuration files. Templates allow you to use environment variables and dynamic values in your configuration.

Template files must have a .j2 extension (e.g., hex-config.toml.j2). The load_config() function automatically detects and renders templates before parsing TOML.

Available templates:

  • hex-config.toml.j2 - Full configuration with all adapters
  • hex-config.http-only.toml.j2 - Minimal HTTP-only configuration
  • hex-config.with-mcp.toml.j2 - Configuration with MCP client adapter

Example template usage:

service:
  name: {{ env.SERVICE_NAME | default("example-service") }}
  runtime: python

inbound:
  http:
    enabled: {{ env.ENABLE_HTTP | default(true) }}
    port: {{ env.HTTP_PORT | default(8000) | int }}
    base_path: {{ env.BASE_PATH | default("/api") }}

Environment variables are available via env.VAR_NAME in templates. Use Jinja2 filters like default(), int(), bool() for type conversion. hexswitch init

Creates hex-config.toml with example configuration

hexswitch init --force # Overwrite existing file


**Validate configuration:**
```bash
hexswitch validate
# Validates hex-config.toml (default)
hexswitch validate --config path/to/config.toml

Run runtime (dry-run mode):

hexswitch run --dry-run
# Shows execution plan without starting runtime

Run runtime:

hexswitch run
# Starts the runtime with all enabled adapters

Global Options

  • --log-level: Set logging level (DEBUG, INFO, WARNING, ERROR)
  • --config: Path to configuration file (default: hex-config.toml)

Example Workflow

# 1. Create example configuration
hexswitch init

# 2. Validate configuration
hexswitch validate

# 3. Preview execution plan
hexswitch run --dry-run

# 4. Run runtime
hexswitch run

Or run it as a Python module:

python -m hexswitch.app version
python -m hexswitch.app init
python -m hexswitch.app validate
python -m hexswitch.app run --dry-run

Programmgesteuerte Verwendung

Einfache Verwendung mit HexSwitchService

Die einfachste Art, HexSwitch programmgesteuert zu verwenden, ist die HexSwitchService-Klasse:

from hexswitch.service import HexSwitchService

class MyService(HexSwitchService):
    def on_start(self):
        """Wird vor Runtime-Start aufgerufen."""
        print("Service wird gestartet...")
    
    def on_ready(self):
        """Wird nach erfolgreichem Start aufgerufen."""
        print("Service ist bereit!")
    
    def on_stop(self):
        """Wird vor Runtime-Stop aufgerufen."""
        print("Service wird gestoppt...")

if __name__ == "__main__":
    # Service erstellen (lädt automatisch hex-config.toml)
    service = MyService()
    
    # Service starten und laufen lassen (bis unterbrochen)
    service.run()  # Behandelt KeyboardInterrupt automatisch

Vorteile von HexSwitchService:

  • Automatische Runtime-Integration
  • Automatisches Config-Loading (aus Datei oder Umgebungsvariable)
  • Lifecycle-Hooks für benutzerdefinierte Initialisierung
  • Standard-Main-Loop mit run() Methode
  • Automatisches Environment-Variable-Loading mit HEX_ Präfix

Environment-Variable-Overrides

HexSwitch unterstützt automatisches Überschreiben von Config-Werten durch Umgebungsvariablen mit dem Präfix HEX_:

# Umgebungsvariablen setzen
export HEX_SERVICE_NAME="my-service"
export HEX_INBOUND_HTTP_PORT="9000"
export HEX_INBOUND_HTTP_BASE_PATH="/api/v2"
export HEX_LOGGING_LEVEL="DEBUG"

# Service starten - Variablen überschreiben automatisch Config-Werte
python my_service.py

Konvertierung:

  • HEX_SERVICE_NAMEservice.name
  • HEX_INBOUND_HTTP_PORTinbound.http.port (automatisch als Integer)
  • HEX_INBOUND_HTTP_BASE_PATHinbound.http.base_path
  • HEX_LOGGING_LEVELlogging.level

Automatische Typkonvertierung:

  • Boolean: "true" / "false"True / False
  • Integer: "8080"8080
  • Float: "30.5"30.5
  • String: bleibt als String

Vorteile:

  • Einfach: Keine Template-Dateien erforderlich
  • Docker-freundlich: Perfekt für Container-Umgebungen
  • CI/CD-freundlich: Einfache Integration in Deployment-Pipelines
  • Automatische Signal-Handler für graceful shutdown
  • Einfache API: start(), stop(), is_running()

Weitere Optionen:

# Mit explizitem Config-Pfad
service = MyService(config_path="custom-config.toml")

# Mit Umgebungsvariable
# export HEXSWITCH_CONFIG_PATH=/path/to/config.toml
service = MyService()  # Lädt automatisch aus Umgebungsvariable

# Mit Config-Dictionary
config = {
    "service": {"name": "my-service"},
    "inbound": {...},
    "outbound": {...}
}
service = MyService(config=config)

Erweiterte Verwendung mit Runtime

Für erweiterte Szenarien können Sie die Runtime-Klasse direkt verwenden:

from hexswitch.runtime import Runtime
from hexswitch.shared.config import load_config

# Konfiguration laden
config = load_config("hex-config.toml")

# Runtime erstellen
runtime = Runtime(config)

# Runtime starten
runtime.start()

try:
    # Runtime läuft...
    import time
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print("Shutting down...")
finally:
    runtime.stop()

Project Structure

  • src/hexswitch/ - Python package (core runtime, adapters, handlers)
  • tests/ - Test suite (unit and integration tests)
  • docs/ - Project documentation

Development

See CONTRIBUTING.md for development guidelines and workflow.

Docker

Build and test the Docker image:

# Build Docker image
docker build -t hexswitch:latest .

# Test Docker image
docker run --rm hexswitch:latest hexswitch version

For more details, see Development Guide.

Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hexswitch-0.1.2.tar.gz (87.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hexswitch-0.1.2-py3-none-any.whl (117.4 kB view details)

Uploaded Python 3

File details

Details for the file hexswitch-0.1.2.tar.gz.

File metadata

  • Download URL: hexswitch-0.1.2.tar.gz
  • Upload date:
  • Size: 87.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hexswitch-0.1.2.tar.gz
Algorithm Hash digest
SHA256 610fc6308b89c7daf23f8421fadd5b9574f24e3caae9a4028eaee9eb072febd9
MD5 ec830a306da5b004269eb08c8dfd8550
BLAKE2b-256 df0c78a0997d9cad2ad8c657ffc8fc8755d9901124654b57626c0a623e5217a4

See more details on using hashes here.

File details

Details for the file hexswitch-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: hexswitch-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 117.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for hexswitch-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3fc5d46e1711b28ca5bdf3bae0192b500fbe0aa6585aeeff579872f0cbe4e40a
MD5 da31744c52a23dd7412f012e53a56362
BLAKE2b-256 903bcfcc605b8cfda9285c68c3a36b82771969477e44801ad26da64224cdcda5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page