summaryrefslogtreecommitdiffstats
path: root/src/server.rs
blob: f2df45cb087026ad8099c450a678e9acd3a1059f (plain)
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
use std::path::PathBuf;

use anyhow::Error;
use anyhow::Result;
use pidlock::{Pidlock, PidlockState};
use actix_web::HttpResponse;
use actix_web::Responder;
use actix_web::http::StatusCode;
use actix_web::body::Body;

use crate::cli::*;
use crate::types::util::*;

pub fn mk_lock() -> Pidlock {
    pidlock::Pidlock::new("/tmp/distrox_server.pid")
}

pub fn do_start(cli: &CLI) -> bool {
    cli.cmd().map(|cmd| Command::Server == *cmd).unwrap_or(false)
}

pub fn is_running(server_lock: &Pidlock) -> bool {
    PathBuf::from("/tmp/distrox_server.pid").exists()
}


pub async fn run_server(mut server_lock: Pidlock, adr: String) -> Result<()> {
    info!("Starting server");
    let _ = server_lock.acquire().map_err(|_| anyhow!("Error while getting the PID lock"))?;

    info!("Got PID lock for server");
    actix_web::HttpServer::new(|| {
        actix_web::App::new()
            .route("*", actix_web::web::get().to(index))
    })
    .bind(adr.clone())
    .expect(&format!("Could not bind to address {}", adr))
    .run()
    .await;

    info!("Server shutdown");
    info!("Releasing PID lock for server");
    server_lock.release().map_err(|_| anyhow!("Error while releasing the PID lock"))
}

async fn index() -> impl Responder {
    debug!("serve index");
    let s = format!("{pre}{style}{index}{post}",
        pre   = include_str!("../assets/index_pre.html"),
        style = include_str!("../assets/style.css"),
        index = include_str!("../assets/index.html"),
        post  = include_str!("../assets/index_post.html"),
    );

    HttpResponse::build(StatusCode::OK).body(Body::from_slice(s.as_bytes()))
}