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
use std::io;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use chrono::{Datelike, Utc};
use ntex::http::Response;

use ntex::web;
use ntex::web::Error;
use ntex::web::types::Path;
use ntex_files::NamedFile;
use ramhorns::{Content, Template};
use crate::config::Config;
use crate::post::Post;
use crate::post_cache::PostCache;
use crate::post_list::PostList;
use crate::post_render::render_post;
use crate::text_utils::format_date_time;

#[derive(Content)]
struct IndexPage {
    years_developing: i64,
    post_count: i64,
    days_since_started: i64,
}

#[derive(Content)]
struct ListPage {
    post_list: Vec<PostItem>,
}

#[derive(Content)]
struct PostItem {
    date: String,
    time: String,
    link: String,
    title: String,
    summary: String,
}

struct AppState {
    activity_start_year: i32,
    posts: PostCache,
    pages: PostCache,
    config: Config,
}

fn get_posts(root_dir: &PathBuf, post_file: &str) -> io::Result<Vec<Post>> {
    let root_dir = root_dir.clone();
    let post_list = PostList {
        root_dir,
        post_file: post_file.to_string(),
    };

    let dirs = post_list.retrieve_dirs()?;
    let mut posts = vec![];
    for dir in dirs.as_slice() {
        let p = dir.join(&post_list.post_file);
        let post = Post::from(&p, true)?;
        posts.push(post);
    }

    // Retrieve files in post directory
    let md_posts: Vec<PathBuf> = post_list.retrieve_files()?;
    for post_file in md_posts {
        let post = Post::from(&post_file, true)?;
        posts.push(post);
    }

    Ok(posts)
}

fn process_post(path: Path<String>, template_dir: &PathBuf, template_name: &str, posts: &PostCache) -> Result<Response, Response> {
    let view_tpl_src: String = match read_template(template_dir, template_name) {
        Ok(s) => s,
        Err(e) => {
            return Err(web::HttpResponse::InternalServerError()
                .body(format!("Error loading post view template: {}", e)));
        }
    };

    // TODO: Cache renderer?
    let view_tpl = match Template::new(view_tpl_src) {
        Ok(x) => x,
        Err(e) => {
            return Err(web::HttpResponse::InternalServerError()
                .body(format!("Error parsing post view template: {}", e)));
        }
    };

    let path = path.into_inner();
    let post_summary = match posts.from_link(&path) {
        Some(post) => post,
        None => return Err(web::HttpResponse::InternalServerError()
            .body(format!("Error loading post with link: {}", &path))),
    };

    let post = match Post::from(&post_summary.header.file_name, false) {
        Ok(post) => post,
        Err(e) => {
            return Err(web::HttpResponse::InternalServerError()
                .body(format!("Error loading post content: {}", e)));
        }
    };

    let html = match render_post(&post.content, None) {
        Ok(post) => post,
        Err(e) => {
            return Err(web::HttpResponse::InternalServerError()
                .body(format!("Error rendering post content: {}", e)));
        }
    };

    let (date, time) = format_date_time(&post.header.date);

    let rendered = view_tpl.render(&ViewItem {
        errors: vec![],
        id: post.header.id.as_str(),
        author: post.header.author.as_str(),
        date: date.as_str(),
        time: time.as_str(),
        post_title: post.title.as_str(),
        post_content: html.as_str(),
    });

    Ok(web::HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(&rendered))
}

fn read_template(tpl_dir: &PathBuf, file_name: &str) -> Result<String, io::Error> {
    let full_path = tpl_dir.join(file_name);
    std::fs::read_to_string(full_path)
}

fn get_file(root_dir: &PathBuf, post: String, file: String) -> Result<NamedFile, Error> {
    if post.contains("../") || file.contains("../") {
        return Err(web::error::ErrorUnauthorized("Access forbidden").into());
    }

    let file_path = root_dir.join(post).join(file);
    Ok(NamedFile::open(file_path)?)
}

