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.
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 macro both creates your simple server (an actor) from an impl of your struct and also generates documentation for your actor so its 100% clear how to call it.
The way simple_json_server works is:
-
Create a
structand create animplblock for it. Theimplblock should contain all the methods you want to expose on your server. The methods must bepub async fnand take&selfas the first parameter. Others are ignored for this purpose. -
The
#[actor]macro will generate the code to make your struct into an actor. Each method in turn gets turned into a JSON-RPC where:- The RPC name is the name of the method in your
impl - The parameters are serialized as JSON parameters of the method. They are automatically turned into a JSON object with the name of the parameter as the key. The value is the JSON equivalent of the value of the parameter
- The return value is the JSON-serialized return value of the method
- The macro will generate documentation for your actor so it's 100% clear how to call it including the specific JSON payload needed to call the method
- The RPC name is the name of the method in your
-
The
createmethod will start a server for you and begin listening for incoming JSON-RPC calls via HTTP.create_wswill do the same but via WebSocket. Options forhttpsandwssare also provided.
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
- 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 and variants on create 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!