1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use serde::Deserialize;
use serde::Serialize;
use std::net::UdpSocket;
use std::time::{SystemTime, UNIX_EPOCH};

/// 网络传输结构体
#[derive(Serialize, Deserialize, Debug)]
pub struct WebResult<T> {
    pub data: Option<T>,
    pub code: usize,
    pub msg: Option<String>,
}

/// WebResult functions
impl<T> WebResult<T> {
    pub fn success(data: T) -> WebResult<T> {
        let result: WebResult<T> = WebResult {
            data: Some(data),
            code: 200,
            msg: Some("success".to_string()),
        };
        result
    }
}

/// 获取本机IP
pub fn get_local_ip() -> String {
    let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    socket.connect("8.8.8.8:80").unwrap();
    socket.local_addr().unwrap().ip().to_string()
}

/// 获取时间戳
/// > 精确到秒,非毫秒
pub async fn get_timestamp() -> u64 {
    let start = SystemTime::now();
    let since_the_epoch = start
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards");
    let timestamp = since_the_epoch.as_secs();
    timestamp
}

/// Get deploy time line
pub async fn get_time_line() -> u64 {
    let timestamp = get_timestamp().await;
    let time_line = timestamp - 2 * 3600;
    time_line
}