Skip to content

Commit

Permalink
add embedded favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
jerbly committed Jan 28, 2024
1 parent 5c58d9e commit 519a898
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 2 deletions.
153 changes: 153 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ futures = "0.3.30"
glob = "0.3.1"
# honeycomb-client = { git = "https://github.com/jerbly/honeycomb-client", tag = "v0.1.0" }
honeycomb-client = { path = "../honeycomb-client" }
mime_guess = "2.0.4"
openssl = { version = "0.10.61", features = ["vendored"] }
rust-embed = { version = "8.2.0", features = ["mime_guess", "axum"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
serde_yaml = "0.9.27"
Expand Down
40 changes: 39 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ use askama::Template;
use askama_axum::IntoResponse;
use axum::{
extract::{Path, State},
http::header::HeaderMap,
http::{
header::{self, HeaderMap},
StatusCode, Uri,
},
response::Response,
routing::get,
Router,
};
use clap::Parser;
use data::Node;
use honeycomb_client::honeycomb::HoneyComb;
use rust_embed;
use rust_embed::RustEmbed;
use semconv::{Attribute, Examples, PrimitiveType, SemanticConventions, Type::Simple};

#[derive(Template)]
Expand Down Expand Up @@ -76,6 +81,38 @@ struct Args {
addr: String,
}

#[derive(RustEmbed)]
#[folder = "static/"]
struct Asset;
pub struct StaticFile<T>(pub T);

impl<T> IntoResponse for StaticFile<T>
where
T: Into<String>,
{
fn into_response(self) -> Response {
let path = self.0.into();

match Asset::get(path.as_str()) {
Some(content) => {
let mime = mime_guess::from_path(path).first_or_octet_stream();
([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response()
}
None => (StatusCode::NOT_FOUND, "404 Not Found").into_response(),
}
}
}

async fn static_handler(uri: Uri) -> impl IntoResponse {
let mut path = uri.path().trim_start_matches('/').to_string();

if path.starts_with("dist/") {
path = path.replace("dist/", "");
}

StaticFile(path)
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// load configuration
Expand Down Expand Up @@ -139,6 +176,7 @@ async fn main() -> anyhow::Result<()> {
"/hnyexists/:dataset/:column/:suffix",
get(honeycomb_exists_handler),
)
.route("/dist/*file", get(static_handler))
.with_state(state);

// run it
Expand Down
Binary file added static/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="https://unpkg.com/[email protected]" integrity="sha384-QFjmbokDn2DjBjq+fM+8LUIVrAgqcNW2s0PjAxHETgRn9l4fvX31ZxDxvwQnyMOX" crossorigin="anonymous"></script>
</script>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
<link rel="icon" type="image/png" href="/favicon.png" />
<link rel="icon" type="image/ico" href="/dist/favicon.ico" />
<title>Honey Explore &mdash; {% block title %}Base{% endblock %}</title>
{% block head %}{% endblock %}
</head>
Expand Down

0 comments on commit 519a898

Please sign in to comment.