use std::str::FromStr;
use francoisgib_webserver::{
config::Config,
http::{
headers::HeaderEntry, methods::HttpMethod, requests::HttpRequest, responses::HttpResponse,
status::HttpStatus,
},
server::HttpServer,
tree::{EndpointType, HttpTree},
utils::buffer::Buffer,
};
fn hello_world_handler(_: &HttpRequest, response: &mut HttpResponse) -> Result<(), String> {
let content_type_header = HeaderEntry::from_str("Content-Type: text/plain").unwrap();
let body = Buffer::from_str("Hello, World!").ok();
let content_length_header = HeaderEntry::from_str("Content-Length: 13").unwrap();
response.status = HttpStatus::Ok;
response.headers.push(content_type_header);
response.headers.push(content_length_header);
response.body = body;
Ok(())
}
fn main() {
let content = std::fs::read_to_string("config.toml").unwrap();
let config: Config = toml::from_str(&content).unwrap();
let mut tree: HttpTree = HttpTree::new();
tree.add_endpoint(
"/static",
HttpMethod::GET,
EndpointType::Directory("./examples/data/static".to_owned()),
);
tree.add_endpoint(
"/json",
HttpMethod::GET,
EndpointType::File("./examples/data/file.json".to_owned()),
);
tree.add_endpoint(
"/hello",
HttpMethod::GET,
EndpointType::Handler(hello_world_handler),
);
tree.add_endpoint(
"/image",
HttpMethod::GET,
EndpointType::File("./examples/data/grotichon.png".to_owned()),
);
let server = HttpServer::new(tree, config);
server.start();
}