This commit is contained in:
Michael Zhang 2019-02-02 19:01:59 -06:00
parent 1e8ab72e37
commit 9e772edc45
No known key found for this signature in database
GPG Key ID: 5BAEFE5D04F0CE6C
7 changed files with 60 additions and 19 deletions

View File

@ -0,0 +1,9 @@
use std::net::SocketAddr;
use crate::db::DatabaseUri;
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
bind_addr: SocketAddr,
database_uri: DatabaseUri,
}

View File

@ -18,23 +18,25 @@ use crate::Error;
pub enum DbPool {
#[cfg(feature = "mysql")]
Mysql(Pool<ConnectionManager<MysqlConnection>>),
#[cfg(feature = "postgres")]
Postgres(Pool<ConnectionManager<PgConnection>>),
#[cfg(feature = "sqlite")]
Sqlite(Pool<ConnectionManager<SqliteConnection>>),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DatabaseUri {
#[cfg(feature = "mysql")]
Mysql(Url),
Mysql(String),
#[cfg(feature = "postgres")]
Postgres(Url),
Postgres(String),
#[cfg(feature = "sqlite")]
Sqlite(Url),
Sqlite(String),
}
impl DatabaseUri {
@ -42,17 +44,17 @@ impl DatabaseUri {
match self {
#[cfg(feature = "mysql")]
DatabaseUri::Mysql(url) => {
let manager = ConnectionManager::<MysqlConnection>::new(url.as_str());
let manager = ConnectionManager::<MysqlConnection>::new(url.as_ref());
Ok(DbPool::Mysql(Pool::new(manager)?))
}
#[cfg(feature = "postgres")]
DatabaseUri::Postgres(url) => {
let manager = ConnectionManager::<PgConnection>::new(url.as_str());
let manager = ConnectionManager::<PgConnection>::new(url.as_ref());
Ok(DbPool::Postgres(Pool::new(manager)?))
}
#[cfg(feature = "sqlite")]
DatabaseUri::Sqlite(url) => {
let manager = ConnectionManager::<SqliteConnection>::new(url.path());
let manager = ConnectionManager::<SqliteConnection>::new(url.as_ref());
Ok(DbPool::Sqlite(Pool::new(manager)?))
}
}
@ -65,11 +67,11 @@ impl FromStr for DatabaseUri {
let url = Url::parse(string)?;
match url.scheme() {
#[cfg(feature = "mysql")]
"mysql" => Ok(DatabaseUri::Mysql(url)),
"mysql" => Ok(DatabaseUri::Mysql(url.as_str().to_owned())),
#[cfg(feature = "postgres")]
"postgres" => Ok(DatabaseUri::Postgres(url)),
"postgres" => Ok(DatabaseUri::Postgres(url.as_str().to_owned())),
#[cfg(feature = "sqlite")]
"sqlite" => Ok(DatabaseUri::Sqlite(url)),
"sqlite" => Ok(DatabaseUri::Sqlite(url.path().to_owned())),
// TODO: an actual error
_ => Err(ParseError::IdnaError),
}

View File

@ -3,12 +3,16 @@ extern crate diesel;
#[macro_use]
extern crate serde_derive;
mod config;
mod db;
mod errors;
mod models;
mod schema;
mod state;
pub mod users;
pub use crate::config::Config;
pub use crate::db::{DatabaseUri, DbPool};
pub use crate::errors::{Error, UserError};
pub use crate::state::State;

14
core/src/state.rs Normal file
View File

@ -0,0 +1,14 @@
use std::sync::Arc;
use crate::{DbPool};
#[derive(Clone)]
pub struct State {
db: Arc<DbPool>,
}
impl State {
pub fn new(db: Arc<DbPool>) -> Self {
State { db }
}
}

View File

@ -4,5 +4,5 @@ use warp::Filter;
use crate::render::render_template;
pub fn get_index() -> Resp!() {
warp::any().and_then(|| render_template("base/index.html", Context::new()))
warp::path::end().and_then(|| render_template("base/index.html", Context::new()))
}

View File

@ -1,11 +1,11 @@
#[macro_use]
extern crate serde_derive;
use core::{DbPool, Error};
use core::{Error, State};
use lazy_static::lazy_static;
use packer::Packer;
use tera::Tera;
use warp::{filters::BoxedFilter, http::Response, Filter, Reply};
use warp::{filters::BoxedFilter, http::Response, Filter, Rejection, Reply};
#[macro_use]
mod macros;
@ -36,11 +36,20 @@ lazy_static! {
};
}
pub fn routes(db: &DbPool) -> BoxedFilter<(impl Reply,)> {
fn set<T: 'static + Clone + Send + Sync>(
t: T,
) -> impl Clone + Filter<Extract = (), Error = Rejection> {
warp::any()
.map(move || warp::ext::set(t.clone()))
.and_then(|()| -> Result<(), Rejection> { Ok(()) })
.untuple_one()
}
pub fn routes(state: State) -> BoxedFilter<(impl Reply,)> {
let routes = route_any! {
GET () => base::get_index(),
GET ("users" / "register") => users::get_register(),
POST ("users" / "register") => users::post_register(),
GET () => base::get_index(),
}
.recover(Error::reply);
@ -48,5 +57,6 @@ pub fn routes(db: &DbPool) -> BoxedFilter<(impl Reply,)> {
.and(warp::path::param())
.and_then(|path: String| Assets::get(&path).ok_or_else(warp::reject::not_found))
.map(|contents| Response::builder().body(contents));
statics.or(routes).boxed()
set(state).and(statics.or(routes)).boxed()
}

View File

@ -1,6 +1,7 @@
use std::net::SocketAddr;
use std::sync::Arc;
use core::DatabaseUri;
use core::{State, DatabaseUri};
use structopt::StructOpt;
#[derive(StructOpt)]
@ -15,5 +16,6 @@ fn main() {
.database_uri
.establish_connection()
.expect("couldn't connect to db");
warp::serve(frontend::routes(&db)).run(opt.addr);
let state = State::new(Arc::new(db));
warp::serve(frontend::routes(state)).run(opt.addr);
}