Releases: co-rs/mco-http
Releases · co-rs/mco-http
v0.1.16
v0.1.15
v0.1.13
v0.1.13
- add method read_json
for example
use cogo_http::json::read_json;
use cogo_http::server::{Request, Response};
fn hello(mut req: Request, res: Response) {
let json_data: serde_json::Value = read_json(&mut req).unwrap();
println!("req:{:?}",json_data);
res.send(json_data.to_string().as_bytes()).unwrap();
}
v0.1.12
v0.1.10
v0.1.9
v0.1.8
v0.1.7
v0.1.6
v0.1.6
- add route mod
use cogo_http::query::read_query;
use cogo_http::route::Route;
use cogo_http::server::{Request, Response};
use cogo_http::route::MiddleWare;
fn js(req: Request, res: Response) {
res.send("{\"name\":\"joe\"}".as_bytes()).unwrap();
}
fn hello(req: Request, res: Response) {
let param = read_query(&req.uri.to_string());
res.send(format!("{:?}", param).as_bytes()).unwrap();
}
// MiddleWare
#[derive(Debug)]
pub struct MyMiddleWare {}
impl MiddleWare for MyMiddleWare {
fn handle(&self, req: &mut Request, res: &mut Response) -> bool {
println!("hello MiddleWare!");
//return true is done
return false;
}
}
fn main() {
env_logger::init().unwrap();
let mut route = Route::new();
route.add_middleware(MyMiddleWare {});
route.handle_fn("/", hello);
route.handle_fn("/js", js);
route.handle_fn("/fn", |req: Request, res: Response| {
res.send(format!("fn").as_bytes());
});
let _listening = cogo_http::Server::http("0.0.0.0:3000").unwrap()
.handle(route);
println!("Listening on http://127.0.0.1:3000");
println!("please click http://127.0.0.1:3000/?q=query_info&b=123");
println!("please click http://127.0.0.1:3000/js");
println!("please click http://127.0.0.1:3000/fn");
}