Using serde_json
This commit is contained in:
@ -8,7 +8,8 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
threadpool = "1.0"
|
threadpool = "1.0"
|
||||||
json = "0.12.4"
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0.64"
|
||||||
openssl = {version = "0.10", features = ["vendored"]}
|
openssl = {version = "0.10", features = ["vendored"]}
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
flate2 = "1.0.0"
|
flate2 = "1.0.0"
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
use super::Method;
|
use super::Method;
|
||||||
use crate::usimp;
|
use crate::usimp;
|
||||||
use crate::websocket;
|
use crate::websocket;
|
||||||
use chrono;
|
use serde_json;
|
||||||
use json;
|
|
||||||
use std::borrow::Borrow;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
pub struct HttpStream {
|
pub struct HttpStream {
|
||||||
pub stream: super::Stream,
|
pub stream: super::Stream,
|
||||||
@ -53,18 +50,13 @@ fn request_handler(client: &mut super::HttpStream) {
|
|||||||
let parts: Vec<&str> = req.uri.split('/').collect();
|
let parts: Vec<&str> = req.uri.split('/').collect();
|
||||||
match parts[2..] {
|
match parts[2..] {
|
||||||
["entity", entity] => res.status(501),
|
["entity", entity] => res.status(501),
|
||||||
[endpoint] => match usimp::is_valid_endpoint(endpoint) {
|
[endpoint] => match req.method {
|
||||||
true => match req.method {
|
Method::POST => return endpoint_handler(client, &req, &mut res, endpoint),
|
||||||
Method::POST => {
|
|
||||||
return endpoint_handler(client, &req, &mut res, endpoint)
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
res.status(405);
|
res.status(405);
|
||||||
res.add_header("Allow", "POST");
|
res.add_header("Allow", "POST");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
false => res.status(400),
|
|
||||||
},
|
|
||||||
_ => res.status(400),
|
_ => res.status(400),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -137,16 +129,7 @@ fn endpoint_handler(
|
|||||||
client.stream.read_exact(&mut buf[..length]);
|
client.stream.read_exact(&mut buf[..length]);
|
||||||
|
|
||||||
// TODO decompress
|
// TODO decompress
|
||||||
let input = match json::parse(match std::str::from_utf8(&buf[..length]) {
|
let input = match serde_json::from_slice(&buf[..length]) {
|
||||||
Ok(source) => source,
|
|
||||||
Err(e) => {
|
|
||||||
return error(
|
|
||||||
400,
|
|
||||||
format!("Unable to parse payload: {}", &e).as_str(),
|
|
||||||
client,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return error(
|
return error(
|
||||||
@ -156,10 +139,13 @@ fn endpoint_handler(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let output = usimp::endpoint(endpoint, input);
|
|
||||||
|
let buf = match usimp::endpoint(endpoint, input) {
|
||||||
|
Ok(output) => output.to_string() + "\r\n",
|
||||||
|
Err(e) => "{\"status\":\"error\"}\r\n".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
// TODO compress
|
// TODO compress
|
||||||
let buf = json::stringify(output) + "\r\n";
|
|
||||||
let length = buf.as_bytes().len();
|
let length = buf.as_bytes().len();
|
||||||
res.add_header("Content-Length", length.to_string().as_str());
|
res.add_header("Content-Length", length.to_string().as_str());
|
||||||
res.add_header("Content-Type", "application/json; charset=utf-8");
|
res.add_header("Content-Type", "application/json; charset=utf-8");
|
||||||
|
@ -4,12 +4,8 @@ mod udp;
|
|||||||
mod usimp;
|
mod usimp;
|
||||||
mod websocket;
|
mod websocket;
|
||||||
|
|
||||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslStream};
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||||
use r2d2;
|
|
||||||
use r2d2::{ManageConnection, Pool};
|
|
||||||
use r2d2_postgres::{postgres::NoTls, PostgresConnectionManager};
|
|
||||||
use std::net::{SocketAddr, TcpListener, UdpSocket};
|
use std::net::{SocketAddr, TcpListener, UdpSocket};
|
||||||
use std::ops::Deref;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
@ -1,35 +1,36 @@
|
|||||||
use crate::database;
|
use crate::database;
|
||||||
use json;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
static ENDPOINTS: [(&str, fn(json::JsonValue) -> json::JsonValue); 1] = [("echo", echo)];
|
pub fn endpoint(endpoint: &str, input: serde_json::Value) -> serde_json::Result<serde_json::Value> {
|
||||||
|
match endpoint {
|
||||||
pub fn is_valid_endpoint(endpoint: &str) -> bool {
|
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(input)?))?),
|
||||||
for (name, _func) in &ENDPOINTS {
|
_ => Ok("{}".into()),
|
||||||
if endpoint.eq(*name) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn endpoint(endpoint: &str, input: json::JsonValue) -> json::JsonValue {
|
#[derive(Serialize, Deserialize)]
|
||||||
for (name, func) in &ENDPOINTS {
|
pub struct EchoInput {
|
||||||
if endpoint.eq(*name) {
|
message: String,
|
||||||
return func(input);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic!("invalid endpoint, check with is_valid_endpoint")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn echo(input: json::JsonValue) -> json::JsonValue {
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct EchoOutput {
|
||||||
|
message: String,
|
||||||
|
database: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn echo(input: EchoInput) -> EchoOutput {
|
||||||
let backend = database::client();
|
let backend = database::client();
|
||||||
let mut output = input.clone();
|
let mut output = EchoOutput {
|
||||||
|
message: input.message,
|
||||||
|
database: None,
|
||||||
|
};
|
||||||
match backend {
|
match backend {
|
||||||
database::Client::Postgres(mut client) => {
|
database::Client::Postgres(mut client) => {
|
||||||
let res = client.query("SELECT * FROM test", &[]).unwrap();
|
let res = client.query("SELECT * FROM test", &[]).unwrap();
|
||||||
for row in res {
|
for row in res {
|
||||||
let val: i32 = row.get(0);
|
output.database = Some(row.get(0));
|
||||||
output["database"] = val.into();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user