Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Web Servers

Rama builds web services too, with a different approach than traditional web frameworks. It is best known for proxies, but web applications and APIs work just as well.

If your service surface is RPC-oriented rather than REST-oriented, also see the dedicated gRPC chapter. gRPC in Rama often builds on top of the same HTTP server machinery, while still being treated as its own protocol layer.

Philosophy

A Rama web service is a regular Service in the same stack as the transport and TLS layers below it:

  • Routing: Router, WebService and the match_service! macro map requests to endpoints
  • Extraction: endpoint functions take typed extractors (path, query, headers, body, …) and return anything that implements IntoResponse
  • Middleware: the same Layers are used for web services, clients and proxies
  • Transport: you choose and configure the listener (TCP, Unix, TLS, HTTP versions) yourself, and can reach connection-level data from within a handler
  • Proxying: web endpoints and proxy services can be served from the same stack

Use Cases

Rama is particularly well-suited for:

  • Building APIs that need fine-grained control over the network stack
  • Services that require both web and proxy capabilities
  • Applications where performance and control are critical
  • Systems that need to integrate with custom protocols or transport layers
  • Services that require deep integration with the operating system

Common examples include:

  • Kubernetes health services
  • Metric exposure endpoints
  • Device management APIs
  • Control panels and admin interfaces
  • Custom protocol servers
  • High-performance API gateways

Comparison with Axum

Axum is a web framework that shares many similarities with Rama. Both run on Tokio and can be used to build web services. The key differences are:

  • Philosophy: Axum focuses on providing high-level abstractions for common web patterns, while Rama emphasizes control and flexibility
  • Scope: Axum is specifically designed for web services, while Rama is a broader service framework that includes web capabilities
  • Control: Rama gives you more direct access to the network stack and transport layers
  • Integration: Rama makes it easier to combine web services with proxy functionality

Type-safe HTML templating

For HTML responses, Rama ships a small templating library exposed under rama::http::protocols::html behind the html feature (included in http-full). It is a permanent fork of vy, reshaped to integrate with the rest of the rama ecosystem (using rama::combinators::Either for branching, and producing a value that already implements IntoResponse).

Each HTML5 element gets its own proc-macro (html!, body!, div!, …). Inside the macro body, leading name = value / name? = value pairs are attributes; everything after them is rendered as children. String content spliced from variables is automatically HTML-escaped; only values wrapped in PreEscaped(...) are written verbatim. The html! macro is special: it always prepends <!DOCTYPE html> so that its output is a complete page.

Element macros nest as arguments of each other, and the result can be returned from a handler as impl IntoResponse. See html! and PreEscaped in the API docs.

For web components and any other custom-named element, use the runtime-tag-name custom! macro, which takes the tag name as its first argument, followed by the usual attributes and children.

Implementing the IntoHtml trait on your own type is the natural escape hatch for type-safe components — your type’s into_html can return any composition of element macros, and the type can then be used wherever IntoHtml is accepted.

For runnable examples see:

Datastar

Datastar helps you build reactive web applications with the simplicity of server-side rendering and the power of a full-stack SPA framework.

— https://data-star.dev/

Rama has built-in support for 🚀 Datastar. You can see it in action in Examples:

Rama rust docs:

llama cruising through space with rama and datastar

You can join the discord server of 🚀 Datastar at https://discord.gg/sGfFuw9k, after which you can join the #general-rust channel for any datastar specific help.

The core concept of datastar is to keep one long lived connection per user (agent) session open, through which you stream your data(star) events (SSE). While your client interacts with the server via regular HTTP calls. This paradigm is often referred to as Command Query Responsibility Segregation (CQRS). Covering CQRS properly is out of scope of this doc as well as the knowledge of the author. You can however start your journey in that rabbit hole by reading these resources:

Examples

Rama provides a rich set of examples demonstrating its web service capabilities. These range from simple services to complex applications:

Basic Services

Newline Delimited JSON (ndjson)

  • /examples/src/http_nd_json.rs - example demonstrating how one can expose a json stream endpoint (see test of this example to see how client side works)

Server-Sent Events (SSE)

See the SSE chapter.

Anti-Bot Examples

Production-Ready Examples

ACME to get server certificates

The following examples show how you can integrate ACME into you webservices (ACME support in Rama is currently still under heavy development)

More Examples

For a real-world example, check out the rama cli fp source code, which implements a production web service for the Rama fingerprinting service.

This example demonstrates the power of Rama’s match_service macro for creating efficient, box-free service routers.