Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Actix #156

Merged
merged 28 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9d2eb38
Add aardwolf-actix
asonix Oct 27, 2018
4d5f5f7
Add db actor
asonix Oct 27, 2018
48262da
Use features to switch servers
asonix Oct 28, 2018
138c94f
Fix logging for actix
asonix Oct 28, 2018
e980f3d
Bring actix a lot closer to Rocket
asonix Oct 28, 2018
af5b00a
SignUp and LogIn posts work
asonix Oct 29, 2018
9c4770b
Fix bug where multiple users couldn't verify emails
asonix Oct 29, 2018
5047641
Bump dependencies
asonix Oct 30, 2018
1a7286e
Add really neat error type except...
asonix Oct 30, 2018
02520a5
Actix nearing feature-parity
asonix Oct 30, 2018
6d05be4
Redirect on form errors
asonix Oct 30, 2018
cdb575d
Improve errors (not done yet)
asonix Oct 30, 2018
34f4cdd
Wire up personas
asonix Oct 31, 2018
32a3255
Get rid of compiler warnings
asonix Oct 31, 2018
947b380
Move user lookups to aardwolf-types
asonix Oct 31, 2018
d265865
Use shared behavior in rocket
asonix Oct 31, 2018
f2a65db
Clean up some duplicate code
asonix Nov 1, 2018
d5ee69f
Be more explicit about errors
asonix Nov 1, 2018
7f4ab9b
Don't implement TemplateError for unused type
asonix Nov 1, 2018
4d53247
THIS IS BROKEN
asonix Nov 3, 2018
4e792a6
Give things consistent names
asonix Nov 3, 2018
b91ee9a
Bring perform to aardwolf-rocket
asonix Nov 3, 2018
7a7c591
Rename and reorganize things in -types
asonix Nov 3, 2018
78ec9e8
Test all crates in travis
asonix Nov 3, 2018
bb2a096
Don't be verbose
asonix Nov 3, 2018
9d6c54e
Silence diesel warnings
asonix Nov 3, 2018
9b1388f
Increase timeout
asonix Nov 3, 2018
ffbdc56
Redo errors, Separate create persona check from create persona operation
asonix Nov 4, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Ignore aardwolf.toml
aardwolf.toml
aardwolf.log
diesel.toml
.config

node_modules
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ before_script:
- diesel migration run
- popd

script:
- |
for project in 'aardwolf-models' 'aardwolf-types' 'aardwolf-actix' 'aardwolf-rocket' '.'; do
pushd $project
cargo build --verbose
cargo test --verbose
popd
done

rust:
- nightly

cache: cargo
cache:
cargo: true
timeout: 1000
1,684 changes: 1,455 additions & 229 deletions Cargo.lock

Large diffs are not rendered by default.

45 changes: 23 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,41 @@ description = "Powering connected social communities with free software."
[workspace]
members = [
"aardwolf-models",
"aardwolf-rocket"
"aardwolf-types",
"aardwolf-rocket",
"aardwolf-actix"
]

[features]
default = ["simple-logging"]
# Integrate with Systemd, includes Journald for logging. Conflicts with log-*
# features.
systemd = ["aardwolf-rocket/systemd"]
# Use syslog for writing logs. Conflicts with use-systemd and log-* features.
syslog = ["aardwolf-rocket/syslog"]
rocket = ["aardwolf-rocket"]
actix = ["aardwolf-actix"]


[dependencies]
config = "0.9.0"
failure = "0.1"
log = "0.4"
yaml-rust = "0.3"
# Use simple log files or stdout/stderr. Conflicts with use-systemd and
# log-* features.
simple-logging = ["aardwolf-rocket/simple-logging"]
simple-logging = { version = "2.0", optional = true }
# Use syslog for writing logs. Conflicts with use-systemd and log-* features.
syslog = { version = "4.0", optional = true }
# Integrate with Systemd, includes Journald for logging. Conflicts with log-*
# features.
systemd = { version = "0.4", optional = true }

[dependencies]
config = "0.8.0"
failure = "0.1.1"
r2d2 = "0.8"
r2d2-diesel = "1.0"
rocket = "0.3.17"
rocket_codegen = "0.3.17"
rocket_contrib = "0.3.17"
yaml-rust = "0.3.5"
log = "0.4.1"
[dependencies.aardwolf-actix]
version = "0.1"
path = "aardwolf-actix"
optional = true

[dependencies.aardwolf-rocket]
version = "0.1"
path = "aardwolf-rocket"
optional = true

[dependencies.clap]
version = "2.29"
features = ["yaml"]

[dependencies.diesel]
version = "1.1"
default-features = false
features = ["postgres", "uuid", "chrono"]
34 changes: 34 additions & 0 deletions aardwolf-actix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "aardwolf-actix"
version = "0.1.0"
authors = ["asonix <asonix@asonix.dog>"]
edition = "2018"

[dependencies]
actix = "0.7"
actix-web = "0.7"
collection_macros = "0.2.0"
config = "0.9.0"
failure = "0.1"
futures = "0.1"
handlebars = "1.1"
log = "0.4"
r2d2 = "0.8"
r2d2-diesel = "1.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

