Craweb
Multithreaded asynchronous web server, written in Rust.
Installation
You can install this crate using crates.io.
[dependencies]
craweb = "*"
Writing basic server
In order to start the server, you must do the following:
- Initialize the server in your
main.rs file.
- Add at least one route.
- Bind the server to the specific IP address and port.
Here's an example (as well as in the example_server in the root repository):
use std::sync::Arc;
use std::collections::HashMap;
use craweb::{
models::{Request, Response, ServerRoute, RequestMethod},
server::Server,
};
fn handle_home_route(_: Request) -> Response {
let mut headers = HashMap::new();
headers.insert(String::from("Content-Type"), String::from("text/html"));
return Response {
content: Some(String::from("<html><h1>Hello, world!</h1></html>")),
status_message: String::from("OK"),
status_code: 200,
headers,
};
}
#[tokio::main]
async fn main() {
let mut server = Server::new(None, None, None, None);
match server.on(
String::from("/"), ServerRoute {
method: RequestMethod::GET, handler: handle_home_route, },
) {
Ok(()) => {}
Err(err) => {
eprintln!("Unable to register a server route: {}", err);
}
}
Arc::new(server).bind("127.0.0.1:3000").await; }
License
This crate is licensed under the MIT License. You can read the full license text here.