simple_json_server
This library is intended to make it very easy to build a JSON-based server. Because its JSON it is easily called from multiple languages (think a TypeScript client). The goal is to provide a lot of functionality out of the box but with excellent ergonomics so that simple servers are easily built without almost any boilerplate.
The approach is based on the Actor model of computation with some necessary deviations from the pure model. We'll document those deviations here. Initially they are:
- Rust itself isn't functional in the same way as Lisp. So we eschew
becomeas a keyword and generally find completely changing behavior is superfluous for general work. - Each actor is single threaded; we take this approach instead of using
becometo manage state changes. - Addresses are well known. This is critical to support cross language invocation (the classic client/server case) and contrasts from the Actor model where addresses are passed around.
- RPC and return values are supported. Again, this is important to simplify cross language invocation and generally simplifies all distributed code at the cost of asynchronous execution and parallelism. In the future we'll look to optimize cases that don't need return values by not waiting for method completion.
Quick Start
The easiest way to create an actor is using the #[actor] macro:
use ;
The #[actor] Macro
The #[actor] procedural macro automatically implements the Actor trait for your struct by:
- Analyzing public async methods in the impl block
- Generating message structs for each method's parameters
- Creating a dispatch method that handles JSON messages mapped from the method parameters.
Features
- Automatic JSON serialization/deserialization of parameters and return values
- Error handling for invalid JSON, unknown methods, and parameter mismatches
- Async support with automatic runtime management
- Type safety with generated message structs
- Snake_case to PascalCase conversion for message struct names
- Comprehensive RustDoc generation with method tables, JSON payload examples, and usage instructions
- TLS/SSL support for secure HTTPS and WSS (WebSocket Secure) connections
Method Requirements
The macro only processes methods that are:
- Public (
pub) - Async (
async fn)
Private methods and synchronous methods are ignored.
Server Support
The library includes built-in HTTP and WebSocket server support. Use the create_options method to start a server.
Note: create_options consumes the actor (moves it), preventing accidental use after starting the server. This is a safety feature that ensures the actor is only used within the server context.
use ;
HTTP Server
The HTTP server expects POST requests with the method name in the URL path and parameters in the JSON body:
# Call the 'greet' method
WebSocket Server
The WebSocket server expects JSON messages in the standard format:
TLS/SSL Support
The library supports secure connections using TLS for both HTTP (HTTPS) and WebSocket (WSS) protocols.
Setting up TLS
First, create a TlsConfig with your certificate and private key files:
use ;
let tls_config = new;
HTTPS Server
// Start an HTTPS server
actor.create_https;
WSS (WebSocket Secure) Server
// Start a WSS server
actor.create_wss;
Generating Test Certificates
For development and testing, you can generate self-signed certificates:
# Use the provided script
# Or manually with OpenSSL
Testing HTTPS Endpoints
# Use -k flag to accept self-signed certificates
TLS Configuration
The TlsConfig struct supports:
- Certificate file: PEM format certificate file
- Private key file: PEM format private key file (PKCS#8)
- Automatic loading: Certificates are loaded asynchronously when the server starts
Examples
There are examples in the simple_json_server crate itself. See the examples/ directory for a more complete (yet simple) demo. The demo will build on its own.
# Basic calculator example
# HTTP and WebSocket server example
# TLS/SSL server example (HTTPS and WSS)
# The full example
Actor Trait
The Actor trait defines two methods:
If you need more control, you can implement the Actor trait manually instead of using the #[actor] macro.
Documentation
The #[actor] macro automatically generates comprehensive RustDoc documentation for your Actor implementations. To view the generated documentation:
The generated documentation includes:
- Method overview table with parameters and return types
- Detailed method documentation extracted from your doc comments
- JSON payload examples for each method
- Usage examples showing how to call each method via
dispatch - Parameter specifications with type information
This makes it easy to understand the JSON API without looking at the source code!
CI/CD and Quality Assurance
This project uses comprehensive GitHub Actions workflows to ensure code quality:
๐ Continuous Integration
- Multi-Rust Version Testing: Tests on stable, beta, and nightly Rust
- Code Formatting: Enforces consistent formatting with
rustfmt - Linting: Uses
clippywith strict warnings - Comprehensive Testing: Runs all unit and integration tests
- Example Validation: Ensures all examples compile and run
๐ Code Coverage
- Coverage Reports: Generated using
cargo llvm-cov - Codecov Integration: Automatic coverage reporting and tracking
- Coverage Artifacts: HTML reports available for download
- Target: Maintains 80%+ coverage on testable code
๐ Security & Dependencies
- Security Audits: Regular
cargo auditchecks for vulnerabilities - License Compliance: Automated license checking with
cargo-deny - Dependency Updates: Weekly automated dependency update PRs
- MSRV Testing: Ensures compatibility with minimum supported Rust version (1.85+)
๐ Documentation & Releases
- Documentation Building: Validates all docs build correctly
- Release Automation: Automatic binary builds for multiple platforms
- Crate Publishing: Automated publishing to crates.io on tags
- Cross-Platform: Builds for Linux, Windows, and macOS (x86_64 + ARM64)
๐งช Integration Testing
- JavaScript Client Testing: Validates the generated JSON-RPC interface
- Server Lifecycle Testing: Tests server startup, API calls, and shutdown
- Cross-Language Validation: Ensures Rust-JavaScript interoperability
๐ Quality Metrics
All workflows are designed to maintain high code quality while enabling rapid development and reliable releases.