// Begin: Redirect region --------
#[web::get("/view/{post}")]
async fn view_wo_slash(path: web::types::Path<String>) -> web::HttpResponse {
    web::HttpResponse::TemporaryRedirect()
        .header("Location", path.into_inner() + "/")
        .content_type("text/html; charset=utf-8")
        .finish()
}

#[web::get("/page/{post}")]
async fn page_wo_slash(path: web::types::Path<String>) -> web::HttpResponse {
    web::HttpResponse::TemporaryRedirect()
        .header("Location", path.into_inner() + "/")
        .content_type("text/html; charset=utf-8")
        .finish()
}
// End: Redirect region --------

#[web::get("/page/{page}/")]
async fn page(path: web::types::Path<String>,
              state: web::types::State<Arc<Mutex<AppState>>>,
) -> web::HttpResponse {
    let state = state.lock().unwrap();
    let template_dir = &state.config.paths.template_dir;
    let posts: &PostCache = &state.pages;

    match process_post(path, template_dir, "page.tpl", posts) {
        Ok(value) => value,
        Err(value) => return value,
    }
}

#[web::get("/view/{post}/")]
async fn view(path: web::types::Path<String>,
              state: web::types::State<Arc<Mutex<AppState>>>,
) -> web::HttpResponse {
    let state = state.lock().unwrap();
    let template_dir = &state.config.paths.template_dir;
    let posts: &PostCache = &state.posts;

    match process_post(path, template_dir, "view.tpl", posts) {
        Ok(value) => value,
        Err(value) => return value,
    }
}

#[web::get("/list")]
async fn list(state: web::types::State<Arc<Mutex<AppState>>>) -> web::HttpResponse {
    let state = &state.lock().unwrap();

    let tpl_dir = &state.config.paths.template_dir;
    let list_tpl_src: String = match read_template(tpl_dir, "postlist.tpl") {
        Ok(s) => s,
        Err(e) => {
            return web::HttpResponse::InternalServerError()
                .body(format!("Error loading postlist template: {}", e));
        }
    };

    let list_tpl = match Template::new(list_tpl_src) {
        Ok(x) => x,
        Err(e) => {
            return web::HttpResponse::InternalServerError()
                .body(format!("Error parsing postlist template: {}", e));
        }
    };

    // TODO: Implement multiple readers, single writer or remove lock
    let mut post_list = vec![];
    {
        let cache = &state.posts;

        for (_, uuid) in cache.post_list.iter() {
            let post_item = cache.posts.get(uuid.as_str()).unwrap();
            let post_link = format!("view/{}/", post_item.link);
            let post = &post_item.post;
            let html = match render_post(post.content.as_str(), Some(post_link.as_str())) {
                Ok(html) => html,
                Err(e) => return web::HttpResponse::InternalServerError()
                    .body(format!("Error rendering post: {}", e)),
            };

            let (date, time) = format_date_time(&post.header.date);
            let post_item = PostItem {
                date: date.to_string(),
                time: time.to_string(),
                link: post_link,
                title: post.title.clone(),
                summary: html,
            };
            post_list.push(post_item);
        }
    }

    let rendered = list_tpl.render(&ListPage {
        post_list
    });

    web::HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(rendered)
}

#[derive(Content)]
struct ViewItem<'a> {
    errors: Vec<String>,
    id: &'a str,
    author: &'a str,
    date: &'a str,
    time: &'a str,
    post_title: &'a str,
    post_content: &'a str,
}

#[web::get("/view/{post}/{file}")]
async fn post_files(path: web::types::Path<(String, String)>,
                    state: web::types::State<Arc<Mutex<AppState>>>,
) -> Result<NamedFile, web::Error> {
    let (post, file) = path.into_inner();
    let state = state.lock().unwrap();
    get_file(&state.config.paths.posts_dir, post, file)
}

