A safe and performance web server framework that is written by Rust.
This is the beginning of the aim to write a safe web server framework by Rust. For now, this repository has not provided a complete and stable function, as xfinal has done, written by modern c++. The aim is to build up a web server framework, which has the same functionalities as xfinal
has.
Since the advantages of Rust are that it is safe on memory and multiple threads, and it is a modern programming language that has almost no pieces baggage c++ has, the framework that is written based on Rust has no worry about the hard problems with memory and data race. Rust can guarantee the safety of these aspects.
Moreover, Rust has the characteristics of zero-cost abstraction, as c++ has, hence the performance of the framework will be desired as you expect, rapid!
Rust has a very convenient package manager: Cargo, which can make you feel free to use any third crate without being necessary to use CMake to manage these dependencies or even to write obscure rules that are ugly.
- HTTP "hello, world"
rust use http_server::{ end_point, EndPoint, HttpServer, Request, Response, GET, }; fn main(){ let mut http_server = HttpServer::create(end_point!(0.0.0.0:8080), 10); http_server.route(GET, "/").reg(|req: &Request, res: &mut Response| { res.write_string(String::from("hello, world"), 200); }); http_server.run(); }
- Use middlewares ````rust use httpserver::{ endpoint, injectmiddlewares, EndPoint, HttpServer, MiddleWare, Request, Response, GET, }; fn main(){ let mut httpserver = HttpServer::create(endpoint!(0.0.0.0:8080), 10); fn interruptsecond(req:& Request,res:&mut Response) ->bool{ println!("invoke middleware2"); match req.getparam("id") { Some(v) => { if v == "1"{ true }else{ res.writestring("invalid request, invalid id value", 400); false } }, None => { res.writestring("invalid request, no id", 400); false }, } } let middlewares = injectmiddlewares! { |req:& Request,res:&mut Response|->bool{ println!("invoke middleware"); true },interrupt_second //, middleware 2,..., so forth };
httpserver.route(GET, "/middle").regwithmiddlewares( middlewares, |req: &Request, res: &mut Response| { println!("invoke router"); res.writestring(String::from("hello from router"), 200); }, ); http_server.run(); } ````
- Query information from Request
rust http_server.route(GET, "/query").reg(|req: &Request, res: &mut Response| { let r:Option<&str> = req.get_query("id"); let host = req.get_header("Host"); let file = req.get_file("file"); let version = req.get_version(); let method = req.get_method(); let headers = req.get_headers(); let forms = req.get_queries(); let get_all_files = req.get_files(); let url = req.get_url(); let id = req.get_param("id"); // /a?id=0 res.write_string("ok", 200); });
- Chunked transfer and/or Rangeable
rust http_server.route([GET,HEAD], "/query").reg(|_req: &Request, res: &mut Response| { // file: res.write_file("./upload/test.mp4",200).chunked().enable_range(); //string: res.write_string("abcdefg",200).chunked(); // res.write_string("abcdefg",200).enable_range(); // file: res.write_file("./upload/test.mp4",200).enable_range().chunked(); // No sequence requirement, whatever combination as you go. });
- Wildcard path
rust http_server.route(GET, "/wildcard/*").reg(|_req: &Request, res: &mut Response|{ let s = format!("hello from {}", req.get_url()); res.write_string(&s, 200); });