sqlite
This commit is contained in:
parent
53f9dec689
commit
8a7e6ce271
@ -10,6 +10,10 @@ serde = "1"
|
||||
serde_derive = "1"
|
||||
serde_cbor = "0.9"
|
||||
ws = "*"
|
||||
env_logger = "*"
|
||||
diesel = { version = "1.0.0", features = ["postgres", "uuid", "r2d2", "sqlite"] }
|
||||
|
||||
dotenv = "0.9.0"
|
||||
env_logger = "*"
|
||||
|
||||
r2d2 = "0.8.2"
|
||||
r2d2_sqlite = "0.6"
|
||||
rusqlite = { version = "0.14.0", features = ["bundled"] }
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
# For documentation on how to configure this file,
|
||||
# see diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/schema.rs"
|
||||
30
src/lib.rs
30
src/lib.rs
@ -1,30 +0,0 @@
|
||||
extern crate rand;
|
||||
extern crate uuid;
|
||||
extern crate ws;
|
||||
extern crate env_logger;
|
||||
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
extern crate dotenv;
|
||||
|
||||
extern crate serde;
|
||||
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()
|
||||
}
|
||||
10
src/main.rs
10
src/main.rs
@ -3,9 +3,11 @@ extern crate uuid;
|
||||
extern crate ws;
|
||||
extern crate env_logger;
|
||||
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
extern crate dotenv;
|
||||
// #[macro_use]
|
||||
// extern crate dotenv;
|
||||
extern crate r2d2;
|
||||
extern crate r2d2_sqlite;
|
||||
extern crate rusqlite;
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_cbor;
|
||||
@ -18,8 +20,6 @@ mod net;
|
||||
mod combat;
|
||||
mod skill;
|
||||
mod rpc;
|
||||
mod schema;
|
||||
mod models;
|
||||
mod user;
|
||||
|
||||
use net::{start};
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
use diesel::sql_types::Blob;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct Lobby {
|
||||
pub id: String,
|
||||
pub a: String,
|
||||
pub b: String,
|
||||
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,
|
||||
}
|
||||
26
src/net.rs
26
src/net.rs
@ -1,21 +1,19 @@
|
||||
use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
||||
use serde_cbor::{to_vec};
|
||||
|
||||
use diesel::prelude::*;
|
||||
// use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{Pool, ConnectionManager};
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
use r2d2::{Pool};
|
||||
use r2d2::{PooledConnection};
|
||||
use r2d2_sqlite::{SqliteConnectionManager};
|
||||
|
||||
pub type Db = PooledConnection<SqliteConnectionManager>;
|
||||
|
||||
use cryp::{generate};
|
||||
use rpc::{Rpc,RpcMessage};
|
||||
|
||||
pub type DbPool = Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
struct Server {
|
||||
out: Sender,
|
||||
rpc: Rpc,
|
||||
db: DbPool,
|
||||
db: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
impl Handler for Server {
|
||||
@ -47,19 +45,11 @@ impl Handler for Server {
|
||||
}
|
||||
|
||||
pub fn start() {
|
||||
// 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 manager = SqliteConnectionManager::file("/var/cryps/cryps.db");
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,14 +3,14 @@ use ws::{Message};
|
||||
use serde_cbor::{from_slice};
|
||||
use serde_cbor::error::Error as CborError;
|
||||
|
||||
use net::Db;
|
||||
use cryp::generate;
|
||||
use net::{DbPool};
|
||||
use user::{create};
|
||||
|
||||
pub struct Rpc;
|
||||
|
||||
impl Rpc {
|
||||
pub fn receive(&self, msg: Message, db: DbPool) -> StdResult<Vec<u8>, RpcError> {
|
||||
pub fn receive(&self, msg: Message, db: Db) -> StdResult<Vec<u8>, RpcError> {
|
||||
// consume the ws data into bytes
|
||||
let data = msg.into_data();
|
||||
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
table! {
|
||||
lobbies (id) {
|
||||
id -> Text,
|
||||
a -> Text,
|
||||
b -> Nullable<Text>,
|
||||
data -> Nullable<Blob>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Text,
|
||||
name -> Text,
|
||||
}
|
||||
}
|
||||
37
src/user.rs
37
src/user.rs
@ -1,29 +1,28 @@
|
||||
use serde_cbor::to_vec;
|
||||
use uuid::Uuid;
|
||||
|
||||
use diesel;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use net::{DbPool};
|
||||
use net::Db;
|
||||
use rpc::{AccountCreateParams};
|
||||
|
||||
use models;
|
||||
use schema;
|
||||
struct User {
|
||||
name: String,
|
||||
id: Uuid,
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
||||
let uuid = Uuid::new_v4();
|
||||
let new_user = User {
|
||||
id: uuid,
|
||||
name: params.name,
|
||||
};
|
||||
|
||||
let conn: &SqliteConnection = &db.get().unwrap();
|
||||
db.execute("
|
||||
CREATE TABLE user (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
)", &[]).unwrap();
|
||||
|
||||
let user = diesel::insert_into(users)
|
||||
.values(&new_user)
|
||||
.get_result(conn)
|
||||
.expect("Error saving user");
|
||||
|
||||
match to_vec(&level_two) {
|
||||
match to_vec(&true) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("couldn't serialize cryp"),
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user