about summary refs log tree commit diff stats
path: root/src/smd/endpoints.rs
blob: 48c9720435e1e35a0fe504611c739b7f4ab421d2 (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
use super::{
    Config,
    context::{SalarymanDContext, StdinBuffer},
};
use dropshot::{HttpError, HttpResponseOk, RequestContext, TypedBody, endpoint};
use std::sync::Arc;

#[endpoint {
    method = GET,
    path = "/config",
}]
pub async fn endpoint_get_config(
    rqctx: RequestContext<Arc<SalarymanDContext>>,
) -> Result<HttpResponseOk<Config>, HttpError> {
    Ok(HttpResponseOk(rqctx.context().config.clone()))
}

#[endpoint {
    method = PUT,
    path = "/services/write"
}]
pub async fn endpoint_post_stdin(
    rqctx: RequestContext<Arc<SalarymanDContext>>,
    update: TypedBody<StdinBuffer>,
) -> Result<HttpResponseOk<()>, HttpError> {
    let ctx = rqctx.context();
    let stdin_str = update.into_inner();
    for i in 0..ctx.service.len() {
        let mut lock = ctx.service[i].lock().await;
        if lock.started().await {
            lock.writeln_stdin(stdin_str.string.clone()).await.unwrap(); //TODO: PROPERLY HANDLE ERROR!
        }
        drop(lock);
    }

    Ok(HttpResponseOk(()))
}