shenyu_client_rust/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use serde::Deserialize;
19use serde_yaml;
20use std::collections::HashMap;
21use std::fs::File;
22use std::io::Read;
23
24#[allow(missing_docs)]
25#[derive(Debug, Deserialize)]
26pub struct EnvConfig {
27    pub(crate) shenyu: ShenYuConfig,
28}
29
30#[allow(missing_docs)]
31#[derive(Debug, Deserialize)]
32pub struct ShenYuConfig {
33    pub register: RegisterConfig,
34    pub uri: UriConfig,
35    pub discovery: DiscoveryConfig,
36}
37
38impl ShenYuConfig {
39    /// Load configuration from a YAML file.
40    pub fn from_yaml_file(file_path: &str) -> Result<Self, Box<dyn std::error::Error>> {
41        let current_dir = std::env::current_dir().unwrap();
42
43        let mut file = File::open(current_dir.join(file_path))?;
44        let mut contents = String::new();
45        _ = file.read_to_string(&mut contents)?;
46        let config: EnvConfig = serde_yaml::from_str(&contents)?;
47        Ok(config.shenyu)
48    }
49}
50
51#[allow(missing_docs)]
52#[derive(Debug, Deserialize)]
53pub struct RegisterConfig {
54    pub register_type: String,
55    pub servers: String,
56    pub namespace_id: Option<String>,
57    pub props: HashMap<String, String>,
58}
59
60#[allow(missing_docs)]
61#[derive(Debug, Deserialize)]
62pub struct UriConfig {
63    pub app_name: String,
64    pub host: String,
65    pub port: u16,
66    pub context_path: String,
67    pub environment: String,
68    pub rpc_type: String,
69}
70
71#[allow(missing_docs)]
72#[derive(Debug, Deserialize)]
73pub struct DiscoveryConfig {
74    pub protocol: String,
75    pub discovery_type: String,
76    pub server_lists: String,
77    pub register_path: String,
78    pub plugin_name: String,
79    pub props: HashMap<String, String>,
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use std::env;
86
87    #[test]
88    fn test_from_yaml_file() {
89        let current_dir = env::current_dir().expect("Failed to get current directory");
90        let config_path = current_dir.join("config.yml");
91        print!("config_path: {:?}", config_path);
92        let config = ShenYuConfig::from_yaml_file(config_path.to_str().unwrap()).unwrap();
93        assert_eq!(config.register.register_type, "http");
94        assert_eq!(config.register.servers, "http://127.0.0.1:9095");
95        assert_eq!(config.register.props.len(), 2);
96    }
97}