@@ -7,7 +7,8 @@ use medup::markdown::{self, Markdown};
77
88use clap:: ArgMatches ;
99use warp:: filters:: BoxedFilter ;
10- use warp:: { http:: Response , Filter , Reply } ;
10+ use warp:: hyper:: StatusCode ;
11+ use warp:: { Filter , Reply } ;
1112
1213#[ tokio:: main]
1314pub 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/*
4739fn 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+
94108fn 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