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
#![deny(missing_docs)]
extern crate chan;
extern crate hoedown;
extern crate porthole;
extern crate url;
extern crate websocket as websockets;
#[macro_use]
extern crate log;
#[macro_use]
extern crate nickel;
pub mod browser;
pub mod markdown;
mod http;
mod websocket;
use std::env;
use std::net::SocketAddr;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::thread;
use http::Server as HttpServer;
use websocket::Server as WebSocketServer;
pub struct Server {
config: Config,
}
pub struct Handle {
http_server: HttpServer,
websocket_server: WebSocketServer,
markdown_sender: mpsc::Sender<String>,
}
#[derive(Debug, Clone)]
pub struct Config {
pub initial_markdown: String,
pub highlight_theme: String,
pub working_directory: PathBuf,
pub custom_css: String,
}
impl Default for Config {
fn default() -> Self {
Config {
working_directory: env::current_dir().unwrap().to_owned(),
initial_markdown: "".to_owned(),
highlight_theme: "github".to_owned(),
custom_css: "/vendor/github-markdown-css/github-markdown.css".to_owned(),
}
}
}
impl Server {
pub fn new() -> Server {
Self::new_with_config(Config { ..Default::default() })
}
pub fn new_with_config(config: Config) -> Server {
Server { config: config }
}
pub fn start(&mut self) -> Handle {
let http_server = HttpServer::new(("localhost", 0), self.config.working_directory.clone());
let mut websocket_server = WebSocketServer::new(("localhost", 0));
let (markdown_sender, markdown_receiver) = mpsc::channel::<String>();
let websocket_sender = websocket_server.start();
thread::spawn(move || {
for markdown in markdown_receiver.iter() {
let html: String = markdown::to_html(&markdown);
websocket_sender.send(html);
}
});
let websocket_port = websocket_server.local_addr().unwrap().port();
debug!("Starting http_server");
http_server.start(websocket_port, &self.config);
Handle {
http_server: http_server,
websocket_server: websocket_server,
markdown_sender: markdown_sender,
}
}
}
impl Handle {
pub fn websocket_addr(&self) -> io::Result<SocketAddr> {
self.websocket_server.local_addr()
}
pub fn http_addr(&self) -> io::Result<SocketAddr> {
self.http_server.local_addr()
}
pub fn change_working_directory<P>(&mut self, dir: P)
where P: AsRef<Path>
{
self.http_server.change_working_directory(dir);
}
pub fn send<S>(&self, data: S)
where S: Into<String>
{
self.markdown_sender.send(data.into()).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::Server;
#[test]
fn sanity() {
let mut server = Server::new();
server.start();
}
}