Skip to content

Commit

Permalink
add diesel with a users table
Browse files Browse the repository at this point in the history
  • Loading branch information
xmclark committed Dec 9, 2018
1 parent 8b3b32e commit 7229543
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=app2.db
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ authors = ["Damon Myers <[email protected]>"]
edition = "2018"

[dependencies]
actix = "0.7"
actix-web = "0.7"
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
dotenv = "0.10"
futures = "0.1"
r2d2 = "0.8"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.4"

# Use the bundled Sqlite on Windows
[target.'cfg(windows)'.dependencies]
libsqlite3-sys = { version = "0.9", features = ["bundled"] }
5 changes: 5 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# For documentation on how to configure this file,
# see diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
Empty file added migrations/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions migrations/2018-12-08-194435_create_users/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE users
6 changes: 6 additions & 0 deletions migrations/2018-12-08-194435_create_users/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR NOT NULL,
password VARCHAR NOT NULL,
hash VARCHAR NOT NULL
)
22 changes: 22 additions & 0 deletions src/db/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use actix_web::actix::*;
use crate::db::messages;
use crate::db::DbExecutor;
use crate::db::models;
use actix_web::error;
use crate::schema;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel;
use diesel::prelude::*;

impl Handler<messages::GetUser> for DbExecutor {
type Result = Result<Option<models::User>, actix_web::Error>;
fn handle(&mut self, msg: messages::GetUser, ctx: &mut Self::Context) -> Self::Result {
use crate::schema::users::dsl::*;
let conn = &self.0.get().unwrap();
let mut items = users
.filter(username.eq(msg.username.clone()))
.load::<models::User>(conn)
.map_err(|_| error::ErrorInternalServerError("Error loading person"))?;
Ok(items.pop())
}
}
11 changes: 11 additions & 0 deletions src/db/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use actix_web::*;
use actix_web::actix::*;
use crate::db::models;

pub struct GetUser {
pub username: String,
}

impl Message for GetUser {
type Result = Result<Option<models::User>, Error>;
}
23 changes: 23 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::env;
use diesel::prelude::*;
use dotenv::dotenv;
use diesel;
use diesel::r2d2::{ConnectionManager, Pool};
use actix_web::*;
use actix_web::actix::*;

pub mod handlers;
pub mod messages;
pub mod models;

pub type ConnectionType = SqliteConnection;

pub struct DbExecutor(pub Pool<ConnectionManager<ConnectionType>>);

impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}

pub fn create_manager() -> ConnectionManager<ConnectionType> {
ConnectionManager::<SqliteConnection>::new("app2.db")
}
7 changes: 7 additions & 0 deletions src/db/models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Serialize, Queryable)]
pub struct User {
pub id: i32,
pub username: String,
pub password: String,
pub hash: String,
}
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate serde_derive;
extern crate actix_web;
extern crate toml;

pub mod config;
pub mod routes;
pub mod state;

pub mod db;
pub mod schema;
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use actix_web::{http::Method, server, App};
use lighthouse::{config, routes, state::AppState};
use lighthouse::{config, routes, state::AppState, db};
use std::path::Path;
use std::result::Result;
use actix::SyncArbiter;

fn main() {
let loaded_config = load_config_from_default_path();
let config_clone = loaded_config.clone();

// Start 3 db executor actors
let manager = db::create_manager();
let pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
let addr = SyncArbiter::start(3, move || db::DbExecutor(pool.clone()));

server::new(move || {
App::with_state(AppState::from(&config_clone))
App::with_state(AppState::from(&config_clone, &addr))
.resource("/_matrix/client/versions", |r| {
r.method(Method::GET).f(routes::version::versions)
})
Expand All @@ -17,7 +25,9 @@ fn main() {
})
.resource("/_matrix/client/r0/login", |r| {
r.method(Method::GET)
.f(routes::login::supported_login_types)
.f(routes::login::supported_login_types);
r.method(Method::POST)
.f(routes::login::login)
})
})
.bind(format!(
Expand Down
15 changes: 14 additions & 1 deletion src/routes/login/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use actix_web::*;
use actix_web::error::Error;
use actix_web::{HttpRequest, Json};
use actix_web::{HttpRequest, HttpResponse, Json, AsyncResponder, FutureResponse};
use crate::state::AppState;
use crate::db;
use futures::Future;

const DUMMY: &str = "m.login.dummy";

Expand All @@ -21,3 +24,13 @@ pub struct LoginFlow {
pub fn supported_login_types(_: &HttpRequest<AppState>) -> Result<Json<Response>, Error> {
Ok(Json(Response { flows: FLOWS }))
}

pub fn login(req: &HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
let username = "username".to_string();
req.state().db.send(db::messages::GetUser { username }).from_err()
.and_then(|result| match result {
Ok(user) => Ok(HttpResponse::Ok().json(user)),
Err(_) => Ok(HttpResponse::Ok().json("{}".to_string())),
})
.responder()
}
8 changes: 8 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
table! {
users (id) {
id -> Integer,
username -> Text,
password -> Text,
hash -> Text,
}
}
6 changes: 5 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use crate::config::Config;
use actix::Addr;
use crate::db;

#[derive(Clone)]
pub struct AppState {
pub domain: String,
pub db: Addr<db::DbExecutor>,
}

impl AppState {
pub fn from(config: &Config) -> AppState {
pub fn from(config: &Config, db: &Addr<db::DbExecutor>) -> AppState {
AppState {
domain: config.domain.clone(),
db: db.clone(),
}
}
}

0 comments on commit 7229543

Please sign in to comment.