2023-06-15 23:54:31 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use grin_config::{config, ConfigError, ConfigMembers, GlobalConfig};
|
|
|
|
use grin_config::config::{API_SECRET_FILE_NAME, FOREIGN_API_SECRET_FILE_NAME, SERVER_CONFIG_FILE_NAME};
|
|
|
|
use grin_core::global::ChainTypes;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::Settings;
|
|
|
|
|
2023-06-19 01:29:15 +03:00
|
|
|
/// Wrapped node config to be used by [`grin_servers::Server`].
|
2023-06-15 23:54:31 +03:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct NodeConfig {
|
2023-06-19 01:29:15 +03:00
|
|
|
pub members: ConfigMembers
|
2023-06-15 23:54:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeConfig {
|
2023-06-19 01:29:15 +03:00
|
|
|
/// Initialize integrated node config.
|
|
|
|
pub fn init(chain_type: ChainTypes) -> Self {
|
2023-06-15 23:54:31 +03:00
|
|
|
let _ = Self::check_api_secret_files(chain_type, API_SECRET_FILE_NAME);
|
|
|
|
let _ = Self::check_api_secret_files(chain_type, FOREIGN_API_SECRET_FILE_NAME);
|
|
|
|
|
2023-06-19 01:29:15 +03:00
|
|
|
let config_members = Self::for_chain_type(chain_type);
|
2023-06-15 23:54:31 +03:00
|
|
|
Self {
|
2023-06-19 01:29:15 +03:00
|
|
|
members: config_members
|
2023-06-15 23:54:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 01:29:15 +03:00
|
|
|
/// Initialize config with provided [`ChainTypes`].
|
|
|
|
pub fn for_chain_type(chain_type: ChainTypes) -> ConfigMembers {
|
|
|
|
let path = Settings::get_config_path(SERVER_CONFIG_FILE_NAME, Some(chain_type));
|
|
|
|
let parsed = Settings::read_from_file::<ConfigMembers>(path.clone());
|
|
|
|
if !path.exists() || parsed.is_err() {
|
|
|
|
let mut default_config = GlobalConfig::for_chain(&chain_type);
|
|
|
|
default_config.update_paths(&Settings::get_working_path(Some(chain_type)));
|
|
|
|
let config = default_config.members.unwrap();
|
|
|
|
Settings::write_to_file(&config, path);
|
|
|
|
config
|
|
|
|
} else {
|
|
|
|
parsed.unwrap()
|
2023-06-15 23:54:31 +03:00
|
|
|
}
|
2023-06-19 01:29:15 +03:00
|
|
|
}
|
2023-06-15 23:54:31 +03:00
|
|
|
|
2023-06-19 01:29:15 +03:00
|
|
|
/// Save node config to disk.
|
|
|
|
pub fn save(&mut self) {
|
|
|
|
let chain_type = self.members.server.chain_type;
|
|
|
|
let config_path = Settings::get_config_path(SERVER_CONFIG_FILE_NAME, Some(chain_type));
|
|
|
|
Settings::write_to_file(&self.members, config_path);
|
2023-06-15 23:54:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that the api secret files exist and are valid.
|
|
|
|
fn check_api_secret_files(
|
2023-06-19 01:29:15 +03:00
|
|
|
chain_type: ChainTypes,
|
2023-06-15 23:54:31 +03:00
|
|
|
secret_file_name: &str,
|
|
|
|
) -> Result<(), ConfigError> {
|
|
|
|
let grin_path = Settings::get_working_path(Some(chain_type));
|
|
|
|
let mut api_secret_path = grin_path;
|
|
|
|
api_secret_path.push(secret_file_name);
|
|
|
|
if !api_secret_path.exists() {
|
|
|
|
config::init_api_secret(&api_secret_path)
|
|
|
|
} else {
|
|
|
|
config::check_api_secret(&api_secret_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|