Skip to content

Commit

Permalink
Default index page and 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluemangoo authored Nov 30, 2024
1 parent 0813be2 commit fac9a18
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ location = ["/"] # optional, see the documents.

[6199.source.static]
source_type="static"
root="/www/public/" # static file root. Relative path will be based on this file.
root="../html" # static file root. Relative path will be based on this file.
#sni = "dev.bluemangoo.net"
#headers_request = { }
#headers_response = { }
Expand Down
30 changes: 30 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Pingpong!</title>
<style>
html {
color-scheme: light dark;
}
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to Pingpong!</h1>
<p>
If you see this page, the Pingpong server is successfully installed.
</p>

<p>
See the documentation on
<a href="https://pingpong.bluemangoo.net/">pingpong.bluemangoo.net</a>
for further configuration.<br />
</p>

<p><em>Thank you for using Pingpong.</em></p>
</body>
</html>
27 changes: 13 additions & 14 deletions src/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::{Proxy, Source};
use crate::util::file_err::PAGE404;
use crate::util::mime::get_mime_type;
use crate::util::path;
use crate::util::route::*;
Expand Down Expand Up @@ -117,9 +118,8 @@ impl ProxyHttp for Gateway {
let (source, uri) = {
if self.check_status {
let mut re: ((&String, &Source), String) =
find_route(&sni, &uri, &self.routes, 0, ctx).map_err(|e| {
find_route(&sni, &uri, &self.routes, 0, ctx).inspect_err(|_| {
error!("[{}]: Failed to find route {}", self.port, &uri_raw);
e
})?;

for _ in 0..10 {
Expand Down Expand Up @@ -205,6 +205,7 @@ impl ProxyHttp for Gateway {
match source.1 {
Source::Proxy(_) => {}
Source::Static(source) => {
let mut status = StatusCode::OK;
let mut file_path = path::resolve_uri(&source.root, uri.as_str());
file_path = file_path.split('?').collect::<Vec<&str>>()[0].to_string();
if file_path.ends_with('/') {
Expand All @@ -213,24 +214,22 @@ impl ProxyHttp for Gateway {
let file = match std::fs::read(&file_path) {
Ok(file) => file,
Err(_) => {
return Err(Error::explain(
HTTPStatus(StatusCode::NOT_FOUND.into()),
"file not found",
))
error!("File not exist: {}", &file_path);
file_path = path::resolve_uri(&source.root, "/404.html");
status = StatusCode::NOT_FOUND;
std::fs::read(&file_path).unwrap_or_else(|_| Vec::from(PAGE404))
}
};

let content_length = file.len();

let mut resp = ResponseHeader::build(StatusCode::OK, Some(4)).unwrap();
resp.insert_header(header::SERVER, "Pingpong").unwrap();
resp.insert_header(header::CONTENT_LENGTH, content_length.to_string())
.unwrap();
resp.insert_header(header::CONTENT_TYPE, get_mime_type(&file_path))
.unwrap();
session.write_response_header(Box::new(resp), false).await.unwrap();
let mut resp = ResponseHeader::build(status, Some(4))?;
resp.insert_header(header::SERVER, "Pingpong")?;
resp.insert_header(header::CONTENT_LENGTH, content_length.to_string())?;
resp.insert_header(header::CONTENT_TYPE, get_mime_type(&file_path))?;
session.write_response_header(Box::new(resp), false).await?;

session.write_response_body(Some(file.into()), true).await.unwrap();
session.write_response_body(Some(file.into()), true).await?;
return Ok(true);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/util/file_err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub static PAGE404: &str = r#"<!DOCTYPE html>
<html>
<head>
<title>404</title>
<style>
html {
color-scheme: light dark;
}
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>404</h1>
<p>
The page you visited does not exist.
</p>
</body>
</html>
"#;
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod path;
pub mod route;
pub mod url;
pub mod mime;
pub mod file_err;

0 comments on commit fac9a18

Please sign in to comment.