~neon/activity-graph

ref: 244b37d839bc92e47a1faff52fcb2c0d79fc53fb activity-graph/src/server.rs -rw-r--r-- 5.3 KiB
244b37d8Jens Pitkanen Add -q 2 years ago
                                                                                
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
use hyper::service::{make_service_fn, service_fn};
use hyper::header::{HeaderValue, CONTENT_TYPE};
use hyper::{Body, Request, Response, Server, StatusCode};
use tokio::runtime::Runtime;
use tokio::task;

use std::convert::Infallible;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::RwLock;
use std::time::{Duration, Instant};

use crate::{generate_years, log, render, ExternalResources, GenerationData};

lazy_static::lazy_static! {
    // These are set before the server is run, and only used in responses
    static ref GENERATION_DATA: RwLock<GenerationData> = RwLock::new(GenerationData::default());
    static ref EXTERNAL_HTML: RwLock<ExternalResources> = RwLock::new(ExternalResources::default());
    static ref LAST_CACHE: RwLock<Instant> = RwLock::new(Instant::now());
    static ref CACHE_LIFETIME: RwLock<Duration> = RwLock::new(Duration::from_secs(0));

    static ref CACHED_HTML: RwLock<String> = RwLock::new(String::new());
    static ref CACHED_CSS: RwLock<String> = RwLock::new(String::new());
}

static REFRESHING_CACHE: AtomicBool = AtomicBool::new(false);
static CACHE_INITIALIZED: AtomicBool = AtomicBool::new(false);

static INDEX_PATHS: &[&str] = &["/", "/index.html", "/index.htm", ""];

pub fn run(gen: &GenerationData, ext: &ExternalResources, host: SocketAddr, cache_lifetime: u64) {
    log::verbose_println(&format!("starting server on {}...", host), true);

    if let (Ok(mut gen_), Ok(mut ext_), Ok(mut lifetime), Ok(mut last_cache)) = (
        GENERATION_DATA.write(),
        EXTERNAL_HTML.write(),
        CACHE_LIFETIME.write(),
        LAST_CACHE.write(),
    ) {
        *gen_ = gen.clone();
        *ext_ = ext.clone();
        *lifetime = Duration::from_secs(cache_lifetime);
        *last_cache = Instant::now() - Duration::from_secs(cache_lifetime * 2);
    } else {
        unreachable!();
    }

    match Runtime::new() {
        Ok(mut runtime) => {
            runtime.block_on(async {
                let make_service =
                    make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });
                let server = Server::bind(&host).serve(make_service);
                log::println(&format!("server started on {}", host));
                if let Err(err) = server.await {
                    log::println(&format!("error: hyper server encountered an error: {}", err));
                }
            });
        }
        Err(err) => {
            log::println(&format!("error: could not start tokio runtime: {}", err));
        }
    }
}

async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let (cache, mime_type) = if INDEX_PATHS.contains(&req.uri().path()) {
        refresh_caches().await;
        (CACHED_HTML.read(), HeaderValue::from_static("text/html"))
    } else if req.uri() == "/activity-graph.css" {
        refresh_caches().await;
        (CACHED_CSS.read(), HeaderValue::from_static("text/css"))
    } else {
        return Ok(error_response("404 Not Found", StatusCode::NOT_FOUND));
    };
    if let Ok(cache) = cache {
        let mut response = Response::new(Body::from(cache.clone()));
        response.headers_mut().insert(CONTENT_TYPE, mime_type);
        Ok(response)
    } else {
        Ok(error_response(
            "500 Internal Server Error\nSorry, the server encountered an unexpected error.",
            StatusCode::INTERNAL_SERVER_ERROR,
        ))
    }
}

fn error_response(s: &'static str, status_code: StatusCode) -> Response<Body> {
    let mut response = Response::new(Body::from(s));
    *response.status_mut() = status_code;
    response
}

async fn refresh_caches() {
    let task = task::spawn_blocking(|| {
        let refresh_time = {
            let last_cache = LAST_CACHE.read().unwrap();
            let lifetime = CACHE_LIFETIME.read().unwrap();
            *last_cache + *lifetime
        };
        if Instant::now() >= refresh_time
            && !REFRESHING_CACHE.compare_and_swap(false, true, Ordering::Relaxed)
        {
            log::verbose_println("refreshing cache...", false);
            let start = Instant::now();
            if let (Ok(gen), Ok(ext)) = (GENERATION_DATA.read(), EXTERNAL_HTML.read()) {
                let years = generate_years(&gen);
                let html_path = PathBuf::from("/index");
                let css_path = PathBuf::from("/activity-graph.css");
                let output_html = render::html(&ext, &html_path, Some(&css_path), &years);
                let output_css = render::css(&ext);
                if let Ok(mut html) = CACHED_HTML.write() {
                    *html = output_html;
                }
                if let Ok(mut css) = CACHED_CSS.write() {
                    *css = output_css;
                }
                if let Ok(mut last_cache) = LAST_CACHE.write() {
                    *last_cache = Instant::now();
                }
            }
            log::println(
                &format!("updated cache, took {:?}", Instant::now() - start),
            );
            REFRESHING_CACHE.store(false, Ordering::Relaxed);
            CACHE_INITIALIZED.store(true, Ordering::Relaxed);
        }
    });

    // If the cache hasn't been initialized yet, wait for the refresh
    // to run by `await`ing it.
    if !CACHE_INITIALIZED.load(Ordering::Relaxed) {
        let _ = task.await;
    }
}