Skip to main content

Transform shell scripts into production-ready APIs with REST, WebRTC, and gRPC support

Project description

โœจ shapi

Transform shell scripts into production-ready APIs with REST, WebRTC, and gRPC support

PyPI Version Python Versions License Code Style Documentation Tests Codecov

๐Ÿš€ Quick Start | ๐Ÿ“– Documentation | ๐Ÿ’ก Examples | ๐Ÿค Contributing | ๐Ÿ“„ License

โœจ Features

  • ๐Ÿš€ Instant API Generation: Convert any shell script into a REST API with a single command
  • ๐Ÿค– AI-Powered: Leverage local LLM (Mistral:7b) for intelligent API generation
  • ๐Ÿณ Container Ready: Automatic Dockerfile and docker-compose.yml generation
  • ๐ŸŒ Multi-Protocol: Support for REST, WebRTC, and gRPC APIs
  • ๐Ÿงช Testing Included: Generated test suites and Ansible playbooks
  • ๐Ÿ“Š Monitoring: Built-in health checks and status endpoints
  • ๐Ÿ”ง Service Management: Start, stop, and manage multiple services
  • ๐Ÿšฆ Port Management: Automatic port conflict resolution
  • ๐ŸŽฏ Daemon Mode: Run services in the background
  • ๐Ÿ”„ Service Discovery: List and manage running services

Quick Start

Installation

# Install using pip
pip install shapi

# Or install from source
git clone https://github.com/wronai/shapi.git
cd shapi
pip install -e .

Basic Usage

# Serve a script directly
shapi serve ./hello.sh --port 8000

# Run in the background (daemon mode)
shapi serve ./hello.sh --port 8000 --daemon

# List running services
shapi service list

# Stop a running service
shapi service stop hello  # by name
shapi service stop 12345  # by PID

# Restart a service
shapi service restart hello

Generate Complete Service Structure

# Generate complete service structure with Docker and tests
shapi generate /path/to/your/script.sh --name my-service

# Navigate to the generated service
cd my-service

# Install dependencies
pip install -r requirements.txt

# Run the service
python main.py

Generated Structure

my-service/
โ”œโ”€โ”€ main.py              # FastAPI service
โ”œโ”€โ”€ Dockerfile           # Container configuration
โ”œโ”€โ”€ docker-compose.yml   # Multi-service setup
โ”œโ”€โ”€ Makefile            # Build and deployment commands
โ”œโ”€โ”€ requirements.txt     # Python dependencies
โ”œโ”€โ”€ test_service.py     # Test suite
โ”œโ”€โ”€ ansible/
โ”‚   โ””โ”€โ”€ test.yml        # Infrastructure tests
โ””โ”€โ”€ script.sh           # Your original script

Usage Examples

Basic Script Conversion

#!/bin/bash
# hello.sh
echo "Hello, $1!"

Generate the service:

shapi generate hello.sh --name greeting-service
cd greeting-service
python main.py

Access your API:

  • Health Check: GET http://localhost:8000/health
  • Documentation: GET http://localhost:8000/docs
  • Execute Script: POST http://localhost:8000/run

API Endpoints

Every generated service includes:

  • GET /health - Service health check
  • GET /info - Script information
  • POST /run - Execute script (sync/async)
  • GET /status/{task_id} - Check async task status
  • GET /docs - Interactive API documentation

Service Management

Managing Multiple Services

# Start multiple services on different ports
shapi serve ./service1.sh --name service1 --port 8000 --daemon
shapi serve ./service2.sh --name service2 --port 8001 --daemon

# List all running services
shapi service list

# Output:
# โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
# โ”ƒ Name      โ”ƒ PID    โ”ƒ Port โ”ƒ Status  โ”ƒ Uptime   โ”ƒ Script                          โ”ƒ
# โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
# โ”‚ service1  โ”‚ 12345  โ”‚ 8000 โ”‚ running โ”‚ 00:05:32 โ”‚ /path/to/service1.sh            โ”‚
# โ”‚ service2  โ”‚ 12346  โ”‚ 8001 โ”‚ running โ”‚ 00:02:15 โ”‚ /path/to/service2.sh            โ”‚
# โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

