Skip to content

Commit 2bd6ac6

Browse files
committed
feature: added index page
Signed-off-by: skoowu <skoowu.fancy@gmail.com>
1 parent afd38e4 commit 2bd6ac6

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ A markdown parsing toolkit
1919
Usage: medup <COMMAND>
2020
2121
Commands:
22-
gen generate HTML based on Markdown!
23-
serve Provide an http service for markdown parsing!
22+
gen Generate html from markdown
23+
serve Provide an http service for markdown parsing
2424
help Print this message or the help of the given subcommand(s)
2525
2626
Options:
@@ -47,4 +47,4 @@ or
4747
docker run -d -p 8181:8181 --rm skoowoo/medup:20230302-0
4848
```
4949

50-
Open `http://localhost:8181/markdown-guide.md` with your browser.
50+
Open `http://localhost:8181` with your browser.

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Welcome to Medup !
3+
4+
* [markdown-guide](./markdown-guide.md)

src/bin/medup/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ fn cli() -> Command {
2222
.subcommand_required(true)
2323
.subcommand(
2424
Command::new("gen")
25-
.about("generate HTML based on Markdown!")
25+
.about("Generate html from markdown")
2626
.arg(arg!(-c --"config-path" [CONFIG_PATH] "Specify path of the config file, it's optional."))
2727
.arg(arg!(-o --output [OUTPUT_HTML_PATH] "Specify a html output path, it's optional."))
2828
.arg(arg!(<MARKDOWN_FILE_PATH>)),
2929
)
3030
.subcommand(
3131
Command::new("serve")
32-
.about("Provide an http service for markdown parsing!")
32+
.about("Provide an http service for markdown parsing")
3333
.arg(arg!(-l --"listen-addr" [LISTEN_ADDR] r#"Specify the listening address of the http server, default ":8181"."#))
3434
.arg(arg!(-c --"config-path" [CONFIG_PATH] "Specify path of the config file, it's optional."))
3535
.arg(arg!(-d --dir [DIR] "Specify the directory where markdown files are stored."))

src/bin/medup/serve.rs

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use medup::markdown::{self, Markdown};
77

88
use clap::ArgMatches;
99
use warp::filters::BoxedFilter;
10-
use warp::{http::Response, Filter, Reply};
10+
use warp::hyper::StatusCode;
11+
use warp::{Filter, Reply};
1112

1213
#[tokio::main]
1314
pub async fn proc_serve(matches: &ArgMatches) {
@@ -27,22 +28,13 @@ pub async fn proc_serve(matches: &ArgMatches) {
2728

2829
// All filters are used to match requests
2930
let filters = static_filter(sdir.to_string())
30-
.or(articles_filter(cfg, dir.to_string()))
31-
.or(index_filter());
31+
.or(articles_filter(cfg.clone(), dir.to_string()))
32+
.or(index_filter(cfg.clone(), dir.to_string()));
3233

3334
println!("---> start to listen on address: \"{}:{}\"", addr, port);
3435
warp::serve(filters).run((addr, port)).await
3536
}
3637

37-
// TODO:
38-
fn index_filter() -> BoxedFilter<(impl Reply,)> {
39-
warp::get()
40-
.and(warp::path::end())
41-
.map(|| Response::builder().body("Hello, Medup!").into_response())
42-
.with(warp::cors().allow_any_origin())
43-
.boxed()
44-
}
45-
4638
// Get /static/*
4739
fn static_filter(dir: String) -> BoxedFilter<(impl Reply,)> {
4840
warp::get()
@@ -62,27 +54,20 @@ fn articles_filter(cfg: Config, dir: String) -> BoxedFilter<(impl Reply,)> {
6254
if !name.ends_with(".md") {
6355
name.push_str(".md");
6456
}
65-
let buf = Path::new(&dir).join(&name);
66-
let s = buf.to_str();
67-
match s {
68-
None => Response::builder()
69-
.header("X-Powered-By", "Medup")
70-
.status(400)
71-
.body(format!(
72-
"failed to join the path: \"{}\", \"{}\"",
73-
dir, name
74-
))
75-
.into_response(),
57+
match Path::new(&dir).join(&name).to_str() {
58+
None => error_repsonse(
59+
StatusCode::BAD_REQUEST,
60+
format!(r#"failed to join the path: {}, {}"#, dir, name),
61+
),
7662
Some(path) => match Markdown::new()
7763
.config(cfg)
7864
.path(path)
7965
.map_mut(markdown::to_html)
8066
{
81-
Err(e) => Response::builder()
82-
.header("X-Powered-By", "Medup")
83-
.status(500)
84-
.body(format!("failed to generate html from markdown: {}", e))
85-
.into_response(),
67+
Err(e) => error_repsonse(
68+
StatusCode::INTERNAL_SERVER_ERROR,
69+
format!("failed to generate html from markdown: {}", e),
70+
),
8671
Ok(v) => warp::reply::html(v).into_response(),
8772
},
8873
}
@@ -91,6 +76,35 @@ fn articles_filter(cfg: Config, dir: String) -> BoxedFilter<(impl Reply,)> {
9176
.boxed()
9277
}
9378

79+
// Get /, then read the index.md file
80+
fn index_filter(cfg: Config, dir: String) -> BoxedFilter<(impl Reply,)> {
81+
warp::get()
82+
.and(warp::path::end())
83+
.and(warp::any().map(move || cfg.clone()))
84+
.and(warp::any().map(move || dir.to_string()))
85+
.map(
86+
|cfg: Config, dir: String| match Path::new(&dir).join("index.md").to_str() {
87+
None => error_repsonse(
88+
StatusCode::BAD_REQUEST,
89+
format!(r#"failed to join the path: {}, index.md"#, dir),
90+
),
91+
Some(path) => match Markdown::new()
92+
.config(cfg)
93+
.path(path)
94+
.map_mut(markdown::to_html)
95+
{
96+
Err(e) => error_repsonse(
97+
StatusCode::INTERNAL_SERVER_ERROR,
98+
format!("failed to generate html from markdown: {}", e),
99+
),
100+
Ok(v) => warp::reply::html(v).into_response(),
101+
},
102+
},
103+
)
104+
.with(warp::cors().allow_any_origin())
105+
.boxed()
106+
}
107+
94108
fn get_dir<'get_dir>(matches: &'get_dir ArgMatches, name: &str) -> &'get_dir str {
95109
match matches.get_one::<String>(name) {
96110
None => ".",
@@ -144,3 +158,11 @@ fn parse_ip_port(matches: &ArgMatches) -> Result<(Ipv4Addr, u16), Box<dyn Error>
144158

145159
Ok((ipaddr, port))
146160
}
161+
162+
fn error_repsonse(status_code: StatusCode, err_msg: String) -> warp::reply::Response {
163+
warp::http::Response::builder()
164+
.header("X-Powered-By", "Medup")
165+
.status(status_code)
166+
.body(err_msg)
167+
.into_response()
168+
}

0 commit comments

Comments
 (0)