cyfs_meta_lib/
meta_target.rs

1use http_types::Url;
2use std::str::FromStr;
3use cyfs_base::{BuckyError, CyfsChannel};
4use log::*;
5use std::fmt::Formatter;
6
7const DEV_SERVICE_URL: &str = "http://nightly.meta.cyfs.com:1423";
8const TEST_SERVICE_URL: &str = "http://beta.meta.cyfs.com:1423";
9
10const DEV_SPV_URL: &str = "http://nightly.meta.cyfs.com:3516";
11const TEST_SPV_URL: &str = "http://beta.meta.cyfs.com:3516";
12
13#[derive(Clone, Debug, Eq, PartialEq)]
14pub enum MetaMinerTarget {
15    // 开发环境
16    Dev,
17
18    // 测试环境
19    Test,
20
21    // 正式环境
22    Formal,
23
24    // other
25    Other(Url, Url),
26}
27
28impl std::fmt::Display for MetaMinerTarget {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        match self {
31            MetaMinerTarget::Dev => {f.write_str("dev")}
32            MetaMinerTarget::Test => {f.write_str("test")}
33            MetaMinerTarget::Formal => {f.write_str("formal")}
34            MetaMinerTarget::Other(miner, spv) => {f.write_fmt(format_args!("meta: {}, spv: {}", miner.to_string(), spv.to_string()))}
35        }
36    }
37}
38
39impl MetaMinerTarget {
40    pub fn new(miner: Url, spv: Url) -> Self {
41        Self::Other(miner, spv)
42    }
43
44    pub fn miner_url(&self) -> String {
45        match self {
46            Self::Dev => DEV_SERVICE_URL.to_owned(),
47            Self::Test => TEST_SERVICE_URL.to_owned(),
48            Self::Formal => {
49                unimplemented!();
50            }
51            Self::Other(url, _) => {
52                let url = url.to_string();
53                if url.ends_with("/") {
54                    url.trim_end_matches("/").to_string()
55                } else {
56                    url
57                }
58            }
59        }
60    }
61
62    pub fn spv_url(&self) -> String {
63        match self {
64            Self::Dev => DEV_SPV_URL.to_owned(),
65            Self::Test => TEST_SPV_URL.to_owned(),
66            Self::Formal => {
67                unimplemented!();
68            }
69            Self::Other(_, url) => {
70                let url = url.to_string();
71                if url.ends_with("/") {
72                    url.trim_end_matches("/").to_string()
73                } else {
74                    url
75                }
76            }
77        }
78    }
79}
80
81impl Default for MetaMinerTarget {
82    fn default() -> Self {
83        match cyfs_base::get_channel() {
84            CyfsChannel::Nightly => Self::Dev,
85            CyfsChannel::Beta => Self::Test,
86            CyfsChannel::Stable => Self::Formal
87        }
88    }
89}
90
91// 兼容旧的没有spv配置的使用情况
92impl FromStr for MetaMinerTarget {
93    type Err = BuckyError;
94
95    fn from_str(value: &str) -> Result<Self, Self::Err> {
96        let ret = match value {
97            "dev" => Self::Dev,
98            "test" => Self::Test,
99            "formal" => Self::Formal,
100            v @ _ => {
101                let mut host = v.to_owned();
102                if !host.ends_with("/") {
103                    host = host + "/";
104                }
105
106                match Url::parse(&host) {
107                    Ok(url) => Self::Other(url.clone(), url),
108                    Err(e) => {
109                        let msg = format!("invalid meta miner target url: {}, {}", host, e);
110                        warn!("{}", msg);
111                        return Err(BuckyError::from(msg));
112                    }
113                }
114            }
115        };
116
117        Ok(ret)
118    }
119}