more examples

This commit is contained in:
MeexReay 2024-06-18 19:50:02 +03:00
parent 05f1b5fe5e
commit efc36aa3f8
4 changed files with 85 additions and 8 deletions

View File

@ -17,9 +17,9 @@ impl HttpServer for EzSite {
if req.page == "/" {
Some(HttpResponse::from_str(
Headers::from(vec![("Content-Type", "text/html")]),
"200 OK".to_string(),
&self.index_page,
Headers::from(vec![("Content-Type", "text/html")]), // response headers
"200 OK".to_string(), // response status code
&self.index_page, // response body
))
} else {
None // just shutdown socket
@ -49,5 +49,6 @@ fn main() {
ezhttp::start_server(site, host).unwrap();
}
```
[More examples](https://github.com/MeexReay/ezhttp/blob/main/examples)

70
examples/simple_site.rs Normal file
View File

@ -0,0 +1,70 @@
use ezhttp::{Headers, HttpRequest, HttpResponse, HttpServer};
struct EzSite {
main_page: String,
}
impl EzSite {
fn new(index_page: &str) -> Self {
EzSite {
main_page: index_page.to_string(),
}
}
fn ok_response(&mut self, content: String) -> HttpResponse {
HttpResponse::from_string(
Headers::from(vec![("Content-Type", "text/html")]),
"200 OK".to_string(),
content,
)
}
fn not_found_response(&mut self, content: String) -> HttpResponse {
HttpResponse::from_string(
Headers::from(vec![("Content-Type", "text/html")]),
"404 Not Found".to_string(),
content,
)
}
async fn get_main_page(&mut self, req: &HttpRequest) -> Option<HttpResponse> {
if req.page == "/" {
Some(self.ok_response(self.main_page.clone()))
} else {
None
}
}
async fn get_unknown_page(&mut self, req: &HttpRequest) -> Option<HttpResponse> {
Some(self.not_found_response(format!("<h1>404 Error</h1>Not Found {}", &req.page)))
}
}
impl HttpServer for EzSite {
async fn on_request(&mut self, req: &HttpRequest) -> Option<HttpResponse> {
println!("{} > {} {}", req.addr, req.method, req.page);
if let Some(resp) = self.get_main_page(req).await {
Some(resp)
} else if let Some(resp) = self.get_unknown_page(req).await {
Some(resp)
} else {
None // shutdown socket
}
}
async fn on_start(&mut self, host: &str) {
println!("Http server started on {}", host);
}
async fn on_close(&mut self) {
println!("Http server closed");
}
}
fn main() {
let site = EzSite::new("<h1>Hello World!</h1>");
let host = "localhost:8080";
ezhttp::start_server(site, host).unwrap();
}

View File

@ -497,6 +497,14 @@ impl HttpResponse {
}
}
pub fn from_string(headers: Headers, status_code: String, data: String) -> Self {
HttpResponse {
headers: headers,
data: data.into_bytes(),
status_code: status_code,
}
}
pub fn get_text(self) -> String {
match String::from_utf8(self.data) {
Ok(i) => i,

View File

@ -15,7 +15,7 @@ impl HttpServer for EzSite {
&self.index_page, // response body
))
} else {
None // shutdown request
None // just shutdown socket
}
}
@ -37,10 +37,8 @@ impl EzSite {
}
fn main() {
let site = EzSite::new(&"Hello World!".repeat(99999));
let site = EzSite::new("Hello World!");
let host = "localhost:8080";
ezhttp::start_server(site, host).unwrap();
// ezhttp::start_server_timeout(site, host, Duration::from_secs(5)).unwrap(); // with read and write timeout
}