@@ -5,24 +5,48 @@ extern crate log;
55extern crate anyhow;
66
77use domain_types:: DOMAIN_ID ;
8+ use serde:: { Deserialize , Serialize } ;
89use simplelog:: { CombinedLogger , Config as LogConfig , LevelFilter } ;
9- use std:: env:: args;
10- use std:: path:: PathBuf ;
11- use std:: sync:: Arc ;
10+ use std:: { env:: args, path:: PathBuf , sync:: Arc } ;
1211use tdn:: { prelude:: * , types:: primitives:: Result } ;
12+ use tdn_did:: { generate_mnemonic, generate_peer, Count , Language } ;
1313use tokio:: sync:: { mpsc:: Sender , RwLock } ;
1414
1515mod layer;
1616mod models;
1717mod rpc;
1818mod storage;
1919
20- pub const DEFAULT_PROVIDER_NAME : & ' static str = "domain.esse" ;
21- pub const DEFAULT_PROVIDER_PROXY : bool = true ;
20+ const DEFAULT_PROVIDER_NAME : & ' static str = "domain.esse" ;
21+ const DEFAULT_PROVIDER_PROXY : bool = true ;
2222
23- pub const DEFAULT_P2P_ADDR : & ' static str = "0.0.0.0:7367" ; // DEBUG CODE
24- pub const DEFAULT_HTTP_ADDR : & ' static str = "127.0.0.1:8003" ; // DEBUG CODE
25- pub const DEFAULT_LOG_FILE : & ' static str = "esse.log.txt" ;
23+ const DEFAULT_P2P_ADDR : & ' static str = "0.0.0.0:7350" ;
24+ const DEFAULT_HTTP_ADDR : & ' static str = "127.0.0.1:7351" ;
25+ const DEFAULT_LOG_FILE : & ' static str = "domain.log.txt" ;
26+
27+ /// parse custom config from config.toml.
28+ #[ derive( Serialize , Deserialize , Debug ) ]
29+ pub struct CustomConfig {
30+ pub name : String ,
31+ pub proxy : bool ,
32+ pub mnemonic : String ,
33+ }
34+
35+ fn custom_config_str ( config : & CustomConfig ) -> String {
36+ format ! (
37+ r#"## Domain custom Config.
38+ ## domain server name.
39+ name = "{}"
40+
41+ ## domain is support proxy request/response.
42+ proxy = {}
43+
44+ ## domain server default mnemonic words (keep PeerId same).
45+ mnemonic = "{}"
46+ "# ,
47+ config. name, config. proxy, config. mnemonic
48+ )
49+ }
2650
2751#[ tokio:: main]
2852async fn main ( ) {
@@ -46,22 +70,32 @@ pub async fn start(db_path: String) -> Result<()> {
4670 init_log ( db_path. clone ( ) ) ;
4771 info ! ( "Core storage path {:?}" , db_path) ;
4872
49- // let _client = storage::connect_database()?;
50-
51- let mut config = Config :: load_save ( db_path. clone ( ) ) . await ;
73+ let mut config = Config :: default ( ) ;
5274 config. db_path = Some ( db_path. clone ( ) ) ;
53- // // use self sign to bootstrap peer.
54- // if config.rpc_ws.is_none() {
55- // // set default ws addr.
56- // config.rpc_ws = Some(DEFAULT_WS_ADDR.parse().unwrap());
57- // }
58- config. rpc_ws = None ;
5975 config. rpc_addr = DEFAULT_HTTP_ADDR . parse ( ) . unwrap ( ) ;
6076 config. p2p_peer = Peer :: socket ( DEFAULT_P2P_ADDR . parse ( ) . unwrap ( ) ) ;
6177 config. p2p_allowlist . append ( & mut vec ! [ Peer :: socket(
6278 "1.15.156.199:7364" . parse( ) . unwrap( ) ,
6379 ) ] ) ;
6480 config. group_ids = vec ! [ DOMAIN_ID ] ;
81+ let config = Config :: load_save ( db_path. clone ( ) , config) . await ?;
82+ let custom: Option < CustomConfig > = Config :: load_custom ( db_path. clone ( ) ) . await ;
83+ let CustomConfig {
84+ name,
85+ proxy,
86+ mnemonic,
87+ } = if let Some ( custom) = custom {
88+ custom
89+ } else {
90+ let mnemonic = generate_mnemonic ( Language :: English , Count :: Words12 ) ;
91+ let custom = CustomConfig {
92+ name : DEFAULT_PROVIDER_NAME . to_owned ( ) ,
93+ proxy : DEFAULT_PROVIDER_PROXY ,
94+ mnemonic : mnemonic,
95+ } ;
96+ Config :: append_custom ( db_path. clone ( ) , & custom_config_str ( & custom) ) . await ?;
97+ custom
98+ } ;
6599
66100 info ! ( "Config RPC HTTP : {:?}" , config. rpc_addr) ;
67101 info ! (
@@ -71,13 +105,12 @@ pub async fn start(db_path: String) -> Result<()> {
71105 ) ;
72106
73107 let _rand_secret = config. secret . clone ( ) ;
74-
75- let ( peer_id, sender, mut recver) = start_with_config ( config) . await . unwrap ( ) ;
108+ let pkey = generate_peer ( Language :: English , & mnemonic , 0 , 0 , None ) ? ;
109+ let ( peer_id, sender, mut recver) = start_with_config_and_key ( config, pkey ) . await ? ;
76110 info ! ( "Network Peer id : {}" , peer_id. to_hex( ) ) ;
77- let name = DEFAULT_PROVIDER_NAME . to_owned ( ) ;
78111
79112 let layer = Arc :: new ( RwLock :: new (
80- layer:: Layer :: new ( db_path, name, peer_id) . await ?,
113+ layer:: Layer :: new ( db_path, name, peer_id, proxy ) . await ?,
81114 ) ) ;
82115
83116 let rpc_handler = rpc:: new_rpc_handler ( layer. clone ( ) ) ;
0 commit comments