micro_http_async
What is it for?
A small, lightweight crate using async to serve web pages or web apis with high performance and low overhead.
How do I use it?
Firstly, install the crate and dependencies:
[dependencies]
micro_http_async = "*"
tokio = "1.11.0"
And if you want to support JSON:
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"]}
This crate is designed to abstract away many of the low level code required to run a safe, asynchronous web server
Here is a small example which shows how to route, use asynchronous call backs and load webpage templates from HTML files.
For the HTML files included, please go to the repository and check the templates
folder.
Static files also included.
To run the included example (which is the example seen below), run cargo run --example hello_world
, and visit 127.0.0.1:8080
Please note this is probably not the final API
Example
/// Small example to show the functionings of the crate. Read the comments to see how everything
/// functions
// All the imports we need
use ;
use Request;
use HtmlConstructor;
use Vars;
use Variable;
use Response;
use Route;
// Macros
use create_route;
/// # main handler
///
/// main handler is a test to test our route and function callbacks work
///
/// And it does!
///
/// The way it works is that we run test_handler when we recieve a connection.
///
/// Then, this handler manipulates the request (for post info, or other info etc)
///
/// after, we return the response as a string. It is then served to the user.
async
/// We have to define a custom error handler, which defines what to do when we have a 404
///
/// Not doing this WILL result in an unrecoverable panic.
async
// If we choose to use JSON (eg, for APIs), we can use the following.
// We define the JSON as a rust struct, allowing us to represent it through rust. We serialize this
// using serde.
// Then, when we handle the response, we convert the Struct using serde_json. We use the JSONResponse class to create
// a response we can send back to the user, using that weird looking return_future method and the box::pins lol.
async
/// # main
///
/// Does what it says, just sets up the server and routes
///
/// then listens for incoming connections
pub async
This crate aims only to simplify web api or lightweight web creation - not intended to run full scale web apps like chatrooms
or other high intensity applications. It implements a simple asynchronous routing system (Made using hash maps for speed and efficiency)
as well as asynchronous file loading and more.
The demo above uses 0% CPU under no load, and less than 10mb of memory under usage.
It compiles in 1m 34s on an i5 5500u (release) from scratch and sits at 700kb.
Changelog v0.1.2:
Added rudementary post requests. There are some explainations of usage in the example hello_world
.