Skip to content

Commit 6b386fd

Browse files
committed
优化更新
1 parent d771e07 commit 6b386fd

15 files changed

+68
-31
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
## nako-blog 博客系统
22

3-
`nako-blog` 是使用 `actix-web`, `sea-orm``tera``rust` 博客系统
3+
`nako-blog` 是使用 actix-web, sea-orm 及 tera 的 `rust` 博客系统
44

55

66
### 项目介绍
77

88
* 使用 `rust` 开发的通用博客系统
99
* 核心使用 `actix-web`, `sea-orm``tera` 等开发
1010
* 博客后台使用 `pear-admin` 后端模板,非前后端分离项目
11-
* 博客模板为可设置模板
11+
* 打包静态文件, 模板文件及配置文件。只需一个文件即可部署
1212

1313

1414
### 环境要求
1515

16-
- rust >= 1.18
16+
- rust
17+
- cargo
1718
- Myql
1819
- Redis
1920

@@ -62,7 +63,7 @@ git clone https://github.com/deatil/nako-blog.git
6263
2. 然后配置数据库等信息
6364

6465
```
65-
/conf.ini
66+
/assert/config/conf.ini
6667
```
6768

6869
3. 最后导入 sql 数据到数据库

assert/config/conf.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ upload_url = "/upload"
5151

5252
# 主题
5353
[view]
54-
blog_tpl_path = "./assert/templates/blog"
54+
blog_tpl_path = "blog"
5555
blog_theme = "nako"

