water_http 1.3.1

fast web http framework that support http 1 and http 2 with very easy use
Documentation
water_http-1.3.1 has been yanked.

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
 cargo add water_http

or you could use cargo.toml file and add the latest version of water_http

water_http="[latest_version_number]"

  • then you need to add tokio
cargo add tokio --features=full
  • (optional) if you wants to use json structs just add serde and serde_json crates
cargo serde serde_json

Now Let`s implement Rust Code

  • first implementation
use std::collections::HashMap;

use water_http::{WaterController, DataHolderInitializer, RunServer, WaterServerConfigurations};
/// notice that this data holder could be used to parse data from middleware to another or 
/// from controller to controller , so you could choose what ever you want from data type ,
/// or you could make it u8 to have single byte if you don`t use it
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
use std::collections::HashMap;

use water_http::{WaterController, DataHolderInitializer, RunServer, WaterServerConfigurations};
/// notice that this data holder could be used to parse data from middleware to another or 
/// from controller to controller , so you could choose what ever you want from data type ,
/// or you could make it u8 to have single byte if you don`t use it
DataHolderInitializer!(HashMap<String,String>);
#[tokio::main]
async fn main() {
    /// it will listen to port 80 
    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
use std::collections::HashMap;

use water_http::{WaterController, DataHolderInitializer, RunServer};
/// notice that this data holder could be used to parse data from middleware to another or 
/// from controller to controller , so you could choose what ever you want from data type ,
/// or you could make it u8 to have single byte if you don`t use it
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
use std::collections::HashMap;

use water_http::{WaterController, DataHolderInitializer, RunServer,MiddlewareBuilder};
// notice that this data holder could be used to parse data from middleware to another or
// from controller to controller , so you could choose what ever you want from data type ,
// or you could make it u8 to have single byte if you don`t use it
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(),
    ],
}


// notice that when you set new water controller you are creating new mod ,  so you need to either use extra_code
// to add your wanted code or use [super::*] or [crate::*]
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",

    // setting middleware algorithm
    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
            }
        }
    ),
}


// notice that when you set new water controller you are creating new mod ,  so you need to either use extra_code
// to add your wanted code or use [super::*] or [crate::*]
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

 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;