# Water Http Framework is the most easy and fast web framework that built on both http1.1 and http2 and it`s supporting them automatically [no extra implementations required]
# so lets talk about installing firstly
- you need to add water_http crate,
and we recommend you to use the terminal or CMD in windows
```shell
cargo add water_http
```
or you could use cargo.toml file and add the latest version of water_http
```toml
water_http="[latest_version_number]"
```
- then you need to add tokio
```shell
cargo add tokio --features=full
```
- (optional) if you wants to use json structs just add serde and serde_json crates
```shell
cargo serde serde_json
```
# Now Let`s implement Rust Code
- first implementation
```rust
use std::collections::HashMap;
use water_http::{WaterController, DataHolderInitializer, RunServer, WaterServerConfigurations};
DataHolderInitializer!(HashMap<String,String>);
#[tokio::main]
async fn main() {
let config = WaterServerConfigurations::bind("127.0.0.0",8084);
RunServer!(
config ,
MainController::build()
);
}
WaterController! {
holder -> crate::_WaterContextDataHolder,
name -> MainController,
functions -> {
GET => / => any_thing_you_want_name(context)async{
let _ = context.send_str_data("Hello ,World!",true).await;
}
},
}
```
- second one
```rust
use std::collections::HashMap;
use water_http::{WaterController, DataHolderInitializer, RunServer, WaterServerConfigurations};
DataHolderInitializer!(HashMap<String,String>);
#[tokio::main]
async fn main() {
let config = WaterServerConfigurations::default();
RunServer!(
config,
MainController::build()
);
}
WaterController! {
holder -> crate::_WaterContextDataHolder,
name -> MainController,
functions -> {
GET => / => any_thing_you_want_name(context)async{
let _ = context.send_str_data("Hello ,World!",true).await;
}
},
}
```
- third one
```rust
use std::collections::HashMap;
use water_http::{WaterController, DataHolderInitializer, RunServer};
DataHolderInitializer!(HashMap<String,String>);
#[tokio::main]
async fn main() {
RunServer!(MainController::build());
}
WaterController! {
holder -> crate::_WaterContextDataHolder,
name -> MainController,
functions -> {
GET => / => any_thing_you_want_name(context)async{
let _ = context.send_str_data("Hello ,World!",true).await;
}
},
}
```
- forth
```rust
use std::collections::HashMap;
use water_http::{WaterController, DataHolderInitializer, RunServer,MiddlewareBuilder};
DataHolderInitializer!(HashMap<String,String>);
#[tokio::main]
async fn main() {
RunServer!(MainController::build());
}
WaterController! {
holder -> crate::_WaterContextDataHolder,
name -> MainController,
functions -> {
GET => / => any_thing_you_want_name(context)async{
let _ = context.send_str_data("Hello ,World!",true).await;
}
},
children->vec![
super::SecondController::build(),
super::ThirdOne::build(),
],
}
WaterController!{
extra_code-> {
use water_http::structure::MiddlewareResult;
},
holder -> crate::_WaterContextDataHolder,
name -> SecondController,
functions -> {
GET => test_second => test_second_function(context)async{
let _ = context.send_str_data("Second Response",true).await;
}
},
prefix -> "second controller",
middleware -> super::MiddlewareBuilder!(
context => async {
let authorization = context.get_from_headers_as_string("Authorization");
match authorization {
Some(authorization) => {
if authorization == "some_thing"{
context.data_holder.as_mut()
.unwrap()
.insert(
"user_id".to_string(),
"whatever".to_string()
);
}
MiddlewareResult::Pass
}
None => MiddlewareResult::Stop
}
}
),
}
WaterController!{
extra_code-> {},
holder -> crate::_WaterContextDataHolder,
name -> ThirdOne,
functions -> {
GET => test_second => any_thing_you_want_name(context)async{
let _ = context.send_str_data("Third Response",true).await;
}
},
prefix->"third",
}
```
and there is a lot of features like downloading files
and setting public directory and streaming videos
and sending many files as response and sending customs responses
and also setting custom ip addresses that would be only them who can connect the server
or restricting ip addresses from connecting the server
and also there is cli application and very easy frontend
building tools are coming soon
```rust
use water_http::WaterServerConfigurations;
let mut config = WaterServerConfigurations::default();
config.public_files_path = String::from("./public");
config.restricted_ips = Some(WaterIpAddressesRestriction::OnlyAllowedIps(vec![
"127.0.0.1".to_string()
]));
config.set_tls_certificate(
"./ssl/certificate.crt",
"./ssl/private.key",
None,
);
// for prevent downloading public files from public directory
config.do_not_even_check_public_resources = true;
```