summaryrefslogtreecommitdiff
path: root/angelsharkd/src/routes/extensions/simple_search.rs
blob: cf75c606eb66be02f14c55b1d6e5542e3a56297b (plain) (blame)
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
use crate::{config::Config, routes::with_runner};
use cached::proc_macro::cached;
use libangelshark::AcmRunner;
use std::convert::Infallible;
use warp::{
    body::{content_length_limit, json},
    get, path, post, Filter, Rejection, Reply,
};

const CMD_LIST_EXT: &str = "list extension-type";
const CMD_LIST_STAT: &str = "list station";

type Terms = Vec<String>;

pub fn search(config: &Config) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
    let runner = config.runner.clone();
    // TODO: anti-caching headers on resp?

    path("search")
        .and(post())
        .and(content_length_limit(1024 * 16))
        .and(json::<Terms>())
        .and(with_runner(runner))
        .and_then(handle_simple_search)
}

pub fn refresh(config: &Config) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
    let runner = config.runner.clone();
    path!("search" / "refresh")
        .and(get())
        .and(with_runner(runner))
        .and_then(handle_refresh)
}

async fn handle_refresh(runner: AcmRunner) -> Result<impl Reply, Infallible> {
    get_extensions_cached(true);
    Ok("Refresh scheduled")
}

async fn handle_simple_search(terms: Terms, runner: AcmRunner) -> Result<impl Reply, Infallible> {
    Ok(get_extensions_cached(false))
}

#[cached]
fn get_extensions_cached(refresh: bool) -> String {
    if refresh {
        tokio::spawn(async move {
            std::thread::sleep_ms(10000);

            if let Ok(mut handle) = GET_EXTENSIONS_CACHED.lock() {
                handle.cache_clear();
                handle.cache_set(false, String::from("fresh"));
                eprintln!("evicted");
            }
        });
    }

    String::from("stale")
}

fn get_extensions(runner: AcmRunner) {
    todo!()
}