#[server] macro transforms async functions into server functions that can be called from the client. It generates client-side stubs that make HTTP requests and server-side handlers that execute the function.
Basic Usage
Macro Arguments
The#[server] macro accepts optional named arguments:
endpoint
Customize the URL path where the function is mounted (defaults to /api):
input
Specify the encoding for request arguments (defaults to Json<T>):
Json<T>- JSON-encoded request bodies (default)Cbor<T>- CBOR binary encodingMessagePack<T>- MessagePack binary encoding- Any axum
FromRequestextractor
output
Specify the encoding for responses (defaults to Json):
Middleware
Add towerLayer middleware to server functions:
tower_http::trace::TraceLayer- Request/response tracingtower_http::compression::CompressionLayer- Response compressiontower_http::cors::CorsLayer- CORS headerstower_http::timeout::TimeoutLayer- Request timeoutstower_sessions::SessionManagerLayer- Session management
Return Types
Server functions must returnResult<T, E> where:
Success Type T
Can be either:
- Serializable: Any type implementing
Serialize + DeserializeOwned - Response type: Types implementing
FromResponseandIntoResponse(likeWebsocket,FileStream, etc.)
Error Type E
Can be:
- Custom error: Any type implementing
From<ServerFnError> + Serialize + DeserializeOwned anyhow::Error: General error handlingStatusCode: HTTP status codesHttpError: HTTP errors with status and message
Argument Types
Server functions accept:Multiple Serializable Arguments
All arguments must implementSerialize + DeserializeOwned:
Single Extractor Argument
One argument implementing bothFromRequest + IntoRequest:
Server-Side Context
Access request context in server functions using extractors:Code Generation
The macro generates: Client-side:- Serializes arguments to JSON (or specified format)
- Makes HTTP POST request to endpoint
- Deserializes response
- Returns
Result<T, E>
- Registers handler at endpoint path
- Deserializes request body
- Calls original function
- Serializes result to response
Related Types
ServerFnResult<T>- Convenient alias forResult<T, ServerFnError>ServerFnError- Error type for server functionsFullstackContext- Server-side request context
Type Reference
ServerFnResult<T>
packages/fullstack-core/src/error.rs:22
ServerFnError
packages/fullstack-core/src/error.rs:27
Variants
-
ServerError- Error running the function on the servermessage: Human-readable error descriptioncode: HTTP status codedetails: Optional structured error data
-
Request- Network error reaching the server (client-side) -
StreamError- Error reading response body stream -
Registration- Error registering server function -
UnsupportedRequestMethod- Invalid HTTP method -
MiddlewareError- Middleware processing error -
Deserialization- Error deserializing server response -
Serialization- Error serializing arguments -
Args- Error deserializing server-side arguments -
MissingArg- Required argument not provided -
Response- Error creating HTTP response
Methods
packages/fullstack-core/src/error.rs:86-117