aboutsummaryrefslogtreecommitdiff
path: root/src/acme_actor.rs
blob: cd41b1fe6d9dc4c39efdcd922237fcb7e92b829e (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
64
65
66
67
68
69
70
71
72
use anyhow::Result;
use log::*;
use tokio::{
  select,
  sync::watch,
  time::{self, Duration},
};

use crate::config::RuntimeConfigAcme;
use crate::messages;

pub struct AcmeActor {
  email: String,
  //last_ports: messages::PublicExposedPorts,
  refresh: Duration,

  rx_ports: watch::Receiver<messages::PublicExposedPorts>,
}

impl AcmeActor {
  pub async fn new(
    config: Option<RuntimeConfigAcme>,
    rxp: &watch::Receiver<messages::PublicExposedPorts>,
  ) -> Result<Option<Self>> {
    if config.is_none() {
      return Ok(None);
    }
    let config = config.unwrap();

    let ctx = Self {
      email: config.email,
      //last_ports: messages::PublicExposedPorts::new(),
      refresh: config.refresh_time,
      rx_ports: rxp.clone(),
    };

    Ok(Some(ctx))
  }

  pub async fn listen(&mut self) -> Result<()> {
    let mut interval = time::interval(self.refresh);
    loop {
      select! {
        Some(ports) = self.rx_ports.recv() => {
          match self.do_acme(ports).await {
            Ok(()) => debug!("Successfully updated ACME"),
            Err(e) => error!("An error occured while updating ACME. {}", e),
          }
        },
        _ = interval.tick() => continue,
        else => break // Sender dropped, terminate loop.
      }
    }

    Ok(())
  }

  pub async fn do_acme(&self, ports: messages::PublicExposedPorts) -> Result<()> {
    if ports.acme.is_empty() {
      return Ok(());
    }

    let primary_url = &ports.acme[0];
    let secondary_urls = &ports.acme[1..];

    println!("Doing ACME!!!");
    println!("Primary URL: {:?}", primary_url);
    println!("Secondary URLs: {:?}", secondary_urls);

    Ok(())
  }
}