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
103
104
105
106
107
108
#![doc(html_favicon_url = "https://kwatafana.org/logo.jpeg")]
#![doc(html_logo_url = "https://kwatafana.org/logo.jpeg")]
#![warn(missing_docs, unreachable_pub, future_incompatible, rust_2018_idioms)]
mod defaults;
mod error;
mod error_response;
pub mod inputs;
mod konfig;
mod kontrol;
mod kroute;
pub mod log;
pub mod validate;
pub use error::KError;
pub use error_response::ErrorResponse;
pub use konfig::Konfig;
pub use kontrol::Kontrol;
pub use kroute::{kroute, Method};
pub use krypto;
pub use rouille as server;
pub use serde_json::{
error::Error as JsonError, from_str as json_from_str, json, Value as JsonValue,
};
use krypto::kpassport::Kpassport;
use std::fs::File;
pub struct Kong {
pub config: Konfig,
pub kpassport: Option<Kpassport>,
pub input: Option<serde_json::Value>,
}
impl Kong {
fn init(config: &Konfig) {
Kong::create_working_directory();
Kong::create_log_file(config);
}
fn create_working_directory() {
let working_dir = Konfig::read_working_dir();
let working_dir = std::path::Path::new(&working_dir);
if !std::path::Path::exists(working_dir) {
std::fs::create_dir(working_dir).unwrap()
}
}
fn create_log_file(config: &Konfig) {
if let Some(file_logging) = config.log_file {
if file_logging {
let working_dir = Konfig::read_working_dir();
let working_dir_path = std::path::Path::new(&working_dir);
let log_file = std::path::Path::new(defaults::LOG_FILE);
let log_file_path = working_dir_path.join(log_file);
if !std::path::Path::exists(&log_file_path) {
File::create(log_file_path).unwrap();
}
}
}
}
}
impl Default for Kong {
fn default() -> Self {
let config = Konfig::read().expect("Could not read configuration file.");
Kong::init(&config);
Kong {
config,
kpassport: None,
input: None,
}
}
}