-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
131 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL=app2.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} | ||
} |