Expand description
spa-rs is a library who can embed all SPA web application files (dist static file), and release as a single binary executable.
It based-on axum and rust_embed
It reexported all axum module for convenient use.
§Example
use spa_rs::spa_server_root;
use spa_rs::SpaServer;
use spa_rs::routing::{get, Router};
use anyhow::Result;
spa_server_root!("web/dist"); // specific your SPA dist file location
#[tokio::main]
async fn main() -> Result<()> {
let data = String::new(); // server context can be acccess by [axum::Extension]
let mut srv = SpaServer::new()?
.port(3000)
.data(data)
.static_path("/png", "web") // static file generated in runtime
.route("/api", Router::new()
.route("/get", get(|| async { "get works" })
)
);
srv.run(spa_server_root!()).await?;
Ok(())
}
§Session
See session module for more detail.
§Dev
When writing SPA application, you may want use hot-reload functionallity provided
by SPA framework. such as [vite dev
] or [ng serve
].
You can use spa-rs to reverse proxy all static requests to SPA framework. (need enable reverse-proxy
feature)
§Example
ⓘ
let forward_addr = "http://localhost:1234";
srv.reverse_proxy(forward_addr.parse()?);
Re-exports§
pub use rust_embed;
Modules§
- auth
- body
- HTTP body utilities.
- error_
handling - Error handling model and utilities
- extract
- Types and traits for extracting data from requests.
- filter
- Conditionally dispatch requests to the inner service based on the result of a predicate.
- handler
- Async functions that can be used to handle requests.
- http
- A general purpose library of common HTTP types
- middleware
- Utilities for writing middleware
- response
- Types and traits for generating responses.
- routing
- Routing between
Service
s and handlers. - serve
- Serve services.
- session
- A tower middleware who can reading and writing session data from Cookie.
Macros§
- embed_
https_ pems - setup https pems
- http_
bail - Return early with an
HttpError
- http_
err - Construct an ad-hoc error from a string or existing error value.
- spa_
server_ root - Specific SPA dist file root path in compile time
Structs§
- Error
- Errors that can happen when using axum.
- Extension
- Extractor and response for extensions.
- Form
- URL encoded extractor and response.
- Http
Error - The error type contains a status code and a string message.
- Https
Config - Json
- JSON Extractor / Response.
- Router
- The router type for composing handlers and services.
- SpaServer
- A server wrapped axum server.
Traits§
- Http
Context - Easily convert std::result::Result to HttpResult
- Request
Ext - Extension trait that adds additional methods to
Request
. - Request
Parts Ext - Extension trait that adds additional methods to
Parts
. - Service
Ext - Extension trait that adds additional methods to any
Service
. - SpaStatic
- Used to release static file into temp dir in runtime.
Functions§
- serve
- Serve the service with the supplied listener.
Type Aliases§
- BoxError
- Alias for a type-erased error type.
- Http
Result - convenient return type when writing axum handler.
Attribute Macros§
- async_
trait - debug_
handler - Generates better error messages when applied to handler functions.
- debug_
middleware - Generates better error messages when applied to middleware functions.