]> OzVa Git service - ozva-cloud/commitdiff
feat: add favicon (#27)
authorsigoden <sigoden@gmail.com>
Tue, 7 Jun 2022 00:59:44 +0000 (08:59 +0800)
committerGitHub <noreply@github.com>
Tue, 7 Jun 2022 00:59:44 +0000 (08:59 +0800)
Return favicon only if requested, avoid 404 errors

close #16

assets/favicon.ico [new file with mode: 0755]
src/server.rs

diff --git a/assets/favicon.ico b/assets/favicon.ico
new file mode 100755 (executable)
index 0000000..57d3531
Binary files /dev/null and b/assets/favicon.ico differ
index cda0804dcdec5544cfd77913a10ec3c99023a826..bf89df0bc0a3aaa29cf7f2d52778e1d77a501a8c 100644 (file)
@@ -44,6 +44,7 @@ type Response = hyper::Response<Body>;
 const INDEX_HTML: &str = include_str!("../assets/index.html");
 const INDEX_CSS: &str = include_str!("../assets/index.css");
 const INDEX_JS: &str = include_str!("../assets/index.js");
+const FAVICON_ICO: &[u8] = include_bytes!("../assets/favicon.ico");
 const INDEX_NAME: &str = "index.html";
 const BUF_SIZE: usize = 1024 * 16;
 
@@ -174,6 +175,12 @@ impl InnerService {
             status!(res, StatusCode::NOT_FOUND);
             return Ok(res);
         }
+        if is_miss && path.ends_with("favicon.ico") {
+            *res.body_mut() = Body::from(FAVICON_ICO);
+            res.headers_mut()
+                .insert("content-type", "image/x-icon".parse().unwrap());
+            return Ok(res);
+        }
 
         let headers = req.headers();