# Craweb
<p>
<img src="https://img.shields.io/crates/d/craweb?style=for-the-badge" alt="Total downloads count" />
<img src="https://img.shields.io/crates/v/craweb?style=for-the-badge" alt="Latest crate version" />
</p>
Multithreaded asynchronous web server, written in Rust. And it's really fast (we are handling one request in less than 1 second)!
### Installation
You can install this crate using [crates.io](https://crates.io/crates/craweb).
```toml
[dependencies]
craweb = "*" # Or you can replace version with specific ones.
```
### Writing basic server
In order to start the server, you must do the following:
1. Initialize the server in your `main.rs` file.
2. Add at least one route.
3. Bind the server to the specific IP address and port.
Here's an example (as well as in the [example_server](https://github.com/Pelfox/craweb/tree/main/example_server) in the root repository):
```rust
use std::sync::Arc;
use std::collections::HashMap;
use craweb::{
models::{Request, Response, ServerRoute, RequestMethod},
server::Server,
};
// handler for our `/` path
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);
// registering a new route
match server.on(
String::from("/"), // it will respond to us in `/` path.
ServerRoute {
method: RequestMethod::GET, // it will respond only on `GET` request
handler: handle_home_route, // and our function will handle it
},
) {
Ok(()) => {}
Err(err) => {
eprintln!("Unable to register a server route: {}", err);
}
}
Arc::new(server).bind("127.0.0.1:3000").await; // binding the server onto specific IP address and port
}
```
### License
This crate is licensed under the MIT License. You can read the full license text [here](https://github.com/Pelfox/craweb/blob/main/craweb/LICENSE).