[dependencies.aardwolf-models]
version = "0.1"
path = "../aardwolf-models"

[dependencies.aardwolf-types]
version = "0.1"
path = "../aardwolf-types"
features = ["use-actix"]

[dependencies.diesel]
version = "1.1"
default-features = false
features = ["postgres", "uuid", "chrono"]
171 changes: 171 additions & 0 deletions aardwolf-actix/src/action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
use std::marker::PhantomData;

use aardwolf_types::{
error::AardwolfFail,
forms::traits::{DbAction, Validate},
};
use futures::future::Future;

use crate::{
db::{DbActionError, PerformDbAction},
AppConfig,
};

pub trait Action<T, E>
where
E: AardwolfFail,
{
fn action(self, state: AppConfig) -> Box<dyn Future<Item = T, Error = E> + Send>;
}

pub struct ValidateWrapper<V, T, E>(V, PhantomData<T>, PhantomData<E>)
where
V: Validate<T, E>,
E: AardwolfFail;

impl<V, T, E> ValidateWrapper<V, T, E>
where
V: Validate<T, E>,
E: AardwolfFail,
{
pub fn new(validate: V) -> Self {
ValidateWrapper(validate, PhantomData, PhantomData)
}
}

impl<V, T, E> From<V> for ValidateWrapper<V, T, E>
where
V: Validate<T, E>,
E: AardwolfFail,
{
fn from(v: V) -> Self {
ValidateWrapper::new(v)
}
}

impl<V, T, E> Action<T, E> for ValidateWrapper<V, T, E>
where
V: Validate<T, E>,
T: Send + 'static,
E: AardwolfFail,
{
fn action(self, _: AppConfig) -> Box<dyn Future<Item = T, Error = E> + Send> {
use futures::future::IntoFuture;

Box::new(self.0.validate().into_future())
}
}

pub struct DbActionWrapper<D, T, E>(D, PhantomData<T>, PhantomData<E>)
where
D: DbAction<T, E>,
E: AardwolfFail;

impl<D, T, E> DbActionWrapper<D, T, E>
where
D: DbAction<T, E>,
E: AardwolfFail,
{
pub fn new(db_action: D) -> Self {
DbActionWrapper(db_action, PhantomData, PhantomData)
}
}

impl<D, T, E> From<D> for DbActionWrapper<D, T, E>
where
D: DbAction<T, E>,
E: AardwolfFail,
{
fn from(d: D) -> Self {
DbActionWrapper::new(d)
}
}

impl<D, T, E> Action<T, DbActionError<E>> for DbActionWrapper<D, T, E>
where
D: DbAction<T, E> + Send + 'static,
T: Send + 'static,
E: AardwolfFail,
{
fn action(
self,
state: AppConfig,
) -> Box<dyn Future<Item = T, Error = DbActionError<E>> + Send> {
let fut = state
.db
.send(PerformDbAction::new(self.0))
.then(|res| match res {
Ok(item_res) => match item_res {
Ok(item) => Ok(item),
Err(e) => Err(e),
},
Err(e) => Err(DbActionError::from(e)),
});

Box::new(fut)
}
}

#[macro_export]
macro_rules! perform {
( $state:expr, $start:expr, $error_type:ty, [] ) => {{
use futures::future::IntoFuture;

(Ok($start) as Result<_, $error_type>).into_future()
}};
(
$state:expr,
$start:expr,
$error_type:ty,
[
($wrapper:ty => $first:expr),
$(($wrappers:ty => $rest:expr),)*
]
) => {{
use $crate::action::Action;

let wrapper: $wrapper = $first.with($start).into();

let fut = wrapper.action($state.clone());

perform_inner!($state, $error_type, fut, [ $(($wrappers => $rest),)* ])
}};
}

macro_rules! perform_inner {
(
$state:expr,
$error_type:ty,
$first:expr,
[]
) => {{
use futures::future::IntoFuture;

$first
.into_future()
.from_err::<$error_type>()
}};
(
$state:expr,
$error_type:ty,
$first:expr,
[
($wrapper:ty => $item:expr),
$(($wrappers:ty => $items:expr),)*
]
) => {{
use futures::future::IntoFuture;
use $crate::action::Action;

$first
.into_future()
.from_err::<$error_type>()
.and_then(move |item| {
let wrapper: $wrapper = $item.with(item).into();

let fut = wrapper.action($state.clone());

perform_inner!($state, $error_type, fut, [ $(($wrappers => $items),)* ])
})
}};
}
27 changes: 27 additions & 0 deletions aardwolf-actix/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::actix::prelude::*;
use diesel::pg::PgConnection;
use r2d2::PooledConnection;
use r2d2_diesel::ConnectionManager;

mod perform_db_action;

pub use self::perform_db_action::{DbActionError, PerformDbAction};

pub type ManagedPgConn = ConnectionManager<PgConnection>;
pub type Pool = r2d2::Pool<ManagedPgConn>;

pub struct Db(Pool);

impl Db {
pub fn new(pool: Pool) -> Self {
Db(pool)
}

fn get_connection(&self) -> Result<PooledConnection<ManagedPgConn>, r2d2::Error> {
self.0.get()
}
}

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