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 std::collections::HashMap;
use crate::{errors::HandlerError, providers::SuggestionProviderRef};
use actix_web::{
get, post,
web::{self, Data, Json},
HttpResponse,
};
use merino_settings::{Settings, SuggestionProviderConfig};
use merino_suggest_providers::IdMultiProviderDetails;
use serde::Serialize;
pub fn configure(config: &mut web::ServiceConfig) {
config
.service(list_providers)
.service(reconfigure_providers);
}
#[get("")]
async fn list_providers(
provider: Data<SuggestionProviderRef>,
) -> Result<HttpResponse, HandlerError> {
let id_provider = &provider.provider.read().await;
let providers = id_provider
.list_providers()
.into_iter()
.map(|p| (p.id.clone(), p))
.collect();
Ok(HttpResponse::Ok()
.append_header(("Cache-Control", "public, max-age=900".to_string()))
.json(ListResponse { providers }))
}
#[derive(Debug, Serialize)]
struct ListResponse {
providers: HashMap<String, IdMultiProviderDetails>,
}
#[post("reconfigure")]
async fn reconfigure_providers(
config: Json<HashMap<String, SuggestionProviderConfig>>,
provider: Data<SuggestionProviderRef>,
settings: Data<Settings>,
) -> Result<HttpResponse, HandlerError> {
if settings.debug {
provider
.reconfigure(config.clone())
.await
.map_err(|_| HandlerError::internal())?;
Ok(HttpResponse::NoContent().body(""))
} else {
Ok(HttpResponse::NotFound().body(""))
}
}