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
73
74
75
76
77
78
79
use serde_derive::{Deserialize, Serialize};
use std::env;
use std::fs::File;
use std::io::BufReader;

use super::log_exit;
use crate::file::get_jinx_files;

#[derive(Debug, Deserialize, Serialize, Clone, std::cmp::PartialEq)]
pub struct JinxService {
  pub name: String,
  pub domain: String,
  pub image_name: String,
  pub image_port: u64,
  pub image_env: Option<Vec<String>>,
  pub image_secrets: Option<Vec<String>>,
  pub image_volumes: Option<Vec<String>>,
  pub published_port: Option<i64>,
  pub https_redirect: bool,
  pub https: bool,
}

impl Default for JinxService {
  fn default() -> Self {
    Self {
      name: "None".to_string(),
      domain: "None".to_string(),
      image_name: "None".to_string(),
      image_port: 8080,
      image_env: None,
      image_secrets: None,
      image_volumes: None,
      published_port: None,
      https_redirect: false,
      https: false,
    }
  }
}

// returns Option<JinxService>
pub fn get_jinx_service() -> JinxService {
  // get current directory
  let current_dir = env::current_dir().expect("[JINX] Failed to get current directory");

  // attempt to open jinx.json in current directory
  let jinx_path = format!("{}/jinx.json", current_dir.display());
  let file = match File::open(jinx_path) {
    Err(err) => log_exit!("[SERVICE] Failed to open jinx.json {}", err),
    Ok(file) => file,
  };

  // read the file
  let reader = BufReader::new(file);

  // parse jinx.json into a JinxService
  let service = match serde_json::from_reader(reader) {
    Err(err) => log_exit!("[SERVICE] Failed to parse jinx.json {}", err),
    Ok(file) => file,
  };

  service
}

pub fn get_jinx_proxy_service() -> JinxService {
  let jinx_files = get_jinx_files();

  let conf = format!("{}:/etc/letsencrypt", jinx_files.letsencrypt_conf);
  let www = format!("{}:/var/www/certbot", jinx_files.letsencrypt_www);
  let volumes = vec![conf, www];

  JinxService {
    name: "jinx_proxy".to_string(),
    image_name: "jinx_proxy".to_string(),
    image_port: 80,
    image_volumes: Some(volumes),
    published_port: Some(80),
    ..Default::default()
  }
}