method handlers

This commit is contained in:
ntr
2018-09-15 14:11:23 +10:00
parent 2b30c87ef8
commit 7a174736f6
7 changed files with 89 additions and 29 deletions
+8
View File
@@ -13,3 +13,11 @@
*
* Blockchain Integration?
# Mechanic Ideas
teams
gem td style attr combinations
stoney + spikey = jagged
+20 -8
View File
@@ -7,9 +7,11 @@ use r2d2::{Pool};
use r2d2::{PooledConnection};
use r2d2_postgres::{TlsMode, PostgresConnectionManager};
static DB_POOL_SIZE: u32 = 20;
pub type Db = PooledConnection<PostgresConnectionManager>;
use rpc::{Rpc};
use rpc::{Rpc, RpcResult};
struct Server {
out: Sender,
@@ -17,6 +19,11 @@ struct Server {
db: Pool<PostgresConnectionManager>,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct RpcErrorResponse {
err: String
}
impl Handler for Server {
fn on_open(&mut self, _: Handshake) -> Result<()> {
println!("somebody joined");
@@ -32,7 +39,7 @@ impl Handler for Server {
self.out.send(response)
},
Err(e) => {
let response = to_vec(&e.to_string())
let response = to_vec(&RpcErrorResponse { err: e.to_string() })
.expect("failed to serialize error response");
self.out.send(response)
}
@@ -54,16 +61,21 @@ impl Handler for Server {
}
}
pub fn db_connection(url: String) -> Pool<PostgresConnectionManager> {
let manager = PostgresConnectionManager::new(url, TlsMode::None)
.expect("could not instantiate pg manager");
Pool::builder()
.max_size(DB_POOL_SIZE)
.build(manager)
.expect("Failed to create pool.")
}
pub fn start() {
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
let manager = PostgresConnectionManager::new(database_url, TlsMode::None)
.expect("could not instantiate pg manager");
let pool = Pool::builder()
.build(manager)
.expect("Failed to create pool.");
let pool = db_connection(database_url);
listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {}, db: pool.clone() } }).unwrap();
}
Regular → Executable
+18 -10
View File
@@ -10,7 +10,7 @@ use user::{User, create};
pub struct Rpc;
impl Rpc {
pub fn receive(&self, msg: Message, db: Db) -> Result<RpcResult, Error> {
pub fn receive(&self, msg: Message, db: Db) -> Result<RpcResponse, Error> {
// consume the ws data into bytes
let data = msg.into_data();
@@ -18,18 +18,26 @@ impl Rpc {
match from_slice::<RpcMessage>(&data) {
Ok(v) => {
println!("{:?}", v.token);
// now we have the method name
// match on that to determine what fn to call
match v.method.as_ref() {
"cryp_generate" => {
match from_slice::<GenerateMsg>(&data) {
Ok(v) => Ok(RpcResult::Cryp(generate(v.params))),
Ok(v) => Ok(RpcResponse {
method: v.method,
params: RpcResult::Cryp(generate(v.params))
}),
Err(_e) => Err(err_msg("invalid params")),
}
},
"account_create" => {
"user_create" => {
match from_slice::<AccountCreateMsg>(&data) {
Ok(v) => create(v.params, db),
Ok(v) => Ok(RpcResponse {
method: v.method,
params: create(v.params, db)?
}),
Err(_e) => Err(err_msg("invalid params")),
}
},
@@ -42,6 +50,11 @@ impl Rpc {
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct RpcResponse {
method: String,
params: RpcResult,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub enum RpcResult {
@@ -52,12 +65,7 @@ pub enum RpcResult {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct RpcMessage {
method: String,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub enum RpcError {
Parse,
UnknownMethod,
token: Option<String>,
}
#[derive(Debug,Clone,Serialize,Deserialize)]