jinx_proxy/
service.rs

1use serde_derive::{Deserialize, Serialize};
2use std::env;
3use std::fs::File;
4use std::io::BufReader;
5
6use super::log_exit;
7use crate::file::get_jinx_files;
8
9#[derive(Debug, Deserialize, Serialize, Clone, std::cmp::PartialEq)]
10pub struct JinxService {
11  pub name: String,
12  pub domain: String,
13  pub image_name: String,
14  pub image_port: i64,
15  pub image_envs: Option<Vec<String>>,
16  pub image_secrets: Option<Vec<String>>,
17  pub image_volumes: Option<Vec<String>>,
18  pub published_port: Option<i64>,
19  pub https_redirect: bool,
20  pub https: bool,
21}
22
23impl Default for JinxService {
24  fn default() -> Self {
25    Self {
26      name: "None".to_string(),
27      domain: "None".to_string(),
28      image_name: "None".to_string(),
29      image_port: 8080,
30      image_envs: None,
31      image_secrets: None,
32      image_volumes: None,
33      published_port: None,
34      https_redirect: false,
35      https: false,
36    }
37  }
38}
39
40// returns Option<JinxService>
41pub fn get_jinx_service() -> JinxService {
42  // get current directory
43  let current_dir = env::current_dir().expect("[JINX] Failed to get current directory");
44
45  // attempt to open jinx.json in current directory
46  let jinx_path = format!("{}/jinx.json", current_dir.display());
47  let file = match File::open(jinx_path) {
48    Err(err) => log_exit!("[SERVICE] Failed to open jinx.json {}", err),
49    Ok(file) => file,
50  };
51
52  // read the file
53  let reader = BufReader::new(file);
54
55  // parse jinx.json into a JinxService
56  let service = match serde_json::from_reader(reader) {
57    Err(err) => log_exit!("[SERVICE] Failed to parse jinx.json {}", err),
58    Ok(file) => file,
59  };
60
61  service
62}
63
64pub fn get_jinx_proxy_service() -> JinxService {
65  let jinx_files = get_jinx_files();
66
67  let conf = format!("{}:/etc/letsencrypt", jinx_files.letsencrypt_conf);
68  let www = format!("{}:/var/www/certbot", jinx_files.letsencrypt_www);
69  let volumes = vec![conf, www];
70
71  JinxService {
72    name: "jinx_proxy".to_string(),
73    image_name: "jinx_proxy".to_string(),
74    image_port: 80,
75    image_volumes: Some(volumes),
76    published_port: Some(80),
77    ..Default::default()
78  }
79}