liveBook · Manning

Server

As explained in Rust Servers, Services, and Apps

A web server is software that accepts network requests and returns responses, typically over HTTP.

An HTTP server accepts requests and returns HTTP responses. A small Rust server makes the request path visible: it listens for TCP byte streams, converts them into HTTP requests, routes each request to a handler, and sends the handler’s response back as bytes. This division keeps listening, message conversion, routing, and response construction separate. section 2.3

A from-scratch implementation can be assembled in this order:

  • Build an HTTP library with HttpRequest and HttpResponse types to convert between messages and Rust data structures. section 2.3
  • Add the main function and server module to receive TCP streams; add a router to select handlers and handlers to construct responses. section 2.3
  • Handle GET requests for static pages and JSON data. This demonstrates both static web serving and a limited web-service capability, not a production-ready HTTP server. section 2.0 section 2.3.5

With Actix, the framework supplies the HTTP server. Configure a path and method with a handler, register that route in an App, then construct and run HttpServer. An Actix server uses worker threads and creates an application instance for each OS thread; the app groups the routes it can handle. section 3.1.1 section 3.1.2

Listing 3.1 Writing a basic Actix web server
// Module imports
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use std::io;
 
// Configure route                                               #1
pub fn general_routes(cfg: &mut web::ServiceConfig) {
    cfg.route("/health", web::get().to(health_check_handler));
}
 
//Configure handler                                              #2
pub async fn health_check_handler() -> impl Responder {
    HttpResponse::Ok().json("Hello. EzyTutors is alive and kicking")
}
 
// Instantiate and run the HTTP server
#[actix_rt::main]
async fn main() -> io::Result<()> {
    // Construct app and configure routes                        #3
    let app = move || App::new().configure(general_routes);
 
    // Start HTTP server                                         #4
    HttpServer::new(app).bind("127.0.0.1:3000")?.run().await
}

The runnable Actix example places the four steps together in src/bin/basic-server.rs: /health routes GET requests to an async handler, and HttpServer binds to 127.0.0.1:3000. section 3.1.1

Check the response, not just the page

Run the from-scratch server and request /, /health, /api/shipping/orders, and /invalid-path with a browser or curl. The index page causes the browser to fetch styles.css separately. Browser developer tools show text/html for HTML, text/css for CSS, and application/json for the orders response. section 2.3.5

A server’s role can change within an application. For a sign-in form, a frontend Actix server receives a POST on the sign-in route; its handler checks credentials in a local database, then redisplays the form with an error or directs the authenticated user to the home page. That frontend server is distinct from the backend web service. section 9.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Builds a limited GET server from TCP and HTTP components, then configures an Actix server; stops short of a production-ready implementation.
Examples
Tests HTML, CSS, JSON, and invalid paths with a browser or curl, and builds an Actix /health endpoint.
What it assumes
Assumes enough Rust and Cargo familiarity to work in a project while teaching TCP and HTTP server fundamentals through implementation.
What you can do after
Build it from scratch with Rust’s standard library or operate/configure it with Actix routes, handlers, and HttpServer.
How it connects
Connects TCP streams and HTTP messages to routing, handlers, web applications, and REST APIs.

Also explained in

Compare how 11 books explain server →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.