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::{
|
||||
StatusCode, Response, Body,
|
||||
header::HeaderValue
|
||||
};
|
||||
use hyper::{StatusCode, Response, Body};
|
||||
|
||||
use mysql_async::Pool;
|
||||
|
||||
use serde_json::{json, Value, to_string};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use db::{
|
||||
self,
|
||||
@ -13,6 +10,8 @@ use db::{
|
||||
channels::Channel
|
||||
};
|
||||
|
||||
use crate::http::set_json_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 {
|
||||
db::Response::Set(channels) => {
|
||||
response.headers_mut().insert("Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
|
||||
*response.body_mut() = Body::from(to_string(&channels).unwrap_or("{}".into()))
|
||||
set_json_body(response, json!(channels));
|
||||
},
|
||||
db::Response::Other(_msg) => *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(?)
|
||||
match db::channels::Channel::add(pool, name, desc, kind.try_into().unwrap()).await {
|
||||
db::Response::Row(row) => {
|
||||
response.headers_mut().insert("Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
|
||||
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
|
||||
set_json_body(response, json!(row));
|
||||
},
|
||||
// user error that the db doesn't deal with so we just blame the user
|
||||
db::Response::RestrictedInput(msg) => {
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
response.headers_mut().insert("Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
let bjson = json!({"error": msg});
|
||||
*response.body_mut() = Body::from(to_string(&bjson).unwrap_or("{}".into()));
|
||||
set_json_body(response, json!({"error": msg}));
|
||||
},
|
||||
|
||||
// inserted but could not fetch
|
||||
|
@ -1,20 +1,19 @@
|
||||
use hyper::{Response, Body, StatusCode};
|
||||
use hyper::header::HeaderValue;
|
||||
use mysql_async::Pool;
|
||||
use serde_json::json;
|
||||
|
||||
use db::member::STATUS_ONLINE;
|
||||
use db::common::FromDB;
|
||||
|
||||
use crate::http::set_json_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
|
||||
// existance
|
||||
// TODO: loggin at some point or something idklol
|
||||
return match db::channels::Channel::filter(p, STATUS_ONLINE).await {
|
||||
db::Response::Set(users) => {
|
||||
response.headers_mut().insert("Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
|
||||
*response.body_mut() = Body::from(serde_json::to_string(&users).unwrap_or("[]".into()));
|
||||
set_json_body(response, json!(users));
|
||||
},
|
||||
db::Response::Other(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
|
||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
||||
|
@ -1,9 +1,9 @@
|
||||
use mysql_async::Pool;
|
||||
use hyper::{Response, Body, StatusCode};
|
||||
use serde_json::Value;
|
||||
use hyper::header::HeaderValue;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::http::{self, set_json_body};
|
||||
use db::messages::Message;
|
||||
|
||||
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 {
|
||||
Ok(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) => {
|
||||
response.headers_mut().insert(
|
||||
"Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
let payload = json!({"messages": messages});
|
||||
*response.body_mut() = Body::from(payload.to_string());
|
||||
set_json_body(response, json!({"messages": messages}));
|
||||
},
|
||||
db::Response::RestrictedInput(_/*error message to log*/) => *response.status_mut() = StatusCode::BAD_REQUEST,
|
||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
||||
db::Response::RestrictedInput(_/*error message to log*/) => {
|
||||
*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) => {
|
||||
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
|
||||
*/
|
||||
// NOTE: auth module guarantees this will be there in the correct form
|
||||
let author = params.get("id")
|
||||
.unwrap().as_u64().unwrap();
|
||||
let author = http::extract_uid(¶ms);
|
||||
|
||||
match (params.get("content") , params.get("channel")) {
|
||||
(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) => {
|
||||
// user issue
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
response.headers_mut().insert("Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
|
||||
let payload = json!({"msg": msg});
|
||||
*response.body_mut() = Body::from(payload.to_string());
|
||||
set_json_body(response, json!({"msg": msg}))
|
||||
},
|
||||
db::Response::Empty => {}, // nothing to do hyper defaults to 200
|
||||
db::Response::Other(msg) => {
|
||||
@ -142,21 +137,15 @@ pub async fn from_id(pool: &Pool, response: &mut Response<Body>, params: Value)
|
||||
Ok(db_response) => {
|
||||
match db_response {
|
||||
db::Response::Set(messages) => {
|
||||
// NOTE: we do this in the api layer because the db's
|
||||
// lead this to not be defined behavior
|
||||
// if a length is _actually_ 0 the check fails
|
||||
// also hot caches rek us pretty hard so that doesn't help either
|
||||
|
||||
// NOTE this check is here because the db's check doesn't
|
||||
// correctly with async and caching and magic idfk its here
|
||||
// it works its correct and the cost is the same as putting
|
||||
// it in the db layer so whatever
|
||||
if messages.len() == 0 {
|
||||
*response.status_mut() = StatusCode::NOT_FOUND;
|
||||
}
|
||||
else {
|
||||
response.headers_mut().insert(
|
||||
"Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
|
||||
let payload = json!({"messages": messages});
|
||||
*response.body_mut() = Body::from(payload.to_string());
|
||||
set_json_body(response, json!({"messages": messages}));
|
||||
}
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user