Skip to main content

Module router

Module router 

Source
Expand description

Protocol-agnostic request routing for REST, GraphQL, and gRPC.

Write handlers once, expose them via any protocol:

§Example

use allframe_core::router::{Router, RestAdapter, GraphQLAdapter, GrpcAdapter};

let mut router = Router::new();
router.register("get_user", || async { r#"{"id": 1}"#.to_string() });

// Same handler, three protocols!
let mut rest = RestAdapter::new();
rest.route("GET", "/users/:id", "get_user");

let mut graphql = GraphQLAdapter::new();
graphql.query("user", "get_user");

let mut grpc = GrpcAdapter::new();
grpc.unary("UserService", "GetUser", "get_user");

Also includes documentation generators:

  • scalar_html - Scalar UI for REST APIs
  • graphiql_html - GraphiQL playground for GraphQL
  • grpc_explorer_html - gRPC Explorer

§Protocol-Agnostic Router

Write handlers once, expose them via REST, GraphQL, and gRPC.

This is AllFrame’s core differentiator - the same handler can serve multiple protocols without code changes.

§Quick Start

use allframe_core::router::{Router, RestAdapter, GraphQLAdapter, GrpcAdapter};

// Create router and register handlers
let mut router = Router::new();
router.register("get_user", || async {
    r#"{"id": 42, "name": "Alice"}"#.to_string()
});

// Expose via REST
let mut rest = RestAdapter::new();
rest.route("GET", "/users/:id", "get_user");

// Expose via GraphQL
let mut graphql = GraphQLAdapter::new();
graphql.query("user", "get_user");

// Expose via gRPC
let mut grpc = GrpcAdapter::new();
grpc.unary("UserService", "GetUser", "get_user");

§Key Types

  • Router - Central handler registry
  • RestAdapter - REST protocol adapter
  • GraphQLAdapter - GraphQL protocol adapter
  • GrpcAdapter - gRPC protocol adapter
  • ProtocolAdapter - Trait for custom protocol adapters

§API Documentation

Generate beautiful API documentation automatically:

  • scalar_html - Scalar UI for REST APIs (<50KB)
  • graphiql_html - GraphiQL playground for GraphQL
  • grpc_explorer_html - gRPC Explorer for gRPC services
  • OpenApiGenerator - OpenAPI 3.1 spec generation

§Configuration-Driven Protocol Selection

Use TOML configuration to select protocols without code changes:

[server]
protocols = ["rest", "graphql", "grpc"]

[server.rest]
port = 8080

[server.graphql]
port = 8081

Re-exports§

pub use adapter::ProtocolAdapter;
pub use builder::RouteBuilder;
pub use config::GraphQLConfig;router
pub use config::GrpcConfig;router
pub use config::RestConfig;router
pub use config::RouterConfig;router
pub use config::ServerConfig;router
pub use contract::ContractTestConfig;
pub use contract::ContractTestResult;
pub use contract::ContractTestResults;
pub use contract::ContractTestable;
pub use contract::ContractTester;
pub use docs::DocsConfig;
pub use graphiql::graphiql_html;
pub use graphiql::GraphiQLConfig;
pub use graphiql::GraphiQLTheme;
pub use graphql::GraphQLAdapter;
pub use graphql::GraphQLOperation;
pub use graphql::OperationType;
pub use graphql_prod::GraphQLProductionAdapter;router-graphql
pub use grpc::GrpcAdapter;
pub use grpc::GrpcMethod;
pub use grpc::GrpcMethodType;
pub use grpc::GrpcRequest;
pub use grpc::GrpcStatus;
pub use grpc_explorer::grpc_explorer_html;
pub use grpc_explorer::GrpcExplorerConfig;
pub use grpc_explorer::GrpcExplorerTheme;
pub use grpc_prod::protobuf;router-grpc
pub use grpc_prod::status;router-grpc
pub use grpc_prod::streaming;router-grpc
pub use grpc_prod::GrpcProductionAdapter;router-grpc
pub use grpc_prod::GrpcService;router-grpc
pub use handler::Handler;
pub use handler::HandlerFn;
pub use handler::HandlerWithArgs;
pub use handler::HandlerWithState;
pub use handler::HandlerWithStateOnly;
pub use handler::IntoHandlerResult;
pub use handler::IntoStreamItem;
pub use handler::Json;
pub use handler::SharedStateMap;
pub use handler::State;
pub use handler::StreamError;
pub use handler::StreamHandler;
pub use handler::StreamReceiver;
pub use handler::StreamSender;
pub use handler::StreamingHandlerFn;
pub use handler::StreamingHandlerWithArgs;
pub use handler::StreamingHandlerWithState;
pub use handler::StreamingHandlerWithStateOnly;
pub use handler::DEFAULT_STREAM_CAPACITY;
pub use metadata::RouteMetadata;
pub use method::Method;
pub use openapi::OpenApiGenerator;
pub use openapi::OpenApiServer;
pub use rest::RestAdapter;
pub use rest::RestRequest;
pub use rest::RestResponse;
pub use rest::RestRoute;
pub use scalar::scalar_html;
pub use scalar::ScalarConfig;
pub use scalar::ScalarLayout;
pub use scalar::ScalarTheme;
pub use schema::ToJsonSchema;
pub use ts_codegen::generate_ts_client;
pub use ts_codegen::HandlerMeta;
pub use ts_codegen::TsField;
pub use ts_codegen::TsType;

Modules§

adapter
Protocol adapter trait for supporting multiple protocols
builder
Route builder for fluent route configuration
configrouter
Configuration-driven protocol selection
contract
Contract Testing Support
docs
Documentation serving helpers
graphiql
GraphiQL Playground Integration
graphql
GraphQL protocol adapter
graphql_prodrouter-graphql
Production GraphQL adapter using async-graphql
grpc
gRPC protocol adapter
grpc_explorer
gRPC Service Explorer Integration
grpc_prodrouter-grpc
Production gRPC adapter using tonic and prost
handler
Handler trait and implementations for protocol-agnostic request handling
metadata
Route metadata for documentation generation
method
HTTP method types for type-safe routing
openapi
OpenAPI 3.1 specification generation
rest
REST/HTTP protocol adapter
scalar
Scalar UI integration for interactive OpenAPI documentation.
schema
JSON Schema generation for OpenAPI documentation
ts_codegen
TypeScript client code generation from Router handler metadata

Structs§

Router
Router manages handler registration and protocol adapters