Repository navigation
HTTP Servers
std::net::http::server turns accepted HTTP requests into values. A server owns
its listening socket; each Incoming request owns its request/response lifecycle.
Defining this function does not start a service. A caller can invoke it with an explicit port when ready to listen.
import std::net::http::server
fn serve_status($port: Integer): () | ErrorValue
{
let $server = std::net::http::server::listen "127.0.0.1" $port
try
{
$server.serve fn ($request: std::net::http::server::Incoming): () | ErrorValue
{
$request.respond (std::net::http::server::text_response "available")
} 1
}
finally { $server.close }
}
The second argument to serve bounds the number of handled requests here.
accept offers manual control instead. address reports the selected endpoint,
including an operating-system-assigned port when listening on zero.
Incoming requests expose method, path, and headers. Read a request body once
through a byte Reader, an exact document, or read_as Type. Respond with a
Response, or helpers such as text_response and json_response.
Close an incoming operation if abandoning it before completion. Keep handler errors explicit: a transport failure and a deliberately chosen HTTP error status are different decisions.
Shelly's server and client share Bytes and I/O ownership rules. For a reusable service, define request limits, error policy, and shutdown behavior alongside the handler rather than hiding those choices in example defaults.
Related: HTTP clients · Networking · JSON
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use