1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Contains the HTTP server component.

use std::fs::{self, File};
use std::io::prelude::*;
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::sync::{Arc, Mutex};

use handlebars_iron::{DirectorySource, HandlebarsEngine, Template};
use iron::prelude::*;
use iron::{self, status, Handler};
use mount::Mount;
use staticfile::Static;

lazy_static! {
    static ref CRATE_ROOT: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
}

/// The HTTP server.
///
/// The server listens on the provided port, rendering the markdown preview when a GET request is
/// received at the server root.
pub struct Server {
    websocket_port: u16,
    styles: StyleConfig,
    working_directory: PathBuf,
}

pub struct StyleConfig {
    pub highlight_theme: String,
    pub css: Vec<String>,
}

/// Data that is passed to the HTML template when rendering.
#[derive(Debug, Serialize)]
struct TemplateData {
    websocket_port: u16,
    initial_html: String,
    highlight_theme: String,

    /// URLs pointing to remote CSS resources.
    remote_custom_css: Vec<String>,

    /// Full text of local CSS resources.
    local_custom_css: Vec<String>,
}

impl Server {
    /// Creates a new server that listens on socket address `addr`.
    pub fn new(working_directory: PathBuf, websocket_port: u16, styles: StyleConfig) -> Server {
        Server {
            working_directory: working_directory,
            websocket_port: websocket_port,
            styles: styles,
        }
    }

    /// Starts the server.
    ///
    /// Once a connection is received, the client will initiate WebSocket connections on
    /// `websocket_port`. If `initial_markdown` is present, it will be displayed on the first
    /// connection.
    pub fn listen<A>(self, address: A, initial_html: &str) -> io::Result<Listening>
    where
        A: ToSocketAddrs,
    {
        let working_directory = Arc::new(Mutex::new(self.working_directory));

        // This is a bit of a hack, should probably use real URLs here.
        let (remote_custom_css, local_custom_css): (Vec<String>, Vec<String>) = self.styles
            .css
            .into_iter()
            .partition(|css| !css.starts_with("file://"));

        let local_custom_css = local_custom_css
            .into_iter()
            .flat_map(|file_uri| File::open(file_uri.trim_start_matches("file://")).ok())
            .map(|mut file| {
                let mut css = String::new();
                file.read_to_string(&mut css).unwrap();
                css
            })
            .collect();

        let handler = create_handler(MarkdownPreview {
            template_data: TemplateData {
                websocket_port: self.websocket_port,
                initial_html: initial_html.to_owned(),
                highlight_theme: self.styles.highlight_theme,
                remote_custom_css,
                local_custom_css,
            },
            working_directory: working_directory.clone(),
        });

        let listening = Listening {
            working_directory: working_directory,
            listening: Iron::new(handler)
                .http(address)
                .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?,
        };

        Ok(listening)
    }
}

#[derive(Debug)]
pub struct Listening {
    listening: iron::Listening,
    working_directory: Arc<Mutex<PathBuf>>,
}

impl Listening {
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        Ok(self.listening.socket)
    }

    pub fn change_working_directory<P>(&mut self, dir: P)
    where
        P: AsRef<Path>,
    {
        let mut working_directory = self.working_directory.lock().unwrap();
        *working_directory = dir.as_ref().to_owned();
    }
}

impl Drop for Listening {
    fn drop(&mut self) {
        self.listening.close().unwrap();
    }
}

/// Wraps the markdown handler with other middleware, such as template rendering and static file
/// serving.
fn create_handler(previewer: MarkdownPreview) -> Box<Handler> {
    let mut chain = Chain::new(previewer);

    let mut hbse = HandlebarsEngine::new();
    hbse.add(Box::new(DirectorySource::new(
        CRATE_ROOT.join("templates/").to_str().unwrap(),
        ".html",
    )));
    if let Err(r) = hbse.reload() {
        panic!("{}", r);
    }

    chain.link_after(hbse);

    let mut mount = Mount::new();
    mount.mount("/", chain);
    mount.mount("/_static", Static::new(CRATE_ROOT.join("static")));

    Box::new(mount)
}

struct MarkdownPreview {
    template_data: TemplateData,
    working_directory: Arc<Mutex<PathBuf>>,
}

impl Handler for MarkdownPreview {
    fn handle(&self, req: &mut Request) -> IronResult<Response> {
        let url_path = req.url.path().join(&MAIN_SEPARATOR.to_string());

        if url_path.is_empty() {
            Ok(Response::with((
                Template::new("markdown_view", &self.template_data),
                status::Ok,
            )))
        } else {
            let local_cwd = self.working_directory.clone();
            let path = local_cwd.lock().unwrap().join(&url_path);

            match fs::metadata(&path) {
                Ok(ref attr) if attr.is_file() => return Ok(Response::with((path, status::Ok))),
                Err(ref e) if e.kind() != io::ErrorKind::NotFound => {
                    debug!("Error getting metadata for file: '{:?}': {:?}", path, e);
                }
                _ => (),
            }

            Ok(Response::with(status::NotFound))
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate iron_test;

    use std::path::PathBuf;
    use std::sync::{Arc, Mutex};

    use iron::Headers;
    use self::iron_test::request;

    use config;
    use super::{MarkdownPreview, TemplateData};

    #[test]
    fn simple() {
        let handler = super::create_handler(MarkdownPreview {
            template_data: TemplateData {
                websocket_port: 1337,
                initial_html: String::new(),
                highlight_theme: config::DEFAULT_HIGHLIGHT_THEME.to_string(),
                remote_custom_css: vec![config::DEFAULT_CSS.to_string()],
                local_custom_css: vec![],
            },
            working_directory: Arc::new(Mutex::new(PathBuf::new())),
        });

        let response = request::get("http://localhost:3000/", Headers::new(), &handler).unwrap();
        assert!(
            response.status.unwrap().is_success(),
            "could not load index"
        );

        let response = request::get(
            "http://localhost:3000/_static/js/markdown_client.js",
            Headers::new(),
            &handler,
        ).unwrap();
        assert!(
            response.status.unwrap().is_success(),
            "static file not found"
        );

        let response = request::get(
            "http://localhost:3000/non-existent",
            Headers::new(),
            &handler,
        ).unwrap();
        assert!(
            response.status.unwrap().is_client_error(),
            "found non-existent file"
        );
    }

    #[test]
    fn vendored() {
        let handler = super::create_handler(MarkdownPreview {
            template_data: TemplateData {
                websocket_port: 1337,
                initial_html: String::new(),
                highlight_theme: config::DEFAULT_HIGHLIGHT_THEME.to_string(),
                remote_custom_css: vec![config::DEFAULT_CSS.to_string()],
                local_custom_css: vec![],
            },
            working_directory: Arc::new(Mutex::new(PathBuf::new())),
        });

        let response = request::get(
            "http://localhost:3000/_static/vendor/highlight.js/highlight.pack.js",
            Headers::new(),
            &handler,
        ).unwrap();
        assert!(
            response.status.unwrap().is_success(),
            "vendored file not found"
        );
    }
}