重构和优化

This commit is contained in:
椰子 2023-01-14 21:19:32 +08:00
parent cb0ff01446
commit c560ae76ae
4 changed files with 30 additions and 35 deletions

View File

@ -11,3 +11,7 @@ rocket = {version="0.5.0-rc.2", features=["json"]}
reqwest = { version = "0.11", features = ["blocking", "json"] } reqwest = { version = "0.11", features = ["blocking", "json"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
[profile.release]
panic = "abort"
opt-level = "s"
lto = true

View File

@ -1,18 +1,5 @@
#[macro_use] extern crate rocket;
use rocket::serde::{Deserialize};
pub mod telecom; pub mod telecom;
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Message<'r> {
pub userid: &'r str,
pub password: &'r str,
pub mobile: &'r str,
pub content: &'r str
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -1,37 +1,30 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
use rocket::serde::json::Json;
use rocket::fs::FileServer;
use easy_sms::Message;
use easy_sms::telecom;
use std::collections::HashMap;
use std::net::Ipv4Addr;
use rocket::Config;
use rocket::serde::json::Json;
use easy_sms::telecom;
use easy_sms::telecom::Message;
#[get("/")] #[get("/")]
async fn index() -> &'static str { async fn index() -> &'static str {
let body = reqwest::get("http://192.168.88.5:8888").await.unwrap().text().await.unwrap();
println!("{}", body);
"Hello, world!" "Hello, world!"
} }
#[post("/send_msg", format="application/json", data="<message>")] #[post("/send_msg", format="application/json", data="<message>")]
async fn send_msg(message: Json<Message<'_>>) -> String { async fn send_msg(message: Json<Message<'_>>) -> String {
const API_URL: &'static str = "http://www.js139.com.cn:8022/hysms/SendMsg"; let body = telecom::send_msg(message.into_inner()).await.expect("Error");
let client = reqwest::Client::new();
let mut ps: HashMap<&str, &str> = HashMap::new();
ps.insert("userId", message.userid);
ps.insert("password", message.password);
ps.insert("mobile", message.mobile);
ps.insert("content", message.content);
let body = client.post(API_URL)
.query(&ps)
.send().await.unwrap()
.text().await.unwrap();
format!("{}", &body) format!("{}", &body)
} }
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
rocket::build() let config = Config {
port: 80,
address: Ipv4Addr::new(0,0,0,0).into(),
..Config::default()
};
rocket::custom(&config)
.mount("/", routes![index]) .mount("/", routes![index])
.mount("/", routes![send_msg]) .mount("/", routes![send_msg])
} }

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use rocket::serde::{Deserialize}; use rocket::serde::{Deserialize};
#[derive(Deserialize)] #[derive(Deserialize)]
@ -9,7 +10,17 @@ pub struct Message<'r> {
pub content: &'r str pub content: &'r str
} }
pub fn send_msg(userId: &str, password: &str, mobile: &str, content:&str) { pub async fn send_msg(message: Message<'_>) -> Result<String, String>{
let resp = reqwest::blocking::get("http://192.168.88.5/"); const API_URL: &'static str = "http://www.js139.com.cn:8022/hysms/SendMsg";
println!("{:?}", resp); let client = reqwest::Client::new();
let mut ps: HashMap<&str, &str> = HashMap::new();
ps.insert("userId", message.userid);
ps.insert("password", message.password);
ps.insert("mobile", message.mobile);
ps.insert("content", message.content);
let body = client.post(API_URL)
.query(&ps)
.send().await.unwrap()
.text().await.unwrap();
Ok(body)
} }