sqlite
This commit is contained in:
parent
53f9dec689
commit
8a7e6ce271
@ -10,6 +10,10 @@ serde = "1"
|
|||||||
serde_derive = "1"
|
serde_derive = "1"
|
||||||
serde_cbor = "0.9"
|
serde_cbor = "0.9"
|
||||||
ws = "*"
|
ws = "*"
|
||||||
env_logger = "*"
|
|
||||||
diesel = { version = "1.0.0", features = ["postgres", "uuid", "r2d2", "sqlite"] }
|
|
||||||
dotenv = "0.9.0"
|
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 ws;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
#[macro_use]
|
// #[macro_use]
|
||||||
extern crate diesel;
|
// extern crate dotenv;
|
||||||
extern crate dotenv;
|
extern crate r2d2;
|
||||||
|
extern crate r2d2_sqlite;
|
||||||
|
extern crate rusqlite;
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_cbor;
|
extern crate serde_cbor;
|
||||||
@ -18,8 +20,6 @@ mod net;
|
|||||||
mod combat;
|
mod combat;
|
||||||
mod skill;
|
mod skill;
|
||||||
mod rpc;
|
mod rpc;
|
||||||
mod schema;
|
|
||||||
mod models;
|
|
||||||
mod user;
|
mod user;
|
||||||
|
|
||||||
use net::{start};
|
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 ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
||||||
use serde_cbor::{to_vec};
|
use serde_cbor::{to_vec};
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use r2d2::{Pool};
|
||||||
// use diesel::pg::PgConnection;
|
use r2d2::{PooledConnection};
|
||||||
use diesel::r2d2::{Pool, ConnectionManager};
|
use r2d2_sqlite::{SqliteConnectionManager};
|
||||||
use dotenv::dotenv;
|
|
||||||
use std::env;
|
pub type Db = PooledConnection<SqliteConnectionManager>;
|
||||||
|
|
||||||
use cryp::{generate};
|
use cryp::{generate};
|
||||||
use rpc::{Rpc,RpcMessage};
|
use rpc::{Rpc,RpcMessage};
|
||||||
|
|
||||||
pub type DbPool = Pool<ConnectionManager<SqliteConnection>>;
|
|
||||||
|
|
||||||
struct Server {
|
struct Server {
|
||||||
out: Sender,
|
out: Sender,
|
||||||
rpc: Rpc,
|
rpc: Rpc,
|
||||||
db: DbPool,
|
db: Pool<SqliteConnectionManager>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Handler for Server {
|
impl Handler for Server {
|
||||||
@ -47,19 +45,11 @@ impl Handler for Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
// dotenv().ok();
|
let manager = SqliteConnectionManager::file("/var/cryps/cryps.db");
|
||||||
|
|
||||||
// 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()
|
let pool = Pool::builder()
|
||||||
.build(manager)
|
.build(manager)
|
||||||
.expect("Failed to create pool.");
|
.expect("Failed to create pool.");
|
||||||
|
|
||||||
listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {}, db: pool.clone() } }).unwrap();
|
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::{from_slice};
|
||||||
use serde_cbor::error::Error as CborError;
|
use serde_cbor::error::Error as CborError;
|
||||||
|
|
||||||
|
use net::Db;
|
||||||
use cryp::generate;
|
use cryp::generate;
|
||||||
use net::{DbPool};
|
|
||||||
use user::{create};
|
use user::{create};
|
||||||
|
|
||||||
pub struct Rpc;
|
pub struct Rpc;
|
||||||
|
|
||||||
impl 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
|
// consume the ws data into bytes
|
||||||
let data = msg.into_data();
|
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 uuid::Uuid;
|
||||||
|
|
||||||
use diesel;
|
use net::Db;
|
||||||
use diesel::prelude::*;
|
|
||||||
|
|
||||||
use net::{DbPool};
|
|
||||||
use rpc::{AccountCreateParams};
|
use rpc::{AccountCreateParams};
|
||||||
|
|
||||||
use models;
|
struct User {
|
||||||
use schema;
|
name: String,
|
||||||
|
id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create(params: AccountCreateParams, db: DbPool) -> Vec<u8> {
|
pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
||||||
let uuid = format!("{}", Uuid::new_v4());
|
let uuid = Uuid::new_v4();
|
||||||
let new_user = models::NewUser {
|
let new_user = User {
|
||||||
id: uuid,
|
id: uuid,
|
||||||
name: params.name,
|
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)
|
match to_vec(&true) {
|
||||||
.values(&new_user)
|
|
||||||
.get_result(conn)
|
|
||||||
.expect("Error saving user");
|
|
||||||
|
|
||||||
match to_vec(&level_two) {
|
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => panic!("couldn't serialize cryp"),
|
Err(e) => panic!("couldn't serialize cryp"),
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user