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
#![deny(missing_docs)]
#![recursion_limit = "1024"]
extern crate handlebars_iron;
extern crate iron;
extern crate mount;
extern crate pulldown_cmark;
extern crate serde;
extern crate staticfile;
extern crate url;
extern crate websocket as websockets;
#[macro_use]
extern crate chan;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
pub mod browser;
pub mod errors;
pub mod markdown;
mod config;
mod http;
mod websocket;
use std::io;
use std::net::SocketAddr;
use std::path::Path;
use std::process::Command;
use config::ExternalRenderer;
use errors::*;
pub use config::Config;
#[derive(Debug, Clone)]
pub struct Server {
config: Config,
}
impl Server {
pub fn new() -> Self {
Self::default()
}
pub fn new_with_config(config: Config) -> Self {
Server { config }
}
pub fn start(&self) -> Result<Listening> {
debug!("starting websocket server");
let websocket_listening =
websocket::Server::new().listen(("localhost", self.config.websocket_port))?;
debug!(
"websockets listening on {}",
websocket_listening.local_addr()?
);
let config = &self.config;
let initial_html = if let Some(ref initial_markdown) = config.initial_markdown.as_ref() {
markdown_to_html(initial_markdown, &config.external_renderer)?
} else {
String::new()
};
debug!("starting http_server");
let assigned_websocket_port = websocket_listening.local_addr()?.port();
let http_listening = http::Server::new(
self.config.working_directory.clone(),
assigned_websocket_port,
http::StyleConfig {
css: config.custom_css.clone(),
highlight_theme: config.highlight_theme.clone(),
},
).listen(("localhost", config.http_port), &initial_html)?;
debug!("http listening on {}", http_listening.local_addr()?);
let listening = Listening {
http_listening: http_listening,
websocket_listening: websocket_listening,
external_renderer: config.external_renderer.clone(),
};
Ok(listening)
}
}
impl Default for Server {
fn default() -> Self {
Server {
config: Default::default(),
}
}
}
pub struct Listening {
http_listening: http::Listening,
websocket_listening: websocket::Listening,
external_renderer: Option<ExternalRenderer>,
}
impl Listening {
pub fn websocket_addr(&self) -> io::Result<SocketAddr> {
self.websocket_listening.local_addr()
}
pub fn http_addr(&self) -> io::Result<SocketAddr> {
self.http_listening.local_addr()
}
pub fn change_working_directory<P>(&mut self, dir: P)
where
P: AsRef<Path>,
{
self.http_listening.change_working_directory(dir);
}
pub fn send(&self, markdown: &str) -> Result<()> {
let html = markdown_to_html(markdown, &self.external_renderer)?;
self.websocket_listening.send(html);
Ok(())
}
}
fn markdown_to_html(markdown: &str, external_command: &Option<ExternalRenderer>) -> Result<String> {
let html = if let Some(&(ref command, ref args)) = external_command.as_ref() {
let mut command = Command::new(command);
command.args(args);
markdown::to_html_external(command, markdown)?
} else {
markdown::to_html_cmark(markdown)
};
Ok(html)
}
#[cfg(test)]
mod tests {
use super::Server;
#[test]
fn sanity() {
let server = Server::new();
server.start().unwrap();
}
}