forget diesel
This commit is contained in:
parent
c2f8993d07
commit
53f9dec689
@ -11,5 +11,5 @@ serde_derive = "1"
|
|||||||
serde_cbor = "0.9"
|
serde_cbor = "0.9"
|
||||||
ws = "*"
|
ws = "*"
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
diesel = { version = "1.0.0", features = ["postgres", "uuid"] }
|
diesel = { version = "1.0.0", features = ["postgres", "uuid", "r2d2", "sqlite"] }
|
||||||
dotenv = "0.9.0"
|
dotenv = "0.9.0"
|
||||||
|
|||||||
13
src/db.rs
13
src/db.rs
@ -1,13 +0,0 @@
|
|||||||
use diesel::prelude::*;
|
|
||||||
use diesel::pg::PgConnection;
|
|
||||||
use dotenv::dotenv;
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
pub fn establish_connection() -> PgConnection {
|
|
||||||
dotenv().ok();
|
|
||||||
|
|
||||||
let database_url = env::var("DATABASE_URL")
|
|
||||||
.expect("DATABASE_URL must be set");
|
|
||||||
PgConnection::establish(&database_url)
|
|
||||||
.expect(&format!("Error connecting to {}", database_url))
|
|
||||||
}
|
|
||||||
28
src/lib.rs
28
src/lib.rs
@ -1,6 +1,30 @@
|
|||||||
|
extern crate rand;
|
||||||
extern crate uuid;
|
extern crate uuid;
|
||||||
|
extern crate ws;
|
||||||
|
extern crate env_logger;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
extern crate dotenv;
|
||||||
|
|
||||||
pub mod schema;
|
extern crate serde;
|
||||||
pub mod models;
|
extern crate serde_cbor;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
|
||||||
|
mod schema;
|
||||||
|
mod models;
|
||||||
|
|
||||||
|
mod cryp;
|
||||||
|
mod battle;
|
||||||
|
mod net;
|
||||||
|
mod combat;
|
||||||
|
mod skill;
|
||||||
|
mod rpc;
|
||||||
|
mod user;
|
||||||
|
|
||||||
|
use net::{start};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
start()
|
||||||
|
}
|
||||||
|
|||||||
@ -20,6 +20,7 @@ mod skill;
|
|||||||
mod rpc;
|
mod rpc;
|
||||||
mod schema;
|
mod schema;
|
||||||
mod models;
|
mod models;
|
||||||
|
mod user;
|
||||||
|
|
||||||
use net::{start};
|
use net::{start};
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,22 @@
|
|||||||
use diesel::sql_types::{Uuid,Bytea};
|
use diesel::sql_types::Blob;
|
||||||
|
|
||||||
#[derive(Queryable)]
|
#[derive(Queryable)]
|
||||||
pub struct Lobby {
|
pub struct Lobby {
|
||||||
pub id: Uuid,
|
pub id: String,
|
||||||
pub a: Uuid,
|
pub a: String,
|
||||||
pub b: Uuid,
|
pub b: String,
|
||||||
pub data: Bytea,
|
pub data: Blob,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Queryable)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[table_name = "users"]
|
||||||
|
pub struct NewUser {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
}
|
}
|
||||||
33
src/net.rs
33
src/net.rs
@ -1,11 +1,21 @@
|
|||||||
use cryp::{generate};
|
|
||||||
use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
||||||
use rpc::{Rpc,RpcMessage};
|
|
||||||
use serde_cbor::{to_vec};
|
use serde_cbor::{to_vec};
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
// use diesel::pg::PgConnection;
|
||||||
|
use diesel::r2d2::{Pool, ConnectionManager};
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
use cryp::{generate};
|
||||||
|
use rpc::{Rpc,RpcMessage};
|
||||||
|
|
||||||
|
pub type DbPool = Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
struct Server {
|
struct Server {
|
||||||
out: Sender,
|
out: Sender,
|
||||||
rpc: Rpc,
|
rpc: Rpc,
|
||||||
|
db: DbPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Handler for Server {
|
impl Handler for Server {
|
||||||
@ -15,7 +25,8 @@ impl Handler for Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(&mut self, msg: Message) -> Result<()> {
|
fn on_message(&mut self, msg: Message) -> Result<()> {
|
||||||
let reply = self.rpc.receive(msg);
|
let db = self.db.get().expect("unable to get db connection");
|
||||||
|
let reply = self.rpc.receive(msg, db);
|
||||||
println!("{:?}", reply);
|
println!("{:?}", reply);
|
||||||
self.out.send(reply.unwrap())
|
self.out.send(reply.unwrap())
|
||||||
}
|
}
|
||||||
@ -36,5 +47,19 @@ impl Handler for Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {} } }).unwrap();
|
// dotenv().ok();
|
||||||
|
|
||||||
|
// let database_url = env::var("DATABASE_URL")
|
||||||
|
// .expect("DATABASE_URL must be set");
|
||||||
|
|
||||||
|
let manager = ConnectionManager::<SqliteConnection>::new("/var/cryps/cryps.db");
|
||||||
|
|
||||||
|
// let manager = ConnectionManager::PgConnection::establish(&database_url)
|
||||||
|
// .expect(&format!("Error connecting to {}", database_url))
|
||||||
|
|
||||||
|
let pool = Pool::builder()
|
||||||
|
.build(manager)
|
||||||
|
.expect("Failed to create pool.");
|
||||||
|
|
||||||
|
listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {}, db: pool.clone() } }).unwrap();
|
||||||
}
|
}
|
||||||
65
src/rpc.rs
65
src/rpc.rs
@ -4,22 +4,13 @@ use serde_cbor::{from_slice};
|
|||||||
use serde_cbor::error::Error as CborError;
|
use serde_cbor::error::Error as CborError;
|
||||||
|
|
||||||
use cryp::generate;
|
use cryp::generate;
|
||||||
|
use net::{DbPool};
|
||||||
|
use user::{create};
|
||||||
|
|
||||||
pub struct Rpc;
|
pub struct Rpc;
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
||||||
struct GenerateMsg {
|
|
||||||
method: String,
|
|
||||||
params: GenerateParams,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
||||||
pub struct GenerateParams {
|
|
||||||
pub level: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Rpc {
|
impl Rpc {
|
||||||
pub fn receive(&self, msg: Message) -> StdResult<Vec<u8>, RpcError> {
|
pub fn receive(&self, msg: Message, db: DbPool) -> StdResult<Vec<u8>, RpcError> {
|
||||||
// consume the ws data into bytes
|
// consume the ws data into bytes
|
||||||
let data = msg.into_data();
|
let data = msg.into_data();
|
||||||
|
|
||||||
@ -36,6 +27,13 @@ impl Rpc {
|
|||||||
Err(_) => Err(RpcError::Parse),
|
Err(_) => Err(RpcError::Parse),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"account_create" => {
|
||||||
|
match from_slice::<AccountCreateMsg>(&data) {
|
||||||
|
Ok(v) => Ok(create(v.params, db)),
|
||||||
|
Err(_) => Err(RpcError::Parse),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
_ => Err(RpcError::UnknownMethod),
|
_ => Err(RpcError::UnknownMethod),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -55,15 +53,38 @@ pub enum RpcError {
|
|||||||
UnknownMethod,
|
UnknownMethod,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
mod tests {
|
struct GenerateMsg {
|
||||||
use super::*;
|
method: String,
|
||||||
use serde_cbor::to_vec;
|
params: GenerateParams,
|
||||||
#[test]
|
|
||||||
fn rpc_parse() {
|
|
||||||
let rpc = Rpc {};
|
|
||||||
let msg = GenerateMsg { method: "cryp_generate".to_string(), params: GenerateParams { level: 64 } };
|
|
||||||
let v = to_vec(&msg).unwrap();
|
|
||||||
let received = rpc.receive(Message::Binary(v));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
pub struct GenerateParams {
|
||||||
|
pub level: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
struct AccountCreateMsg {
|
||||||
|
method: String,
|
||||||
|
params: AccountCreateParams,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
pub struct AccountCreateParams {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod tests {
|
||||||
|
// use super::*;
|
||||||
|
// use serde_cbor::to_vec;
|
||||||
|
// #[test]
|
||||||
|
// fn rpc_parse() {
|
||||||
|
// let rpc = Rpc {};
|
||||||
|
// let msg = GenerateMsg { method: "cryp_generate".to_string(), params: GenerateParams { level: 64 } };
|
||||||
|
// let v = to_vec(&msg).unwrap();
|
||||||
|
// let received = rpc.receive(Message::Binary(v));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
@ -1,8 +1,15 @@
|
|||||||
table! {
|
table! {
|
||||||
lobbies (id) {
|
lobbies (id) {
|
||||||
id -> Uuid,
|
id -> Text,
|
||||||
a -> Uuid,
|
a -> Text,
|
||||||
b -> Nullable<Uuid>,
|
b -> Nullable<Text>,
|
||||||
data -> Nullable<Bytea>,
|
data -> Nullable<Blob>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table! {
|
||||||
|
users (id) {
|
||||||
|
id -> Text,
|
||||||
|
name -> Text,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/user.rs
Normal file
30
src/user.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use diesel;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
use net::{DbPool};
|
||||||
|
use rpc::{AccountCreateParams};
|
||||||
|
|
||||||
|
use models;
|
||||||
|
use schema;
|
||||||
|
|
||||||
|
pub fn create(params: AccountCreateParams, db: DbPool) -> Vec<u8> {
|
||||||
|
let uuid = format!("{}", Uuid::new_v4());
|
||||||
|
let new_user = models::NewUser {
|
||||||
|
id: uuid,
|
||||||
|
name: params.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
let conn: &SqliteConnection = &db.get().unwrap();
|
||||||
|
|
||||||
|
let user = diesel::insert_into(users)
|
||||||
|
.values(&new_user)
|
||||||
|
.get_result(conn)
|
||||||
|
.expect("Error saving user");
|
||||||
|
|
||||||
|
match to_vec(&level_two) {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => panic!("couldn't serialize cryp"),
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user