Skip to content

hash-line/wright-server-ktor

Repository files navigation

Wright Server Ktor

⚙️ Wright Server - A powerful middleware runtime built with Ktor for dynamic API mapping and transformation

Overview

Wright Server Ktor is a lightweight middleware application that provides dynamic API mapping, transformation, and routing capabilities. It acts as an intelligent proxy that can transform requests and responses between different API formats (SOAP to REST, REST to REST) while providing advanced data transformation capabilities.

Key Features

🔄 Dynamic API Mapping

  • SOAP to REST transformation
  • REST to REST mapping
  • Dynamic routing based on request patterns
  • Configurable endpoint mappings via YAML configuration

🛠️ Advanced Data Transformation

  • Built-in transformation functions including:
    • Cryptographic operations (RSA encryption/decryption, AES encryption)
    • Data type conversions (string, int, double, boolean)
    • String manipulation (case conversion, substring extraction)
    • Date/time formatting and calculations
    • Mathematical operations (sum, subtract)
    • Conditional logic and expressions
    • Name parsing (first name, last name extraction)

📊 Observability & Monitoring

  • OpenTelemetry integration for distributed tracing
  • Jaeger-compatible span naming and correlation
  • Structured logging with MDC correlation
  • Health check endpoints for monitoring

🔒 Security & Configuration

  • Configurable authentication (Bearer tokens, API keys)
  • Proxy support with SSL verification options
  • Environment-based configuration management
  • Sensitive data masking capabilities

🚀 High Performance

  • Asynchronous processing with Kotlin coroutines
  • HTTP client optimization with compression and connection pooling
  • Configurable timeouts and retries
  • Docker containerization ready

Architecture

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Client App    │───▶│  Wright Server   │───▶│  Target API     │
│                 │    │     (Ktor)       │    │                 │
└─────────────────┘    └──────────────────┘    └─────────────────┘
                              │
                              ▼
                       ┌──────────────────┐
                       │  Mapping Engine  │
                       │  + Transformations│
                       └──────────────────┘

Quick Start

Prerequisites

  • Java 11 or higher
  • Docker (optional)

Running with Docker

  1. Clone the repository

    git clone <repository-url>
    cd wright-server-ktor
  2. Configure environment variables

    # Edit docker-compose.yml and set:
    - BASE_URL=<your-target-api-url>
    - BEARER_TOKEN=<your-auth-token>
    - OTEL_EXPORTER_OTLP_ENDPOINT=<your-observability-endpoint>
  3. Start the application

    docker-compose up -d
  4. Verify deployment

    curl http://localhost:8080/health
    # Expected response: "Healthy! 🚀"

Running Locally

  1. Build the project

    ./gradlew build
  2. Run the application

    ./gradlew :server:run

Configuration

Application Configuration (application.yaml)

ktor:
  deployment:
    port: 8080
    contextPath: "" # API context path

middleware:
  mappingConfigPath: "mapping/sample_mapping.yaml"
  wright:
    baseUrl: ${BASE_URL}
    timeout: 120000
    bearerToken: ${BEARER_TOKEN}
    maxRetries: 3
  logging:
    logBodies: true
    maskSensitive: false
  crypto:
    aesKey: "Your AES Key Here"
    iv: "Your IV Here"
    rsaPrivateKey: "Your RSA Private Key Here"
  proxy:
    enabled: ${PROXY_ENABLED:false}
    host: ${PROXY_HOST:localhost}
    port: ${PROXY_PORT:5559}
    type: ${PROXY_TYPE:HTTP}
    disableSslVerification: ${PROXY_DISABLE_SSL_VERIFICATION:false}

Mapping Configuration

The application uses YAML-based mapping configurations to define API transformations. See server/src/main/resources/mapping/sample_mapping.yaml for examples.

API Endpoints

Core Endpoints

  • GET /health - Health check endpoint
  • GET / - Redirects to Swagger documentation
  • POST /{...} - Dynamic routing for all API transformations

Dynamic Routing

The application supports dynamic routing where any POST request to any path will be processed based on the mapping configuration. The system automatically:

  1. Extracts request metadata (path, method, content type, SOAP action)
  2. Matches against configured mappings
  3. Transforms request data using defined schemas and transformation rules
  4. Forwards to target API
  5. Transforms response data back to client format
  6. Returns transformed response

Transformation Functions

Wright Server provides a comprehensive set of built-in transformation functions:

Cryptographic Functions

  • RSADecrypt(cipherText) - RSA decryption
  • AESEncrypt(plainText) - AES encryption

Data Type Conversions

  • toString(value) - Convert to string
  • toInt(value) - Convert to integer
  • toDouble(value) - Convert to double
  • toBool(value) - Convert to boolean

String Operations

  • toUpper(value) / toLower(value) - Case conversion
  • takeLast6(value) - Extract last 6 characters
  • firstName(fullName) / lastName(fullName) - Name parsing
  • len(value) - String length

Mathematical Operations

  • sum(value1, value2, ...) - Addition
  • subtract(value1, value2, ...) - Subtraction

Date/Time Functions

  • formatDate(source, inputFormat, outputFormat) - Date formatting
  • timeDiff(field1, field2, dateFormat) - Time difference calculation

Conditional Logic

  • takeIf(expression, trueValue, falseValue) - Conditional values
  • exp(expression) - Expression evaluation

Specialized Functions

  • toISOAmountFormat(amount) - Currency formatting

Development

Project Structure

wright-server-ktor/
├── server/                 # Main Ktor application
│   ├── src/main/kotlin/
│   │   ├── Application.kt  # Application entry point
│   │   ├── config/        # Configuration management
│   │   ├── core/          # Core mapping and transformation logic
│   │   ├── domain/        # Domain models
│   │   ├── model/         # Data models
│   │   ├── service/       # Business logic services
│   │   └── utils/         # Utility functions
│   └── src/main/resources/
│       ├── application.yaml
│       └── mapping/       # Mapping configurations
├── core/                  # Shared core library
├── client/               # HTTP client library
└── docker-compose.yml    # Docker configuration

Building and Testing

# Build all modules
./gradlew build

# Run tests
./gradlew test

# Run specific module tests
./gradlew :server:test

# Build Docker image
docker build -t wright-server-ktor .

Observability

OpenTelemetry Integration

The application is fully instrumented with OpenTelemetry for:

  • Distributed tracing across service boundaries
  • Span correlation with request/response cycles
  • Custom span naming for better Jaeger filtering
  • Context propagation for end-to-end tracing

Logging

  • Structured logging with SLF4J
  • MDC correlation with trace IDs
  • Configurable log levels and body logging
  • Sensitive data masking options

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Submit a pull request

License

This project is licensed under the terms specified in the LICENSE file.

Support

For questions, issues, or contributions, please refer to the project's issue tracker or contact the development team.

About

⚙️ Wright Server - Ktor Runtime for Wright Configurations

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors