mirror of https://github.com/EasyCTF/librectf
...
This commit is contained in:
parent
1e8ab72e37
commit
9e772edc45
|
@ -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,
|
||||
}
|
|
@ -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),
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
}
|
|
@ -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()))
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue