-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support server, location and upstream editor
- Loading branch information
Showing
15 changed files
with
753 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ target/ | |
/target | ||
.vscode | ||
node_moudles | ||
dist |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use bytes::Bytes; | ||
use hex::encode; | ||
use http::{header, HeaderValue, StatusCode}; | ||
use rust_embed::EmbeddedFile; | ||
|
||
use crate::cache::HttpResponse; | ||
|
||
pub struct StaticFile(pub Option<EmbeddedFile>); | ||
|
||
impl From<StaticFile> for HttpResponse { | ||
fn from(value: StaticFile) -> Self { | ||
if value.0.is_none() { | ||
return HttpResponse::not_found(); | ||
} | ||
// value 0 is some | ||
let file = value.0.unwrap(); | ||
// hash为基于内容生成 | ||
let str = &encode(file.metadata.sha256_hash())[0..8]; | ||
let mime_type = file.metadata.mimetype(); | ||
// 长度+hash的一部分 | ||
let entity_tag = format!(r#""{:x}-{str}""#, file.data.len()); | ||
// 因为html对于网页是入口,避免缓存后更新不及时 | ||
// 因此设置为0 | ||
// 其它js,css会添加版本号,因此无影响 | ||
let max_age = if mime_type.contains("text/html") { | ||
0 | ||
} else { | ||
24 * 3600 | ||
}; | ||
|
||
let mut headers = vec![]; | ||
if let Ok(value) = HeaderValue::from_str(mime_type) { | ||
headers.push((header::CONTENT_TYPE, value)); | ||
} | ||
if let Ok(value) = HeaderValue::from_str(&entity_tag) { | ||
headers.push((header::ETAG, value)); | ||
} | ||
|
||
HttpResponse { | ||
status: StatusCode::OK, | ||
body: Bytes::copy_from_slice(&file.data), | ||
max_age: Some(max_age), | ||
headers: Some(headers), | ||
..Default::default() | ||
} | ||
} | ||
} |
Oops, something went wrong.