-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.rs
395 lines (352 loc) · 12.8 KB
/
web_server.rs
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use async_trait::async_trait;
use horrorshow::helper::doctype;
use horrorshow::prelude::*;
use http_types::mime;
use serde_json::json;
use std::sync::Arc;
use tide::{
http::StatusCode, log, sse::Sender, Middleware, Next, Request, Response, Server, Status,
};
use crate::content_finder::{ContentError, ContentFinder};
use crate::markdown_converter::{Converter, MarkdownConverter, MarkdownError};
use crate::offline_converter::OfflineConverter;
use crate::static_files;
/// Allows us to use either a GitHub API-based converter or an offline converter
/// through pulldown cmark.
pub enum Converters {
Github(Converter),
Offline(OfflineConverter),
}
#[async_trait]
impl MarkdownConverter for Converters {
async fn convert_markdown(&self, md: &str) -> Result<String, MarkdownError> {
match self {
Converters::Github(converter) => converter.convert_markdown(&md).await,
Converters::Offline(offline) => offline.convert_markdown(&md).await,
}
}
}
/// The state necessary to process requests.
///
/// It needs something to find some markdown content based on a URL path and something to take that
/// markdown content and convert it to HTML to display.
pub struct State<M, C>
where
M: MarkdownConverter,
C: ContentFinder,
{
markdown_converter: M,
content_finder: C,
}
impl<M, C> State<M, C>
where
M: MarkdownConverter + Send + Sync + 'static,
C: ContentFinder + Send + Sync + 'static,
{
pub fn new(markdown_converter: M, content_finder: C) -> State<M, C> {
State {
markdown_converter,
content_finder,
}
}
}
/// The basic HTML of our page, the `<head>` and CSS and `<body>`.
/// Also includes the script to subscribe to the Server Sent Events for the page
/// and update the page if the file changes.
fn base_html(title: &str, content: &str) -> String {
format!(
"{}",
html! {
: doctype::HTML;
html {
head {
link(rel="stylesheet", href="/static/octicons/octicons.css");
link(rel="stylesheet", href="https://github.githubassets.com/assets/frameworks-146fab5ea30e8afac08dd11013bb4ee0.css");
link(rel="stylesheet", href="https://github.githubassets.com/assets/site-897ad5fdbe32a5cd67af5d1bdc68a292.css");
link(rel="stylesheet", href="https://github.githubassets.com/assets/github-c21b6bf71617eeeb67a56b0d48b5bb5c.css");
link(rel="stylesheet", href="/static/style.css");
title : title;
script {
: Raw("let hash = '';
let event = new EventSource(`//${location.host}/__rs-readme${location.pathname}`);
event.addEventListener('update', (e) => {
let message = JSON.parse(e.data);
if (message.hash !== hash) {
hash = message.hash;
document.getElementById('rs-readme-content').innerHTML = message.contents;
}
});")
}
}
body : Raw(content);
}
}
)
}
/// The wrapping necessary to make the rendered markdown file to look right
fn markdown_html(file_name: &str, md_content: &str) -> String {
format!(
"{}",
html! {
div(class="page") {
div(id="preview-page", class="preview-page") {
div(role="main", class="main-content") {
div(class="container new-discussion-timeline experiment-repo-nav") {
div(class="repository-content") {
div(id="readme", class="readme boxed-group clearfix announce instapaper_body md") {
h3 {
span(class="octicon octicon-book");
: format!(" {}",file_name);
}
article(id="rs-readme-content", class="markdown-body entry-content", itemprop="text") {
: Raw(md_content);
}
}
}
}
}
}
div : Raw(" ");
}
}
)
}
/// The error HTML indicating the requested file is not markdown
/// and therefore can't be rendered.
fn not_markdown_html(title: &str, file: &str) -> String {
format!(
"{}",
html! {
: doctype::HTML;
html {
head {
title : title;
}
body {
h1 : "Not a Markdown File";
p {
strong : file;
: " is not a markdown file and cannot be rendered";
}
}
}}
)
}
/// The error HTML indicating the requested file cannot be found.
fn file_not_found(title: &str, file: &str) -> String {
format!(
"{}",
html! {
: doctype::HTML;
html {
head {
title : title;
}
body {
h1 {
: "Couldn't find ";
: file;
}
p {
: "For the index page ";
em : "rs-readme";
: " will look for a file named README in the root folder. Otherwise it looks for an exact file name.";
}
}
}
}
)
}
/// The `tide::Endpoint` to render the `README.md`.
///
/// It assumes that there will be a `README.md` in your folder. It lets us have a special error
/// message for it and lets the root of the website render `README.md`. It might not be necessary
/// though, maybe we could just redirect `/` to `/README.md`.
async fn render_readme(
req: Request<
Arc<
State<impl MarkdownConverter + Send + Sync + 'static, impl ContentFinder + Send + Sync>,
>,
>,
) -> tide::Result {
let state = req.state();
let (contents, _hash) = state
.content_finder
.content_for("README.md")
.with_status(|| StatusCode::NotFound)?;
let converted = state.markdown_converter.convert_markdown(&contents).await?;
let resp = base_html("README.md", &markdown_html("README.md", &converted));
Ok(Response::builder(StatusCode::Ok)
.body(resp)
.content_type(mime::HTML)
.build())
}
/// Renders any given file path containing markdown as HTML.
async fn render_markdown_path(
req: Request<
Arc<
State<impl MarkdownConverter + Send + Sync + 'static, impl ContentFinder + Send + Sync>,
>,
>,
) -> tide::Result {
let state = req.state();
let path = req.url().path();
let file = path.split('/').last().unwrap_or("rs-readme");
let (contents, _hash) = state.content_finder.content_for(&format!(".{}", path))?;
let converted = state.markdown_converter.convert_markdown(&contents).await?;
let resp = base_html(file, &markdown_html(file, &converted));
Ok(Response::builder(StatusCode::Ok)
.body(resp)
.content_type(mime::HTML)
.build())
}
/// Sends an event periodically with the file contents and the SHA1 of the contents.
/// The front end will update if the hash differs.
async fn render_page_update(
req: Request<
Arc<State<impl MarkdownConverter + Send + Sync, impl ContentFinder + Send + Sync>>,
>,
sender: Sender,
) -> Result<(), http_types::Error> {
let state = req.state();
let path = &req.url().path()["/__rs-readme".len()..];
let (contents, hash) = if path == "/" {
state.content_finder.content_for("./README.md")?
} else {
state.content_finder.content_for(&format!(".{}", path))?
};
let converted = state.markdown_converter.convert_markdown(&contents).await?;
let message = json!({
"contents": &converted,
"hash": &format!("{:x}", &hash),
});
sender.send("update", &message.to_string(), None).await?;
Ok(())
}
struct ErrorMiddleware {}
impl ErrorMiddleware {
fn not_markdown(&self, path: &str) -> tide::Result {
Ok(Response::builder(StatusCode::BadRequest)
.body(not_markdown_html("rs-readme", path))
.content_type(mime::HTML)
.build())
}
fn not_found(&self, resource: &str) -> tide::Result {
Ok(Response::builder(StatusCode::NotFound)
.body(file_not_found("rs-readme", resource))
.content_type(mime::HTML)
.build())
}
}
#[async_trait]
impl<State: Clone + Send + Sync + 'static> Middleware<State> for ErrorMiddleware {
async fn handle(&self, req: Request<State>, next: Next<'_, State>) -> tide::Result {
let url = req.url().clone();
let res = next.run(req).await;
if let Some(err) = res.downcast_error::<ContentError>() {
match err {
ContentError::NotMarkdown => self.not_markdown(url.path()),
ContentError::CouldNotFetch(resource) => self.not_found(resource),
}
} else {
Ok(res)
}
}
}
/// Builds a `tide::Server` with the appropriate endpoint mappings.
pub fn build_app(
state: Arc<
State<
impl MarkdownConverter + Send + Sync + 'static,
impl ContentFinder + Send + Sync + 'static,
>,
>,
) -> Server<Arc<State<impl MarkdownConverter, impl ContentFinder>>> {
let mut app = Server::with_state(state);
app.with(log::LogMiddleware::new());
app.with(ErrorMiddleware {});
app.at("").get(render_readme);
app.at("/static/octicons/:file").get(static_files::octicons);
app.at("/static/style.css").get(static_files::style);
app.at("/__rs-readme/")
.get(tide::sse::endpoint(render_page_update));
app.at("/__rs-readme/*")
.get(tide::sse::endpoint(render_page_update));
app.at("/*").get(render_markdown_path);
app
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_base_html() {
let expected = "\
<!DOCTYPE html>\
<html>\
<head>\
<link rel=\"stylesheet\" href=\"/static/octicons/octicons.css\">\
<link rel=\"stylesheet\" href=\"https://github.githubassets.com/assets/frameworks-146fab5ea30e8afac08dd11013bb4ee0.css\">\
<link rel=\"stylesheet\" href=\"https://github.githubassets.com/assets/site-897ad5fdbe32a5cd67af5d1bdc68a292.css\">\
<link rel=\"stylesheet\" href=\"https://github.githubassets.com/assets/github-c21b6bf71617eeeb67a56b0d48b5bb5c.css\">\
<link rel=\"stylesheet\" href=\"/static/style.css\">\
<title>test title</title>\
<script>let hash = '';
let event = new EventSource(`//${location.host}/__rs-readme${location.pathname}`);
event.addEventListener('update', (e) => {
let message = JSON.parse(e.data);
if (message.hash !== hash) {
hash = message.hash;
document.getElementById('rs-readme-content').innerHTML = message.contents;
}
});</script>\
</head>\
<body>\
Test content\
</body>\
</html>";
let actual = base_html("test title", "Test content");
assert_eq!(expected, actual);
}
#[test]
fn test_markdown_html() {
let expected = "\
<div class=\"page\">\
<div id=\"preview-page\" class=\"preview-page\">\
<div role=\"main\" class=\"main-content\">\
<div class=\"container new-discussion-timeline experiment-repo-nav\">\
<div class=\"repository-content\">\
<div id=\"readme\" class=\"readme boxed-group clearfix announce instapaper_body md\">\
<h3>\
<span class=\"octicon octicon-book\"></span> \
file_name.md\
</h3>\
<article id=\"rs-readme-content\" class=\"markdown-body entry-content\" itemprop=\"text\">\
Test content\
</article>\
</div>\
</div>\
</div>\
</div>\
</div>\
<div> </div>\
</div>";
let actual = markdown_html("file_name.md", "Test content");
assert_eq!(expected, actual);
}
#[test]
fn test_not_markdown_html() {
let expected = "\
<!DOCTYPE html>\
<html>\
<head><title>rs-readme</title></head>\
<body>\
<h1>Not a Markdown File</h1>\
<p><strong>test_file</strong> is not a markdown file and cannot be rendered</p>\
</body>\
</html>\
";
let actual = not_markdown_html("rs-readme", "test_file");
assert_eq!(expected, actual);
}
}