1
- use lazy_static:: lazy_static;
1
+ #![ allow( deprecated) ]
2
+ use std:: collections:: HashMap ;
2
3
use serde_derive:: { Deserialize , Serialize } ;
3
- use std:: fs:: File ;
4
- use std:: io:: Read ;
4
+ use config:: { Config , File } ;
5
+ use notify:: { Event , RecommendedWatcher , RecursiveMode , Watcher } ;
6
+ use std:: path:: Path ;
7
+ use std:: sync:: mpsc:: channel;
8
+ use std:: sync:: RwLock ;
9
+ use std:: thread;
10
+ use std:: time:: Duration ;
11
+
12
+ const CONFIG_PATH : & str = "src/config.toml" ;
5
13
6
14
#[ derive( Serialize , Deserialize , Debug ) ]
7
- pub struct Config {
8
- pub net : NetConfig ,
9
- pub service : ServiceConfig ,
10
- pub log : LogConfig ,
15
+ pub struct ArkimeConfig {
16
+ net : NetConfig ,
17
+ service : ServiceConfig ,
18
+ log : LogConfig ,
11
19
}
12
20
13
21
#[ derive( Serialize , Deserialize , Debug ) ]
14
22
pub struct NetConfig {
15
- pub dynamic_interfaces : bool ,
16
- pub bpf_filter : String ,
17
- pub link_name : Vec < String > ,
23
+ dynamic_interfaces : bool ,
24
+ bpf_filter : String ,
25
+ link_name : Vec < String > ,
18
26
}
19
27
20
28
#[ derive( Serialize , Deserialize , Debug ) ]
21
29
pub struct ServiceConfig {
30
+ hostname : String ,
22
31
host : String ,
23
32
port : u32 ,
33
+ socket_path : String ,
24
34
}
25
35
26
36
#[ derive( Serialize , Deserialize , Debug ) ]
@@ -29,30 +39,72 @@ pub struct LogConfig {
29
39
output : String ,
30
40
}
31
41
32
- impl Default for Config {
33
- fn default ( ) -> Self {
34
- let file_path = "src/config.toml" ;
42
+ lazy_static:: lazy_static! {
43
+ static ref SETTINGS : RwLock <Config > = RwLock :: new( {
44
+ let settings = Config :: builder( )
45
+ . add_source( config:: Environment :: with_prefix( "ARKIME-RUST" ) . separator( "_" ) )
46
+ . add_source( File :: with_name( CONFIG_PATH ) ) ;
47
+ settings. build( ) . unwrap( )
48
+ } ) ;
49
+ }
35
50
36
- let mut file = match File :: open ( file_path) {
37
- Ok ( f) => f,
38
- Err ( e) => panic ! ( "error is op en conf {e}" ) ,
39
- } ;
51
+ fn watch ( ) {
52
+ thread:: spawn ( || {
53
+ let ( tx, rx) = channel ( ) ;
40
54
41
- let mut str = String :: new ( ) ;
42
- match file . read_to_string ( & mut str ) {
43
- Ok ( s ) => s ,
44
- Err ( e ) => panic ! ( "error read str {}" , e ) ,
45
- } ;
55
+ let mut watcher : RecommendedWatcher = Watcher :: new (
56
+ tx ,
57
+ notify :: Config :: default ( ) . with_poll_interval ( Duration :: from_secs ( 1000 ) ) ,
58
+ )
59
+ . unwrap ( ) ;
46
60
47
- toml:: from_str ( & str) . expect ( "parse config file failed" )
48
- }
49
- }
61
+ watcher
62
+ . watch (
63
+ Path :: new ( CONFIG_PATH ) ,
64
+ RecursiveMode :: NonRecursive ,
65
+ )
66
+ . unwrap ( ) ;
67
+ loop {
68
+ match rx. recv ( ) {
69
+ Ok ( Ok ( Event {
70
+ kind : notify:: event:: EventKind :: Modify ( _) ,
71
+ ..
72
+ } ) ) => {
73
+ println ! ( " * Settings.toml written; refreshing configuration ..." ) ;
74
+ SETTINGS . write ( ) . unwrap ( ) . refresh ( ) . unwrap ( ) ;
75
+ show ( ) ;
76
+ }
77
+
78
+ Err ( e) => println ! ( "watch error: {:?}" , e) ,
50
79
51
- impl Config {
52
- pub fn get < ' a > ( ) -> & ' a Self {
53
- lazy_static ! {
54
- static ref CACHE : Config = Config :: default ( ) ;
80
+ _ => {
81
+ // Ignore event
82
+ }
83
+ }
55
84
}
56
- & CACHE
85
+ } ) ;
86
+ }
87
+
88
+ impl ArkimeConfig {
89
+
90
+ pub fn init ( ) {
91
+ SETTINGS . read ( ) . unwrap ( ) ;
92
+ watch ( ) ;
93
+ }
94
+
95
+ pub fn get_interface ( self ) -> Vec < String > {
96
+ return self . net . link_name ;
57
97
}
58
98
}
99
+
100
+ fn show ( ) {
101
+ println ! (
102
+ " * Settings :: \n \x1b [31m{:?}\x1b [0m" ,
103
+ SETTINGS
104
+ . read( )
105
+ . unwrap( )
106
+ . clone( )
107
+ . try_deserialize:: <HashMap <String , String >>( )
108
+ . unwrap( )
109
+ ) ;
110
+ }
0 commit comments