Cataclysm. A simple rust http server
Work in progress: The work is completely unusable in its current state, but updates will come soon until a full HTTP/1.1 server is functional. This legend will be removed when such a state is reached.
Cataclysm is an http framework influenced by actix-web, and built over tokio. A minimal working example is the following
use ;
async
async
Closures as callbacks
Until async closures
become stable, the option to pass closures as a path handler is with a closure that returns an async block
use ;
async
Extractors
Some data can be retrieved from an http request by just adding arguments to the callback, with types that implement the Extractor
trait. The default implementation list is the following
String
: Tries to extract the body as a valid utf-8 string. Returns Bad-Request if the operation failsVec<u8>
: Returns the content of thehttp
call as a stream of bytesRequest
: Returns the request for a bit more control within the callbackPath<T>
: Returns the parameters from the path. T must be a tuple.Shared<T>
: Returns the shared data provided to the server (if any).
Sharing data to the functions from the server
Data can be shared accross the server calls through the share
method from the ServerBuilder
structure, and with the help of the Shared
structure.
use ;
// Receives a string, and concatenates the shared suffix
async
async
If you want to share mutable data, then use rust's Mutex
structure (as the Shared
structure already provides an Arc
wrapper).
SPA, and static file serving
The branch structure has both the file
method and the defaults_to_file
to create a simple SPA server, one allows all paths that contain an extention to be looked for in the provided folder path, and the other to serve a file as specified by the provided path in any other match that has no extension, respectively.
use ;
async
async
Layers
Cataclysm allows for layer handling, a.k.a. middleware.
use ;
use Arc;
use FutureExt;
async
As seen in the example, layer functions receive a Request
and a boxed Pipeline
enum. The Pipeline
enum contains a nested structure of futures (the layers + the core handler), and has the execute
to simplify things a bit. This function must return a Pin<Box<_>>
future, so either use the boxed
method from the FutureExt
trait from the futures
crate, or wrap it manually.
Full log feature
If the full_log
feature is activated, log function through the levels Debug
and Trace
will be provided. This might come in handy for debugging, but it is left optional as it might have a strong toll in performance.
TODO
- Regex with / cause problems in branch creation (fix with queue implementation for "{", "}" detection)