Browse Source

Serving static files (sorta kinda, still need to check the type)

master
Julio Biason 5 months ago
parent
commit
1c7717ab10
  1. 64
      Cargo.lock
  2. 1
      Cargo.toml
  3. 7
      src/main.rs
  4. 26
      src/staticfiles/mod.rs
  5. 5
      staticfiles/main.css
  6. 1
      templates/index.html

64
Cargo.lock generated

@ -526,6 +526,7 @@ dependencies = [
"askama",
"axum",
"clap",
"phf",
"tokio",
]
@ -569,6 +570,48 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "phf"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
dependencies = [
"phf_macros",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project"
version = "1.1.5"
@ -619,6 +662,21 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rustc-demangle"
version = "0.1.24"
@ -690,6 +748,12 @@ dependencies = [
"serde",
]
[[package]]
name = "siphasher"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
[[package]]
name = "smallvec"
version = "1.13.2"

1
Cargo.toml

@ -7,4 +7,5 @@ edition = "2021"
askama = "0.12.1"
axum = "0.7.5"
clap = { version = "4.5.7", features = ["derive", "env"] }
phf = { version = "0.11.2", features = ["macros"] }
tokio = { version = "1.38.0", features = ["rt-multi-thread", "macros"] }

7
src/main.rs

@ -1,5 +1,6 @@
mod index;
mod args;
mod index;
mod staticfiles;
use axum::routing::get;
use axum::Router;
@ -12,7 +13,9 @@ async fn main() {
println!("Starting server at {:?}", &opts.address);
let app = Router::new().route("/", get(index::index));
let app = Router::new()
.route("/", get(index::index))
.route("/static/:file", get(staticfiles::staticfile));
let listener = tokio::net::TcpListener::bind(&opts.address).await.unwrap();
axum::serve(listener, app).await.unwrap();
}

26
src/staticfiles/mod.rs

@ -0,0 +1,26 @@
//! Static file serving
use axum::body::Body;
use axum::extract::Path;
use axum::http::header;
use axum::http::StatusCode;
use axum::response::Response;
use phf::phf_map;
static FILES: phf::Map<&'static str, &'static str> = phf_map! {
"main.css" => include_str!("../../staticfiles/main.css")
};
pub(super) async fn staticfile(Path(filename): Path<String>) -> Response {
match FILES.get(&filename) {
Some(content) => Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/css")
.body(Body::from(*content))
.unwrap(),
None => Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("Not found"))
.unwrap(),
}
}

5
staticfiles/main.css

@ -0,0 +1,5 @@
/* main */
body {
background-color: #14142E;
color: #ffffff;
}

1
templates/index.html

@ -3,6 +3,7 @@
<head>
<meta charset="utf-8" />
<title>NoteVine</title>
<link rel="stylesheet" href="static/main.css" />
</head>
<body>
{% block main %}

Loading…
Cancel
Save