use afire::{Content, HeaderType, Method, Response, Server, Status};
use crate::Example;
pub struct Header;
impl Example for Header {
fn name(&self) -> &'static str {
"header"
}
fn exec(&self) {
let mut server = Server::<()>::new("localhost", 8080)
.default_header("X-Server-Header", "This is a server wide header")
.default_header(HeaderType::Server, "afire");
server.route(Method::GET, "/", |_req| {
let headers = vec![
afire::Header::new(HeaderType::ContentType, "text/html"),
afire::Header::new(HeaderType::Location, "https://connorcode.com"),
afire::Header::new("X-Custom-Header", "This is a custom header"),
];
let text = "<a href=\"https://connorcode.com\">connorcode</a>";
Response::new()
.status(Status::PermanentRedirect)
.text(text)
.headers(&headers)
});
server.route(Method::GET, "/headers", |req| {
let body = req
.headers
.iter()
.fold(String::new(), |old, new| old + &format!("{:?}<br />", new));
Response::new().text(body).content(Content::HTML)
});
server.start().unwrap();
}
}