Skip to content

Commit

Permalink
add favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni committed Jun 19, 2024
1 parent fca8e23 commit f50e4dc
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 126 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.DS_Store
122 changes: 1 addition & 121 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusty-live-server"
version = "0.4.0"
version = "0.4.1"
edition = "2021"

[dependencies]
Expand All @@ -9,10 +9,9 @@ sha1 = { version = "0.11.0-pre.3", default-features = false }
tokio = { version = "1.38.0", default-features = false, features = ["sync", "rt", "net", "io-util", "fs", "time", "macros", "rt-multi-thread"] }
notify = { version = "6.1.1", default-features = false, features = ["macos_kqueue"], optional = true }
blake3 = { version = "1.5.1", optional = true }
clap = { version = "4.5.7", features = ["derive"], optional = true }
log = { version ="0.4.21", optional = true }

[features]
default = ["filesystem-events", "log"]
default = ["filesystem-events"]
filesystem-events = ["dep:notify", "dep:blake3"]
log = ["dep:log"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Rusty Live Server

Minimal local async development server with live reload feature for static & dynamic pages

```sh
rusty-live-server ./html --port 8080
rusty-live-server ./html -p 8080
Expand Down
Binary file added favicon.ico
Binary file not shown.
18 changes: 18 additions & 0 deletions ico.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 19 additions & 2 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use tokio::{
fs::{read_dir, File},
fs::{self, read_dir, File},
io::{AsyncReadExt as _, AsyncWriteExt as _},
net::TcpStream,
};
Expand Down Expand Up @@ -37,6 +37,8 @@ pub async fn handle_client(mut stream: TcpStream, base_dir: PathBuf, signal: Arc
}
if let Some(key) = websocket {
let _ = handle_websocket(stream, key, signal).await;
} else if path == "/favicon.ico" {
serve_favicon(&file_path, &mut stream).await;
} else if file_path.is_dir() {
if serve_directory(&file_path, &mut stream).await.is_err() {
serve_500(&mut stream).await;
Expand All @@ -60,6 +62,9 @@ async fn serve_directory(dir: &Path, stream: &mut TcpStream) -> io::Result<()> {
let mut entries = read_dir(dir).await?;
while let Ok(Some(entry)) = entries.next_entry().await {
let file_name = entry.file_name().into_string().unwrap_or_default();
if file_name == "index.html" {
return serve_file(&dir.join("index.html"), stream).await;
}
response.push_str(&format!(
"<li><a href=\"{}\">{}</a></li>",
file_name, file_name
Expand Down Expand Up @@ -104,5 +109,17 @@ async fn serve_404(stream: &mut TcpStream) {

async fn serve_500(stream: &mut TcpStream) {
let response = "HTTP/1.1 500 INTERNAL SERVER ERROR\r\n\r\n";
let _ = stream.write_all(response.as_bytes()).await;
let _ = stream.write(response.as_bytes()).await;
}

async fn serve_favicon(path: &Path, stream: &mut TcpStream) {
let bytes = fs::read(path)
.await
.unwrap_or(include_bytes!("../favicon.ico").to_vec());
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Type: image/x-icon\r\nContent-Length: {}\r\n\r\n",
bytes.len()
);
let _ = stream.write(response.as_bytes()).await;
let _ = stream.write(&bytes).await;
}

0 comments on commit f50e4dc

Please sign in to comment.