54 lines
1.3 KiB
Rust

use {
hyper::{
Request,
body::{
Incoming,
},
},
serde_json::{
Value as Json,
json,
},
skytable::pool::ConnectionMgrTcp,
bb8::Pool,
std::sync::Arc,
crate::{
double_split,
},
};
type DBPool = Arc<Pool<ConnectionMgrTcp>>;
pub async fn api(req: Request<Incoming>, pool: DBPool) -> Json {
let uri: &str = req.uri().path().as_ref();
match &uri[7..uri.len()] {
"/auth" => auth(req, pool.clone()).await,
"/auth_get" => auth_get(req, pool.clone()).await,
_ => json!({"error": true, "msg": "No endpoint"})
}
}
async fn auth(req: Request<Incoming>, _pool: DBPool) -> Json {
let query = req.uri().query().or(Some("")).unwrap();
let query = double_split(query.to_string(), "&", "=");
let sess = std::string::String::from(query
.get("session")
.or(Some(&"".to_string()))
.unwrap());
match sess.as_str() {
"" => json!({"error": true, "msg": "No session in url"}),
_ => json!({
"error": false,
"link": format!("https://auth.bitheaven.ru/authorize?session={}", sess)
})
}
}
async fn auth_get(req: Request<Incoming>, _pool: DBPool) -> Json {
let query = req.uri().query().or(Some("")).unwrap();
let query = double_split(query.to_string(), "&", "=");
println!("{:?}", query);
json!({"error": false, "msg": "test auth_get endpoint v0"})
}