rocketmq_common/common/server/
config.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use serde::Deserialize;
18use serde::Serialize;
19
20/// Default value functions for Serde deserialization
21mod defaults {
22    pub fn listen_port() -> u32 {
23        10911
24    }
25
26    pub fn bind_address() -> String {
27        "0.0.0.0".to_string()
28    }
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct ServerConfig {
34    #[serde(default = "defaults::listen_port")]
35    pub listen_port: u32,
36
37    #[serde(default = "defaults::bind_address")]
38    pub bind_address: String,
39}
40
41impl Default for ServerConfig {
42    fn default() -> Self {
43        ServerConfig {
44            listen_port: defaults::listen_port(),
45            bind_address: defaults::bind_address(),
46        }
47    }
48}
49
50impl ServerConfig {
51    pub fn bind_address(&self) -> String {
52        self.bind_address.clone()
53    }
54
55    pub fn listen_port(&self) -> u32 {
56        self.listen_port
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_server_config() {
66        let config = ServerConfig::default();
67
68        assert_eq!(config.listen_port(), 10911);
69
70        assert_eq!(config.bind_address(), "0.0.0.0".to_string());
71    }
72}