Skip to content

Commit

Permalink
fix(static): 静态资源content-type缺失异常修复
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertshelley committed May 28, 2024
1 parent 877e3cc commit e52f2b4
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 299 deletions.
2 changes: 1 addition & 1 deletion examples/grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hello_world::HelloRequest;
pub mod hello_world {
tonic::include_proto!("helloworld");
}

#[allow(dead_code)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = GreeterClient::connect("http://0.0.0.0:50051").await?;
Expand Down
1 change: 1 addition & 0 deletions examples/grpc_streaming/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async fn bidirectional_streaming_echo_throttle(client: &mut EchoClient<Channel>,
}
}

#[allow(dead_code)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = EchoClient::connect("http://0.0.0.0:50051").await.unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/grpc_streaming/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use silent::prelude::{logger, HandlerAppend, Level, Route, RouteService, Server};

mod client;
Expand Down
3 changes: 0 additions & 3 deletions examples/llma_chat/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions examples/llma_chat/Cargo.toml

This file was deleted.

91 changes: 0 additions & 91 deletions examples/llma_chat/src/llm_middleware.rs

This file was deleted.

137 changes: 0 additions & 137 deletions examples/llma_chat/src/main.rs

This file was deleted.

48 changes: 0 additions & 48 deletions examples/llma_chat/src/message_event_middleware.rs

This file was deleted.

1 change: 1 addition & 0 deletions silent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ pbkdf2 = { version = "0.12", features = ["simple"], optional = true }
aes-gcm = { version = "0.10.3", optional = true }
aes = { version = "0.8", optional = true }
rsa = { version = "0.9.6", optional = true }
mime_guess = "2.0.4"
6 changes: 5 additions & 1 deletion silent/src/handler/handler_wrapper_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::prelude::stream_body;
use crate::{Handler, Request, Response, SilentError, StatusCode};
use async_trait::async_trait;
use futures_util::StreamExt;
use mime::Mime;
use tokio::fs::File;
use tokio_util::io::ReaderStream;

Expand Down Expand Up @@ -38,8 +39,11 @@ impl Handler for HandlerWrapperStatic {
if path.ends_with('/') {
path.push_str("index.html");
}
if let Ok(file) = File::open(path).await {
if let Ok(file) = File::open(&path).await {
let mut res = Response::empty();
if let Some(content_type) = mime_guess::from_path(path).first() {
res.set_typed_header(headers::ContentType::from(content_type));
}
let reader_stream = ReaderStream::new(file);
let stream = reader_stream.boxed();
res.set_body(stream_body(stream));
Expand Down
8 changes: 8 additions & 0 deletions silent/src/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ impl Route {
Route::new("<path:**>").insert_handler(Method::GET, Arc::new(static_handler(path))),
)
}

#[cfg(feature = "static")]
pub fn with_static_in_url(self, url: &str, path: &str) -> Self {
self.append(
Route::new(format!("{}/<path:**>", url).as_str())
.insert_handler(Method::GET, Arc::new(static_handler(path))),
)
}
}

#[cfg(test)]
Expand Down
6 changes: 4 additions & 2 deletions silent/src/templates/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub struct TemplateResponse {
data: Value,
}

impl<T: Serialize> From<(String, T)> for TemplateResponse {
fn from((template, data): (String, T)) -> Self {
impl<T: Serialize, S: Into<String>> From<(S, T)> for TemplateResponse {
fn from((template, data): (S, T)) -> Self {
let template = template.into();
serde_json::to_value(data)
.map(|data| TemplateResponse { template, data })
.unwrap()
Expand Down Expand Up @@ -61,6 +62,7 @@ impl MiddleWareHandler for TemplateMiddleware {
})?
.into(),
);
res.set_typed_header(headers::ContentType::html());
Ok(MiddlewareResult::Continue)
}
}
Expand Down

0 comments on commit e52f2b4

Please sign in to comment.