#[web::get("/page/{post}/{file}")]
async fn page_files(path: web::types::Path<(String, String)>,
                    state: web::types::State<Arc<Mutex<AppState>>>,
) -> Result<NamedFile, web::Error> {
    let (post, file) = path.into_inner();
    let state = state.lock().unwrap();
    get_file(&state.config.paths.pages_dir, post, file)
}

#[web::get("/public/{file_name}")]
async fn public_files(path: web::types::Path<String>, state: web::types::State<Arc<Mutex<AppState>>>) -> Result<NamedFile, web::Error> {
    if path.contains("../") {
        return Err(web::error::ErrorUnauthorized("Access forbidden").into());
    }

    let state = state.lock().unwrap();
    let file_path = state.config.paths.public_dir.join(path.into_inner());

    Ok(NamedFile::open(file_path)?)
}

#[web::get("/")]
async fn index(req: web::HttpRequest, state: web::types::State<Arc<Mutex<AppState>>>) -> web::HttpResponse {
    let state = state.lock().unwrap();
    let index_tpl_src: String = match read_template(&state.config.paths.template_dir, "index.tpl") {
        Ok(s) => s,
        Err(e) => {
            return web::HttpResponse::InternalServerError()
                .body(format!("Error loading index template: {}", e));
        }
    };

    let index_tpl = match Template::new(index_tpl_src) {
        Ok(x) => x,
        Err(e) => {
            return web::HttpResponse::InternalServerError()
                .body(format!("Error parsing index template: {}", e));
        }
    };

    let cache = &state.posts;
    let days_since_first_post = if cache.post_list.is_empty() {
        0
    } else {
        let (date, _) = cache.post_list.last().unwrap();
        let res = Utc::now().naive_utc().signed_duration_since(date.clone());
        res.num_days()
    };
    let years_developing = (Utc::now().year() - state.activity_start_year) as i64;

    let rendered = index_tpl.render(&IndexPage {
        years_developing,
        post_count: cache.posts.len() as i64,
        days_since_started: days_since_first_post,
    });

    let mut referer: String = match req.headers().get("referer") {
        Some(v) => v.to_str().unwrap().to_string(),
        None => "http://sei-la/".to_string(),
    };

    if !referer.ends_with("/") {
        referer += "/";
    }

    web::HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(rendered)
}

pub async fn server_run(config: Config) -> io::Result<()> {
    let md_posts = match get_posts(&config.paths.posts_dir, config.defaults.index_file_name.as_str()) {
        Ok(posts) => posts,
        Err(err) => {
            return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Error retrieving post list template: {}. Dir={}", err, config.paths.posts_dir.to_str().unwrap())));
        }
    };

    let md_pages = match get_posts(&config.paths.pages_dir, config.defaults.index_file_name.as_str()) {
        Ok(posts) => posts,
        Err(err) => {
            return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Error retrieving post list template: {}. Dir={}", err, config.paths.posts_dir.to_str().unwrap())));
        }
    };


    let bind_addr = config.server.address.clone();
    let bind_port = config.server.port;
    let app_state = Arc::new(Mutex::new(AppState {
        activity_start_year: config.personal.activity_start_year,
        posts: PostCache::new(config.defaults.index_file_name.as_str()),
        pages: PostCache::new(config.defaults.index_file_name.as_str()),
        config,
    }));

    {
        let state = &mut app_state.lock().unwrap();
        let post_cache = &mut state.posts;
        for post in md_posts {
            post_cache.add(post)?;
        }
        post_cache.sort();

        let page_cache = &mut state.pages;
        for post in md_pages {
            page_cache.add(post)?;
        }
        page_cache.sort();
    }

    web::HttpServer::new(move || {
        web::App::new()
            .state(app_state.clone())
            .service(index)
            .service(public_files)
            .service(list)
            .service(view)
            .service(view_wo_slash)
            .service(post_files)
            .service(page)
            .service(page_wo_slash)
            .service(page_files)
    })
        .bind((bind_addr, bind_port))?
        .run()
        .await
}