Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite from proc-macro to build.rs #15

Merged
merged 16 commits into from
Nov 11, 2024
Merged
Next Next commit
WIP move from proc-macro to build.rs
marlonbaeten committed Oct 8, 2024

Verified

This commit was signed with the committer’s verified signature.
commit 4164a7d674d5706b4d4ac892d81a7bb9bc618490
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["memory-serve", "memory-serve-macros", "examples/test"]
members = ["memory-serve", "examples/test"]
resolver = "2"

[workspace.package]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -84,6 +84,15 @@ the following configuration methods:

See [`Cache control`](#cache-control) for the cache control options.

## Asset path

The `load_assets!` macro accepts relative (from `CARGO_MANIFEST_DIR`) or absolute paths. When the environment variable `MEMORY_SERVE_ROOT`
is set, the path is constructed relative from the path provided by `MEMORY_SERVE_ROOT`.

## Build cache



## Logging

During compilation, problems that occur with the inclusion or compression
4 changes: 2 additions & 2 deletions examples/test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use axum::{response::Html, routing::get, Router};
use memory_serve::{load_assets, MemoryServe};
use memory_serve::MemoryServe;
use std::net::SocketAddr;
use tracing::info;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();

let memory_router = MemoryServe::new(load_assets!("../../static"))
let memory_router = MemoryServe::new()
.index_file(Some("/index.html"))
.into_router();

22 changes: 0 additions & 22 deletions memory-serve-macros/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion memory-serve-macros/README.md

This file was deleted.

31 changes: 0 additions & 31 deletions memory-serve-macros/src/asset.rs

This file was deleted.

62 changes: 0 additions & 62 deletions memory-serve-macros/src/lib.rs

This file was deleted.

13 changes: 12 additions & 1 deletion memory-serve/Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,9 +11,20 @@ description.workspace = true
brotli = "6.0"
flate2 = "1.0"
axum = { version = "0.7", default-features = false }
memory-serve-macros = { version = "0.6", path = "../memory-serve-macros" }
tracing = "0.1"
sha256 = "1.4"
postcard = { version = "1.0", features = ["use-std"] }
serde = "1.0"

[build-dependencies]
sha256 = "1.4"
brotli = "6.0"
tracing = "0.1"
mime_guess = "2.0"
walkdir = "2"
tracing-subscriber = { version = "0.3", features = ["fmt", "ansi"], default-features = false }
postcard = { version = "1.0", features = ["use-std"] }
serde = "1.0"

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
28 changes: 28 additions & 0 deletions memory-serve/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::path::Path;

mod utils;

const ASSET_FILE: &str = "memory_serve_assets.bin";

fn main() {
let out_dir: String = std::env::var("OUT_DIR").unwrap();

let Ok(memory_serve_dir) = std::env::var("ASSET_DIR") else {
panic!("Please specify the ASSET_DIR environment variable.");
};

let path = Path::new(&memory_serve_dir);
let path = path.canonicalize().expect("Unable to canonicalize the path specified by ASSET_DIR.");

if !path.exists() {
panic!("The path {memory_serve_dir} specified by ASSET_DIR does not exists!");
}

let target = Path::new(&out_dir).join(ASSET_FILE);

let assets = utils::list_assets(&path);
let data = postcard::to_allocvec(&assets).expect("Unable to serialize memory-serve assets.");
std::fs::write(target, data).expect("Unable to write memory-serve asset file.");

println!("cargo::rerun-if-changed={memory_serve_dir}");
}
3 changes: 2 additions & 1 deletion memory-serve/src/asset.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use axum::{
},
response::{IntoResponse, Response},
};
use serde::{Deserialize, Serialize};
use tracing::{debug, error};

use crate::{
@@ -33,7 +34,7 @@ const GZIP_ENCODING: &str = "gzip";
const GZIP_HEADER: (HeaderName, HeaderValue) =
(CONTENT_ENCODING, HeaderValue::from_static(GZIP_ENCODING));

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Asset {
pub route: &'static str,
pub path: &'static str,
Loading