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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
use serde_derive::{Deserialize, Serialize};
use serde_json::json;
use std::fs;
use std::fs::File;
use std::io::BufReader;
use std::io::ErrorKind;

use super::log_exit;
use crate::file::{get_jinx_files, JinxFiles};
use crate::service::JinxService;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct JinxConf {
  pub nginx_user: String,
  pub nginx_worker_processes: u8,
  pub nginx_worker_connections: u16,
  pub jinx_services: Vec<JinxService>,
}

impl Default for JinxConf {
  fn default() -> Self {
    Self {
      nginx_user: "nginx".to_string(),
      nginx_worker_processes: 1,
      nginx_worker_connections: 1024,
      jinx_services: vec![],
    }
  }
}

pub fn open_jinx_conf(jinx_files: &JinxFiles) -> File {
  // try to open jinx_conf
  let jinx_conf_file = match File::open(&jinx_files.jinx_conf) {
    Err(err) => {
      // create default jinx_conf if not found
      if err.kind() == ErrorKind::NotFound {
        // create jinx directory
        let _dir = match fs::create_dir_all(&jinx_files.jinx_home) {
          Err(err) => log_exit!("[JINX] Failed to create jinx directory", err),
          Ok(dir) => dir,
        };

        // create default jinx_conf
        let jinx_conf = JinxConf {
          ..Default::default()
        };
        let json = json!(jinx_conf);

        // write file
        fs::write(&jinx_files.jinx_conf, &json.to_string().as_bytes())
          .expect("[CONF] Failed to write jinx_conf");

        // return file
        match File::open(&jinx_files.jinx_conf) {
          Err(err) => log_exit!("[CONF] Failed to open jinx_conf", err),
          Ok(file) => file,
        }
      } else {
        log_exit!("[CONF] Failed to open jinx_conf", err)
      }
    }
    Ok(file) => file,
  };

  jinx_conf_file
}

// returns JinxConf
pub fn get_jinx_conf() -> JinxConf {
  // get jinx files
  let jinx_files = get_jinx_files();

  // open jinx conf
  let jinx_conf_file = open_jinx_conf(&jinx_files);

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

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

  jinx_conf
}

// writes JinxConf to jinx_conf file
pub fn write_jinx_conf(jinx_conf: &JinxConf) {
  // get jinx files
  let jinx_files = get_jinx_files();

  // ensure path exists
  let _jinx_conf_file = open_jinx_conf(&jinx_files);

  // convert jinx_conf to JSON
  let json = json!(jinx_conf);

  // write the file
  fs::write(&jinx_files.jinx_conf, &json.to_string().as_bytes())
    .expect("[CONF] Failed to write jinx_conf");
}