Initial commit
This commit is contained in:
commit
cb0ff01446
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
1919
Cargo.lock
generated
Normal file
1919
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "easy-sms"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
serde ={version ="1.0", features=["derive"]}
|
||||||
|
rocket = {version="0.5.0-rc.2", features=["json"]}
|
||||||
|
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
|
34
src/lib.rs
Normal file
34
src/lib.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#[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::*;
|
||||||
|
#[test]
|
||||||
|
fn test_to_urlencoded() {
|
||||||
|
let message = Message {
|
||||||
|
userid: "123",
|
||||||
|
password: "456",
|
||||||
|
mobile: "18605120786",
|
||||||
|
content: "Hello world!"
|
||||||
|
};
|
||||||
|
let s = format!("userId={}&password={}&mobile={}&content={}", message.userid, message.password, message.mobile, message.content);
|
||||||
|
assert_eq!(&s, rocket::http::RawStr::new(&s).percent_encode().as_str());
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_req() {
|
||||||
|
telecom::send_msg("1","2","3","4");
|
||||||
|
}
|
||||||
|
}
|
37
src/main.rs
Normal file
37
src/main.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#[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;
|
||||||
|
|
||||||
|
|
||||||
|
#[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="<message>")]
|
||||||
|
async fn send_msg(message: Json<Message<'_>>) -> 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();
|
||||||
|
format!("{}", &body)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[launch]
|
||||||
|
fn rocket() -> _ {
|
||||||
|
rocket::build()
|
||||||
|
.mount("/", routes![index])
|
||||||
|
.mount("/", routes![send_msg])
|
||||||
|
}
|
15
src/telecom/mod.rs
Normal file
15
src/telecom/mod.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
use rocket::serde::{Deserialize};
|
||||||
|
|
||||||
|
#[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
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send_msg(userId: &str, password: &str, mobile: &str, content:&str) {
|
||||||
|
let resp = reqwest::blocking::get("http://192.168.88.5/");
|
||||||
|
println!("{:?}", resp);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user