# Stop a service
shapi service stop service1

# Force stop a service if it's not responding
shapi service stop service2 --force

# Restart a service
shapi service restart service1

Port Management

# Start a service with automatic port conflict resolution
shapi serve ./service.sh --port 8000 --force

# The --force flag will automatically stop any service using port 8000

Example API Request

POST /run
{
  "parameters": {
    "name": "World",
    "verbose": true
  },
  "async_execution": false
}

Docker Deployment

# Build and run with Docker
make docker-build
make docker-run

# Or use docker-compose
docker-compose up -d

Testing

# Run tests
make test

# Or directly
python -m pytest test_service.py -v

Configuration

Create a config.yaml file for advanced configuration:

service:
  name: "my-advanced-service"
  description: "Advanced shell script API"
  version: "1.0.0"
  
protocols:
  rest: true
  grpc: true
  webrtc: true
  
security:
  auth_required: false
  cors_enabled: true
  
monitoring:
  health_check_interval: 30
  metrics_enabled: true

CLI Commands

# Generate service structure
shapi generate hello.sh --name service-name --output ./output

# Serve script directly
shapi serve hello.sh --host 0.0.0.0 --port 8008

# Test generated service
shapi test ./generated/service-name

# Build Docker image
shapi build ./generated/service-name

Advanced Features

Async Execution

# Enable async execution for long-running scripts
response = requests.post("/run", json={
    "parameters": {"input": "data"},
    "async_execution": True
})

task_id = response.json()["task_id"]

# Check status
status = requests.get(f"/status/{task_id}")

Multiple Protocols

The generated service supports multiple communication protocols:

  • REST API: Standard HTTP endpoints
  • WebRTC: Real-time data streaming
  • gRPC: High-performance RPC calls

Production Deployment

# Using Makefile
make deploy

# Manual deployment
docker-compose up -d

Requirements

  • Python 3.8+
  • Docker (optional, for containerization)
  • Bash (for shell script execution)

Contributing

Contributions are welcome! Please see our Contributing Guidelines.

License

Apache License 2.0 - see LICENSE file for details.

Support


shapi - From shell to service in seconds! ๐Ÿš€

Contributing Guidelines

CONTRIBUTING.md

"""

Contributing to shapi

We welcome contributions to shapi! This document provides guidelines for contributing.

Development Setup

  1. Clone the repository:
git clone https://github.com/wronai/shapi.git
cd shapi
  1. Create virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install in development mode:
pip install -e .[dev]

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=shapi

# Run specific test file
pytest tests/test_core.py -v

Code Style

We use black for code formatting and flake8 for linting:

# Format code
black shapi/

# Check linting
flake8 shapi/

Submitting Changes

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

Reporting Issues

Please use the GitHub issue tracker to report bugs or request features. """

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

shapi-0.2.13.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

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

shapi-0.2.13-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file shapi-0.2.13.tar.gz.

File metadata

  • Download URL: shapi-0.2.13.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.14.9-300.fc42.x86_64

File hashes

Hashes for shapi-0.2.13.tar.gz
Algorithm Hash digest
SHA256 7d58d344186dc75ae0b54f53f5118c7663ac7d6a3072cc102b0f46718e09aa8c
MD5 77fa272b64567293de1e71c5a2b9bc94
BLAKE2b-256 a92e3404b384835cfb46688a72882ef3fa03d282ca77edb2206aac3d8cebc6ea

See more details on using hashes here.

File details

Details for the file shapi-0.2.13-py3-none-any.whl.

File metadata

  • Download URL: shapi-0.2.13-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.14.9-300.fc42.x86_64

File hashes

Hashes for shapi-0.2.13-py3-none-any.whl
Algorithm Hash digest
SHA256 5ba88a8e28be880e7f17beeb46421a665561021a58ba517729cc567ee2f1a426
MD5 05b95719ad2dc79c41a83f5f352f23b3
BLAKE2b-256 67621f9464640f7f5df68f7bcc49d61d792f53af5873e65ee01a4fb6216ba7b4

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