No more messing with hyper Reponse<Body> manually.
All that is handled to the http module
This commit is contained in:
parent
9e505bd2bb
commit
49c675b97a
@ -1,11 +1,8 @@
|
|||||||
use hyper::{
|
use hyper::{StatusCode, Response, Body};
|
||||||
StatusCode, Response, Body,
|
|
||||||
header::HeaderValue
|
|
||||||
};
|
|
||||||
|
|
||||||
use mysql_async::Pool;
|
use mysql_async::Pool;
|
||||||
|
|
||||||
use serde_json::{json, Value, to_string};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use db::{
|
use db::{
|
||||||
self,
|
self,
|
||||||
@ -13,6 +10,8 @@ use db::{
|
|||||||
channels::Channel
|
channels::Channel
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::http::set_json_body;
|
||||||
|
|
||||||
|
|
||||||
pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
||||||
/*
|
/*
|
||||||
@ -21,10 +20,7 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
|||||||
*/
|
*/
|
||||||
return match db::channels::Channel::filter(pool, 0).await {
|
return match db::channels::Channel::filter(pool, 0).await {
|
||||||
db::Response::Set(channels) => {
|
db::Response::Set(channels) => {
|
||||||
response.headers_mut().insert("Content-Type",
|
set_json_body(response, json!(channels));
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
|
|
||||||
*response.body_mut() = Body::from(to_string(&channels).unwrap_or("{}".into()))
|
|
||||||
},
|
},
|
||||||
db::Response::Other(_msg) => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR,
|
db::Response::Other(_msg) => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
_ => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR,
|
_ => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
@ -57,18 +53,12 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
// Send the data up to the db, then return the new channel back to the user(?)
|
// Send the data up to the db, then return the new channel back to the user(?)
|
||||||
match db::channels::Channel::add(pool, name, desc, kind.try_into().unwrap()).await {
|
match db::channels::Channel::add(pool, name, desc, kind.try_into().unwrap()).await {
|
||||||
db::Response::Row(row) => {
|
db::Response::Row(row) => {
|
||||||
response.headers_mut().insert("Content-Type",
|
set_json_body(response, json!(row));
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
|
|
||||||
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
|
|
||||||
},
|
},
|
||||||
// user error that the db doesn't deal with so we just blame the user
|
// user error that the db doesn't deal with so we just blame the user
|
||||||
db::Response::RestrictedInput(msg) => {
|
db::Response::RestrictedInput(msg) => {
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
response.headers_mut().insert("Content-Type",
|
set_json_body(response, json!({"error": msg}));
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
let bjson = json!({"error": msg});
|
|
||||||
*response.body_mut() = Body::from(to_string(&bjson).unwrap_or("{}".into()));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// inserted but could not fetch
|
// inserted but could not fetch
|
||||||
|
@ -1,20 +1,19 @@
|
|||||||
use hyper::{Response, Body, StatusCode};
|
use hyper::{Response, Body, StatusCode};
|
||||||
use hyper::header::HeaderValue;
|
|
||||||
use mysql_async::Pool;
|
use mysql_async::Pool;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
use db::member::STATUS_ONLINE;
|
use db::member::STATUS_ONLINE;
|
||||||
use db::common::FromDB;
|
use db::common::FromDB;
|
||||||
|
|
||||||
|
use crate::http::set_json_body;
|
||||||
|
|
||||||
pub async fn get_online_members(p: &Pool, response: &mut Response<Body>) {
|
pub async fn get_online_members(p: &Pool, response: &mut Response<Body>) {
|
||||||
// TODO: at some point we should provide a way of not querying literally every user in
|
// TODO: at some point we should provide a way of not querying literally every user in
|
||||||
// existance
|
// existance
|
||||||
// TODO: loggin at some point or something idklol
|
// TODO: loggin at some point or something idklol
|
||||||
return match db::channels::Channel::filter(p, STATUS_ONLINE).await {
|
return match db::channels::Channel::filter(p, STATUS_ONLINE).await {
|
||||||
db::Response::Set(users) => {
|
db::Response::Set(users) => {
|
||||||
response.headers_mut().insert("Content-Type",
|
set_json_body(response, json!(users));
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
|
|
||||||
*response.body_mut() = Body::from(serde_json::to_string(&users).unwrap_or("[]".into()));
|
|
||||||
},
|
},
|
||||||
db::Response::Other(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
|
db::Response::Other(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use mysql_async::Pool;
|
use mysql_async::Pool;
|
||||||
use hyper::{Response, Body, StatusCode};
|
use hyper::{Response, Body, StatusCode};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use hyper::header::HeaderValue;
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
use crate::http::{self, set_json_body};
|
||||||
use db::messages::Message;
|
use db::messages::Message;
|
||||||
|
|
||||||
pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
||||||
@ -41,18 +41,18 @@ pub async fn get_by_time(pool: &Pool, response: &mut Response<Body>, params: Val
|
|||||||
match Message::get_time_range(pool, channel, start, end, limit).await {
|
match Message::get_time_range(pool, channel, start, end, limit).await {
|
||||||
Ok(db_response) => {
|
Ok(db_response) => {
|
||||||
match db_response {
|
match db_response {
|
||||||
|
// this absolute lack of data streaming is prolly gonna suck like
|
||||||
|
// a whore in hell week for performance but lets pretend servers don't get massive
|
||||||
db::Response::Set(messages) => {
|
db::Response::Set(messages) => {
|
||||||
response.headers_mut().insert(
|
set_json_body(response, json!({"messages": messages}));
|
||||||
"Content-Type",
|
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
let payload = json!({"messages": messages});
|
|
||||||
*response.body_mut() = Body::from(payload.to_string());
|
|
||||||
},
|
},
|
||||||
db::Response::RestrictedInput(_/*error message to log*/) => *response.status_mut() = StatusCode::BAD_REQUEST,
|
db::Response::RestrictedInput(_/*error message to log*/) => {
|
||||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
// this absolute lack of data streaming is prolly gonna suck like
|
|
||||||
// a whore in hell week for performance but lets pretend servers don't get massive
|
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
@ -70,8 +70,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Va
|
|||||||
* @channel: channel id that we're going to send a message to
|
* @channel: channel id that we're going to send a message to
|
||||||
*/
|
*/
|
||||||
// NOTE: auth module guarantees this will be there in the correct form
|
// NOTE: auth module guarantees this will be there in the correct form
|
||||||
let author = params.get("id")
|
let author = http::extract_uid(¶ms);
|
||||||
.unwrap().as_u64().unwrap();
|
|
||||||
|
|
||||||
match (params.get("content") , params.get("channel")) {
|
match (params.get("content") , params.get("channel")) {
|
||||||
(Some(content_v), Some(channel_id_v)) => {
|
(Some(content_v), Some(channel_id_v)) => {
|
||||||
@ -90,11 +89,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Va
|
|||||||
db::Response::RestrictedInput(msg) => {
|
db::Response::RestrictedInput(msg) => {
|
||||||
// user issue
|
// user issue
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
response.headers_mut().insert("Content-Type",
|
set_json_body(response, json!({"msg": msg}))
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
|
|
||||||
let payload = json!({"msg": msg});
|
|
||||||
*response.body_mut() = Body::from(payload.to_string());
|
|
||||||
},
|
},
|
||||||
db::Response::Empty => {}, // nothing to do hyper defaults to 200
|
db::Response::Empty => {}, // nothing to do hyper defaults to 200
|
||||||
db::Response::Other(msg) => {
|
db::Response::Other(msg) => {
|
||||||
@ -142,21 +137,15 @@ pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: Value)
|
|||||||
Ok(db_response) => {
|
Ok(db_response) => {
|
||||||
match db_response {
|
match db_response {
|
||||||
db::Response::Set(messages) => {
|
db::Response::Set(messages) => {
|
||||||
// NOTE: we do this in the api layer because the db's
|
// NOTE this check is here because the db's check doesn't
|
||||||
// lead this to not be defined behavior
|
// correctly with async and caching and magic idfk its here
|
||||||
// if a length is _actually_ 0 the check fails
|
// it works its correct and the cost is the same as putting
|
||||||
// also hot caches rek us pretty hard so that doesn't help either
|
// it in the db layer so whatever
|
||||||
|
|
||||||
if messages.len() == 0 {
|
if messages.len() == 0 {
|
||||||
*response.status_mut() = StatusCode::NOT_FOUND;
|
*response.status_mut() = StatusCode::NOT_FOUND;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
response.headers_mut().insert(
|
set_json_body(response, json!({"messages": messages}));
|
||||||
"Content-Type",
|
|
||||||
HeaderValue::from_static("application/json"));
|
|
||||||
|
|
||||||
let payload = json!({"messages": messages});
|
|
||||||
*response.body_mut() = Body::from(payload.to_string());
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user