From c560ae76ae8d69bdff57cfed0650d4a12ed83d2e Mon Sep 17 00:00:00 2001 From: Yaser Hsueh Date: Sat, 14 Jan 2023 21:19:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=92=8C=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 4 ++++ src/lib.rs | 13 ------------- src/main.rs | 31 ++++++++++++------------------- src/telecom/mod.rs | 17 ++++++++++++++--- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 70134dd..8a0acf0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,7 @@ rocket = {version="0.5.0-rc.2", features=["json"]} reqwest = { version = "0.11", features = ["blocking", "json"] } tokio = { version = "1", features = ["full"] } +[profile.release] +panic = "abort" +opt-level = "s" +lto = true diff --git a/src/lib.rs b/src/lib.rs index 74350e0..c70e345 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,5 @@ -#[macro_use] extern crate rocket; - -use rocket::serde::{Deserialize}; - 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)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index da8a38d..86f1c2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,30 @@ #[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("/")] async fn index() -> &'static str { - let body = reqwest::get("http://192.168.88.5:8888").await.unwrap().text().await.unwrap(); - println!("{}", body); "Hello, world!" } #[post("/send_msg", format="application/json", data="")] async fn send_msg(message: Json>) -> String { - const API_URL: &'static str = "http://www.js139.com.cn:8022/hysms/SendMsg"; - 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(); + let body = telecom::send_msg(message.into_inner()).await.expect("Error"); format!("{}", &body) } #[launch] 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![send_msg]) } diff --git a/src/telecom/mod.rs b/src/telecom/mod.rs index bd09f07..64ee9b9 100644 --- a/src/telecom/mod.rs +++ b/src/telecom/mod.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use rocket::serde::{Deserialize}; #[derive(Deserialize)] @@ -9,7 +10,17 @@ pub struct Message<'r> { pub content: &'r str } -pub fn send_msg(userId: &str, password: &str, mobile: &str, content:&str) { - let resp = reqwest::blocking::get("http://192.168.88.5/"); - println!("{:?}", resp); +pub async fn send_msg(message: Message<'_>) -> Result{ + const API_URL: &'static str = "http://www.js139.com.cn:8022/hysms/SendMsg"; + 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) }