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>,
}
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
}
}
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
}
pub async fn get_time_line() -> u64 {
let timestamp = get_timestamp().await;
let time_line = timestamp - 2 * 3600;
time_line
}