src/boot/app.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ pub async fn start() -> std::io::Result<()> {
6767
let server_url = format!("{host}:{port}");
6868

6969
let conn = db::connect().await.unwrap_or_default();
70-
let mut view = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/assert/templates/**/*")).unwrap_or_default();
70+
71+
let mut view: Tera;
72+
73+
// 是否打包
74+
let is_embed = config::section::<bool>("app", "is_embed", true);
75+
if is_embed {
76+
view = Tera::default();
77+
78+
for file in embed::Templates::iter() {
79+
let filename = file.as_ref();
80+
view.add_raw_template(filename.clone(), embed::get_tpl_data(filename.clone()).as_str()).unwrap_or_default();
81+
}
82+
} else {
83+
view = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/assert/templates/**/*")).unwrap_or_default();
84+
}
7185

7286
// 设置模板函数
7387
nako_view::set_fns(&mut view);

src/boot/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::nako::{
3131
};
3232

3333
pub(crate) async fn app_default(req: HttpRequest) -> impl Responder {
34-
get_error_response(&req, "no page")
34+
get_error_response(&req, "404 Not Found")
3535
}
3636

3737
pub(crate) fn json_parser_error(

src/nako/app.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use actix_web::{
55
HttpResponse,
66
};
77

8-
use crate::nako::http;
98
use crate::nako::{
9+
http,
10+
embed,
11+
utils,
1012
config,
1113
};
1214

@@ -53,10 +55,30 @@ pub fn attach_path(name: String) -> String {
5355

5456
// 列出模板
5557
pub fn list_tpls_by_prefix(file_prefix: String) -> Vec<String> {
56-
let tpl = config::section::<String>("view", "blog_tpl_path", "./assert/templates/blog".to_string());
58+
let base_path = "./assert/templates";
59+
60+
let tpl_path = config::section::<String>("view", "blog_tpl_path", "blog".to_string());
5761
let theme = config::section::<String>("view", "blog_theme", "nako".to_string());
5862

59-
let path = format!("{}/{}/", tpl, theme);
63+
// 打包静态文件
64+
let is_embed = config::section::<bool>("app", "is_embed", true);
65+
if is_embed {
66+
let prefix = format!("{}/{}/{}", tpl_path, theme, file_prefix);
67+
68+
let mut tpl_list: Vec<String> = Vec::new();
69+
for file in embed::Templates::iter() {
70+
let filename = file.as_ref();
71+
let file_name = filename.clone();
72+
73+
if file_name.starts_with(&prefix) {
74+
tpl_list.push(utils::get_path_filename(filename.clone()));
75+
}
76+
}
77+
78+
return tpl_list;
79+
}
80+
81+
let path = format!("{}/{}/{}/", base_path, tpl_path, theme);
6082

6183
let path_object = Path::new(&path);
6284

@@ -101,9 +123,10 @@ pub fn page_tpls() -> Vec<String> {
101123

102124
// 模板路径
103125
pub fn view_path(name: &str) -> String {
126+
let tpl_path = config::section::<String>("view", "blog_tpl_path", "blog".to_string());
104127
let theme = config::section::<String>("view", "blog_theme", "nako".to_string());
105128

106-
let path = format!("blog/{}/{}", theme, name);
129+
let path = format!("{}/{}/{}", tpl_path, theme, name);
107130

108131
path
109132
}

src/nako/file.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl File {
1717
}
1818

1919
/// 判断是否存在
20-
pub fn exists(f: &str) -> bool {
20+
pub fn exists(&self, f: &str) -> bool {
2121
if path::Path::new(f).exists() {
2222
return true;
2323
}
@@ -26,14 +26,14 @@ impl File {
2626
}
2727

2828
/// 删除文件
29-
pub fn remove(f: &str) -> Result<(), Error> {
29+
pub fn remove(&self, f: &str) -> Result<(), Error> {
3030
fs::remove_file(f)?;
3131

3232
Ok(())
3333
}
3434

3535
/// 创建文件
36-
pub fn create(f: &str) -> Result<fs::File, Error> {
36+
pub fn create(&self, f: &str) -> Result<fs::File, Error> {
3737
let file = fs::OpenOptions::new()
3838
.write(true)
3939
.create_new(true)
@@ -43,7 +43,7 @@ impl File {
4343
}
4444

4545
/// 读取
46-
pub fn read(f: &str) -> Result<String, Error> {
46+
pub fn read(&self, f: &str) -> Result<String, Error> {
4747
let mut file = fs::File::open(f)?;
4848

4949
let mut buffer = Vec::new();
@@ -55,7 +55,7 @@ impl File {
5555
}
5656

5757
/// 写入信息
58-
pub fn write(f: &str, content: String) -> Result<(), Error> {
58+
pub fn write(&self, f: &str, content: String) -> Result<(), Error> {
5959
let path = path::Path::new(f);
6060

6161
let mut file = fs::File::create(path)?;
@@ -65,14 +65,14 @@ impl File {
6565
}
6666

6767
/// 创建文件夹
68-
pub fn mkdir(d: &str) -> Result<(), Error> {
68+
pub fn mkdir(&self, d: &str) -> Result<(), Error> {
6969
fs::create_dir_all(d)?;
7070

7171
Ok(())
7272
}
7373

7474
/// 删除文件夹
75-
pub fn rmdir(d: &str) -> Result<(), Error> {
75+
pub fn rmdir(&self, d: &str) -> Result<(), Error> {
7676
fs::remove_dir(d)?;
7777

7878
Ok(())

src/nako/http.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use actix_web::{
1111

1212
use crate::nako::{
1313
app,
14-
embed,
15-
config,
1614
global::Serialize,
1715
};
1816

@@ -69,17 +67,7 @@ pub fn view_data() -> tera::Context {
6967
pub fn view(view: &mut tera::Tera, name: &str, ctx: &tera::Context) -> HttpResponse {
7068
let err = format!("html is error.");
7169

72-
let render: tera::Result<String>;
73-
74-
let is_embed = config::section::<bool>("app", "is_embed", true);
75-
if is_embed {
76-
let tpl_data = embed::get_tpl_data(name);
77-
render = view.render_str(tpl_data.as_str(), ctx);
78-
} else {
79-
render = view.render(name, ctx);
80-
}
81-
82-
let res_body: String = match render {
70+
let res_body: String = match view.render(name, ctx) {
8371
Ok(v) => v,
8472
Err(e) => {
8573
if app::is_debug() {

src/nako/utils.rs

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ pub fn get_extension(filename: &str) -> String {
5757
"".to_string()
5858
}
5959

60+
pub fn get_path_filename(path: &str) -> String {
61+
let path_string = path.to_string();
62+
let files = path_string.split("/").collect::<Vec<_>>();
63+
let file_name = files.last();
64+
65+
match file_name {
66+
Some(v) => v.to_string(),
67+
None => "".to_string(),
68+
}
69+
}
70+
6071
pub fn formatsize<T: humansize::ToF64 + humansize::Unsigned>(size: T) -> String {
6172
let res: String = format_size(size, DECIMAL);
6273

Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)