mirror of https://github.com/raftario/filite.git
Convert to std::future
This commit is contained in:
parent
0e4b47fea9
commit
07208152e3
File diff suppressed because it is too large
Load Diff
16
Cargo.toml
16
Cargo.toml
|
@ -15,20 +15,20 @@ keywords = [
|
|||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
actix-files = "0.1.6"
|
||||
actix-identity = "0.1.0"
|
||||
actix-web = "1.0.8"
|
||||
actix-files = "0.2.1"
|
||||
actix-identity = "0.2.1"
|
||||
actix-rt = "1.0.0"
|
||||
actix-web = "2.0.0"
|
||||
base64 = "0.11.0"
|
||||
blake2 = "0.8.1"
|
||||
chrono = "0.4.9"
|
||||
chrono = "0.4.10"
|
||||
diesel_migrations = "1.4.0"
|
||||
dirs = "2.0.2"
|
||||
dotenv = { version = "0.15.0", optional = true }
|
||||
env_logger = "0.7.1"
|
||||
futures = "0.1.29"
|
||||
lazy_static = "1.4.0"
|
||||
num_cpus = "1.10.1"
|
||||
toml = "0.5.3"
|
||||
num_cpus = "1.11.1"
|
||||
toml = "0.5.5"
|
||||
[dependencies.diesel]
|
||||
version = "1.4.3"
|
||||
features = ["r2d2", "sqlite"]
|
||||
|
@ -36,7 +36,7 @@ features = ["r2d2", "sqlite"]
|
|||
version = "0.16.0"
|
||||
features = ["bundled"]
|
||||
[dependencies.serde]
|
||||
version = "1.0.102"
|
||||
version = "1.0.104"
|
||||
features = ["derive"]
|
||||
|
||||
[features]
|
||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -33,9 +33,10 @@ pub mod setup;
|
|||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
#[cfg(not(feature = "dev"))]
|
||||
embed_migrations!("./migrations");
|
||||
embed_migrations!();
|
||||
|
||||
fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() {
|
||||
let config = {
|
||||
#[cfg(feature = "dev")]
|
||||
{
|
||||
|
@ -75,7 +76,7 @@ fn main() {
|
|||
};
|
||||
|
||||
let port = config.port;
|
||||
let max_filesize = (config.max_filesize as f64 * 1.37) as usize;
|
||||
let max_filesize_json = (config.max_filesize as f64 * 1.37) as usize;
|
||||
|
||||
println!("Listening on port {}", port);
|
||||
|
||||
|
@ -93,29 +94,29 @@ fn main() {
|
|||
.route("/", web::get().to(routes::index))
|
||||
.route("/logout", web::get().to(routes::logout))
|
||||
.route("/config", web::get().to(routes::get_config))
|
||||
.route("/f", web::get().to_async(routes::files::gets))
|
||||
.route("/l", web::get().to_async(routes::links::gets))
|
||||
.route("/t", web::get().to_async(routes::texts::gets))
|
||||
.route("/f/{id}", web::get().to_async(routes::files::get))
|
||||
.route("/l/{id}", web::get().to_async(routes::links::get))
|
||||
.route("/t/{id}", web::get().to_async(routes::texts::get))
|
||||
.route("/f", web::get().to(routes::files::gets))
|
||||
.route("/l", web::get().to(routes::links::gets))
|
||||
.route("/t", web::get().to(routes::texts::gets))
|
||||
.route("/f/{id}", web::get().to(routes::files::get))
|
||||
.route("/l/{id}", web::get().to(routes::links::get))
|
||||
.route("/t/{id}", web::get().to(routes::texts::get))
|
||||
.service(
|
||||
web::resource("/f/{id}")
|
||||
.data(web::Json::<routes::files::PutFile>::configure(|cfg| {
|
||||
cfg.limit(max_filesize)
|
||||
cfg.limit(max_filesize_json)
|
||||
}))
|
||||
.route(web::put().to_async(routes::files::put))
|
||||
.route(web::delete().to_async(routes::files::delete)),
|
||||
.route(web::put().to(routes::files::put))
|
||||
.route(web::delete().to(routes::files::delete)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/l/{id}")
|
||||
.route(web::put().to_async(routes::links::put))
|
||||
.route(web::delete().to_async(routes::links::delete)),
|
||||
.route(web::put().to(routes::links::put))
|
||||
.route(web::delete().to(routes::links::delete)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/t/{id}")
|
||||
.route(web::put().to_async(routes::texts::put))
|
||||
.route(web::delete().to_async(routes::texts::delete)),
|
||||
.route(web::put().to(routes::texts::put))
|
||||
.route(web::delete().to(routes::texts::delete)),
|
||||
)
|
||||
})
|
||||
.bind(&format!("localhost:{}", port))
|
||||
|
@ -124,6 +125,7 @@ fn main() {
|
|||
process::exit(1);
|
||||
})
|
||||
.run()
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("Can't start webserver: {}.", e);
|
||||
process::exit(1);
|
||||
|
|
291
src/routes.rs
291
src/routes.rs
|
@ -2,11 +2,12 @@
|
|||
|
||||
use crate::setup::{self, Config};
|
||||
use actix_identity::Identity;
|
||||
use actix_web::{error::BlockingError, web, HttpRequest, HttpResponse, Responder};
|
||||
use actix_web::{error::BlockingError, web, Error, HttpRequest, HttpResponse, Responder};
|
||||
use base64;
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use diesel;
|
||||
use serde::Serialize;
|
||||
use std::convert::Infallible;
|
||||
|
||||
#[cfg(feature = "dev")]
|
||||
use crate::get_env;
|
||||
|
@ -21,8 +22,7 @@ fn parse_id(id: &str) -> Result<i32, HttpResponse> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Checks for authentication
|
||||
fn auth(
|
||||
async fn auth(
|
||||
identity: Identity,
|
||||
request: HttpRequest,
|
||||
password_hash: &[u8],
|
||||
|
@ -50,8 +50,10 @@ fn auth(
|
|||
let connection_string = header.replace("Basic ", "");
|
||||
let (user, password) = match base64::decode(&connection_string) {
|
||||
Ok(c) => {
|
||||
let credentials: Vec<Vec<u8>> =
|
||||
c.splitn(2, |b| b == &b':').map(|s| s.to_vec()).collect();
|
||||
let credentials: Vec<Vec<u8>> = c
|
||||
.splitn(2, |b| b == &b':')
|
||||
.map(|s| s.to_vec())
|
||||
.collect::<Vec<Vec<u8>>>();
|
||||
match credentials.len() {
|
||||
2 => (credentials[0].clone(), credentials[1].clone()),
|
||||
_ => return Err(HttpResponse::BadRequest().body("Invalid Authorization header")),
|
||||
|
@ -60,7 +62,8 @@ fn auth(
|
|||
Err(_) => return Err(HttpResponse::BadRequest().body("Invalid Authorization header")),
|
||||
};
|
||||
|
||||
if setup::hash(password).as_slice() == password_hash {
|
||||
let infallible_hash = move || -> Result<Vec<u8>, Infallible> { Ok(setup::hash(password)) };
|
||||
if web::block(infallible_hash).await.unwrap().as_slice() == password_hash {
|
||||
match String::from_utf8(user.to_vec()) {
|
||||
Ok(u) => {
|
||||
identity.remember(u);
|
||||
|
@ -79,24 +82,30 @@ fn auth(
|
|||
#[inline(always)]
|
||||
fn match_replace_result<T: Serialize>(
|
||||
result: Result<T, BlockingError<diesel::result::Error>>,
|
||||
) -> Result<HttpResponse, HttpResponse> {
|
||||
) -> Result<HttpResponse, Error> {
|
||||
match result {
|
||||
Ok(x) => Ok(HttpResponse::Created().json(x)),
|
||||
Err(_) => Err(HttpResponse::InternalServerError().body("Internal server error")),
|
||||
Err(_) => Err(HttpResponse::InternalServerError()
|
||||
.body("Internal server error")
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles error from single GET queries using find
|
||||
#[inline(always)]
|
||||
fn match_find_error<T>(error: BlockingError<diesel::result::Error>) -> Result<T, HttpResponse> {
|
||||
fn match_find_error<T>(error: BlockingError<diesel::result::Error>) -> Result<T, Error> {
|
||||
match error {
|
||||
BlockingError::Error(e) => match e {
|
||||
diesel::result::Error::NotFound => Err(HttpResponse::NotFound().body("Not found")),
|
||||
_ => Err(HttpResponse::InternalServerError().body("Internal server error")),
|
||||
diesel::result::Error::NotFound => {
|
||||
Err(HttpResponse::NotFound().body("Not found").into())
|
||||
}
|
||||
_ => Err(HttpResponse::InternalServerError()
|
||||
.body("Internal server error")
|
||||
.into()),
|
||||
},
|
||||
BlockingError::Canceled => {
|
||||
Err(HttpResponse::InternalServerError().body("Internal server error"))
|
||||
}
|
||||
BlockingError::Canceled => Err(HttpResponse::InternalServerError()
|
||||
.body("Internal server error")
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,25 +119,22 @@ fn timestamp_to_last_modified(timestamp: i32) -> String {
|
|||
/// GET multiple entries
|
||||
macro_rules! select {
|
||||
($m:ident) => {
|
||||
pub fn gets(
|
||||
pub async fn gets(
|
||||
request: HttpRequest,
|
||||
query: actix_web::web::Query<SelectQuery>,
|
||||
pool: actix_web::web::Data<Pool>,
|
||||
identity: actix_identity::Identity,
|
||||
password_hash: actix_web::web::Data<Vec<u8>>,
|
||||
) -> impl futures::Future<Item = actix_web::HttpResponse, Error = actix_web::Error> {
|
||||
) -> Result<actix_web::HttpResponse, actix_web::Error> {
|
||||
crate::routes::auth(identity, request, &password_hash).await?;
|
||||
|
||||
let filters = crate::queries::SelectFilters::from(query.into_inner());
|
||||
futures::future::result(crate::routes::auth(identity, request, &password_hash))
|
||||
.and_then(move |_| {
|
||||
actix_web::web::block(move || crate::queries::$m::select(filters, pool)).then(
|
||||
|result| match result {
|
||||
Ok(x) => Ok(actix_web::HttpResponse::Ok().json(x)),
|
||||
Err(_) => Err(actix_web::HttpResponse::InternalServerError()
|
||||
.body("Internal server error")),
|
||||
},
|
||||
)
|
||||
})
|
||||
.from_err()
|
||||
match actix_web::web::block(move || crate::queries::$m::select(filters, pool)).await {
|
||||
Ok(x) => Ok(actix_web::HttpResponse::Ok().json(x)),
|
||||
Err(_) => Err(actix_web::HttpResponse::InternalServerError()
|
||||
.body("Internal server error")
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -136,24 +142,20 @@ macro_rules! select {
|
|||
/// DELETE an entry
|
||||
macro_rules! delete {
|
||||
($m:ident) => {
|
||||
pub fn delete(
|
||||
pub async fn delete(
|
||||
request: HttpRequest,
|
||||
path: actix_web::web::Path<String>,
|
||||
pool: actix_web::web::Data<Pool>,
|
||||
identity: actix_identity::Identity,
|
||||
password_hash: actix_web::web::Data<Vec<u8>>,
|
||||
) -> impl futures::Future<Item = actix_web::HttpResponse, Error = actix_web::Error> {
|
||||
futures::future::result(crate::routes::auth(identity, request, &password_hash))
|
||||
.and_then(move |_| futures::future::result(crate::routes::parse_id(&path)))
|
||||
.and_then(move |id| {
|
||||
actix_web::web::block(move || crate::queries::$m::delete(id, pool)).then(
|
||||
|result| match result {
|
||||
Ok(()) => Ok(actix_web::HttpResponse::NoContent().body("Deleted")),
|
||||
Err(e) => crate::routes::match_find_error(e),
|
||||
},
|
||||
)
|
||||
})
|
||||
.from_err()
|
||||
) -> Result<actix_web::HttpResponse, actix_web::Error> {
|
||||
crate::routes::auth(identity, request, &password_hash).await?;
|
||||
|
||||
let id = crate::routes::parse_id(&path)?;
|
||||
match actix_web::web::block(move || crate::queries::$m::delete(id, pool)).await {
|
||||
Ok(()) => Ok(actix_web::HttpResponse::NoContent().body("Deleted")),
|
||||
Err(e) => crate::routes::match_find_error(e),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -195,12 +197,12 @@ lazy_static! {
|
|||
}
|
||||
|
||||
/// Index page letting users upload via a UI
|
||||
pub fn index(
|
||||
pub async fn index(
|
||||
request: HttpRequest,
|
||||
identity: Identity,
|
||||
password_hash: web::Data<Vec<u8>>,
|
||||
) -> impl Responder {
|
||||
if let Err(response) = auth(identity, request, &password_hash) {
|
||||
if let Err(response) = auth(identity, request, &password_hash).await {
|
||||
return response;
|
||||
}
|
||||
|
||||
|
@ -215,7 +217,7 @@ pub fn index(
|
|||
}
|
||||
#[cfg(not(feature = "dev"))]
|
||||
{
|
||||
INDEX_CONTENTS.clone()
|
||||
(&*INDEX_CONTENTS).clone()
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -225,20 +227,20 @@ pub fn index(
|
|||
}
|
||||
|
||||
/// GET the config info
|
||||
pub fn get_config(
|
||||
pub async fn get_config(
|
||||
request: HttpRequest,
|
||||
config: web::Data<Config>,
|
||||
identity: Identity,
|
||||
password_hash: web::Data<Vec<u8>>,
|
||||
) -> impl Responder {
|
||||
match auth(identity, request, &password_hash) {
|
||||
match auth(identity, request, &password_hash).await {
|
||||
Ok(_) => HttpResponse::Ok().json(config.get_ref()),
|
||||
Err(response) => response,
|
||||
}
|
||||
}
|
||||
|
||||
/// Logout route
|
||||
pub fn logout(identity: Identity) -> impl Responder {
|
||||
pub async fn logout(identity: Identity) -> impl Responder {
|
||||
if identity.identity().is_some() {
|
||||
identity.forget();
|
||||
HttpResponse::Ok().body("Logged out")
|
||||
|
@ -260,34 +262,28 @@ pub mod files {
|
|||
use actix_identity::Identity;
|
||||
use actix_web::{error::BlockingError, http, web, Error, HttpRequest, HttpResponse};
|
||||
use chrono::Utc;
|
||||
use futures::{future, Future};
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
select!(files);
|
||||
|
||||
/// GET a file entry and statically serve it
|
||||
pub fn get(
|
||||
pub async fn get(
|
||||
path: web::Path<String>,
|
||||
pool: web::Data<Pool>,
|
||||
config: web::Data<Config>,
|
||||
) -> impl Future<Item = NamedFile, Error = Error> {
|
||||
future::result(parse_id(&path))
|
||||
.and_then(move |id| {
|
||||
web::block(move || queries::files::find(id, pool)).then(
|
||||
move |result| match result {
|
||||
Ok(file) => {
|
||||
let mut path = config.files_dir.clone();
|
||||
path.push(file.filepath);
|
||||
match NamedFile::open(&path) {
|
||||
Ok(nf) => Ok(nf),
|
||||
Err(_) => Err(HttpResponse::NotFound().body("Not found")),
|
||||
}
|
||||
}
|
||||
Err(e) => match_find_error(e),
|
||||
},
|
||||
)
|
||||
})
|
||||
.from_err()
|
||||
) -> Result<NamedFile, Error> {
|
||||
let id = parse_id(&path)?;
|
||||
match web::block(move || queries::files::find(id, pool)).await {
|
||||
Ok(file) => {
|
||||
let mut path = config.files_dir.clone();
|
||||
path.push(file.filepath);
|
||||
match NamedFile::open(&path) {
|
||||
Ok(nf) => Ok(nf),
|
||||
Err(_) => Err(HttpResponse::NotFound().body("Not found").into()),
|
||||
}
|
||||
}
|
||||
Err(e) => match_find_error(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Request body when PUTting files
|
||||
|
@ -298,7 +294,7 @@ pub mod files {
|
|||
}
|
||||
|
||||
/// PUT a new file entry
|
||||
pub fn put(
|
||||
pub async fn put(
|
||||
request: HttpRequest,
|
||||
path: web::Path<String>,
|
||||
body: web::Json<PutFile>,
|
||||
|
@ -306,51 +302,50 @@ pub mod files {
|
|||
config: web::Data<Config>,
|
||||
identity: Identity,
|
||||
password_hash: web::Data<Vec<u8>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
future::result(auth(identity, request, &password_hash))
|
||||
.and_then(move |_| future::result(parse_id(&path)))
|
||||
.and_then(move |id| {
|
||||
web::block(move || {
|
||||
let mut path = config.files_dir.clone();
|
||||
let mut relative_path = PathBuf::new();
|
||||
if fs::create_dir_all(&path).is_err() {
|
||||
return Err(http::StatusCode::from_u16(500).unwrap());
|
||||
}
|
||||
) -> Result<HttpResponse, Error> {
|
||||
auth(identity, request, &password_hash).await?;
|
||||
|
||||
let mut filename = body.filename.clone();
|
||||
filename = format!("{:x}.{}", Utc::now().timestamp(), filename);
|
||||
path.push(&filename);
|
||||
relative_path.push(&filename);
|
||||
let id = parse_id(&path)?;
|
||||
let result = web::block(move || {
|
||||
let mut path = config.files_dir.clone();
|
||||
let mut relative_path = PathBuf::new();
|
||||
if fs::create_dir_all(&path).is_err() {
|
||||
return Err(http::StatusCode::from_u16(500).unwrap());
|
||||
}
|
||||
|
||||
let relative_path = match relative_path.to_str() {
|
||||
Some(rp) => rp,
|
||||
None => return Err(http::StatusCode::from_u16(500).unwrap()),
|
||||
};
|
||||
let mut filename = body.filename.clone();
|
||||
filename = format!("{:x}.{}", Utc::now().timestamp(), filename);
|
||||
path.push(&filename);
|
||||
relative_path.push(&filename);
|
||||
|
||||
let contents = match base64::decode(&body.base64) {
|
||||
Ok(contents) => contents,
|
||||
Err(_) => return Err(http::StatusCode::from_u16(400).unwrap()),
|
||||
};
|
||||
if fs::write(&path, contents).is_err() {
|
||||
return Err(http::StatusCode::from_u16(500).unwrap());
|
||||
}
|
||||
let relative_path = match relative_path.to_str() {
|
||||
Some(rp) => rp,
|
||||
None => return Err(http::StatusCode::from_u16(500).unwrap()),
|
||||
};
|
||||
|
||||
match queries::files::replace(id, relative_path, pool) {
|
||||
Ok(file) => Ok(file),
|
||||
Err(_) => Err(http::StatusCode::from_u16(500).unwrap()),
|
||||
}
|
||||
})
|
||||
.then(|result| match result {
|
||||
Ok(file) => Ok(HttpResponse::Created().json(file)),
|
||||
Err(e) => match e {
|
||||
BlockingError::Error(sc) => Err(HttpResponse::new(sc)),
|
||||
BlockingError::Canceled => {
|
||||
Err(HttpResponse::InternalServerError().body("Internal server error"))
|
||||
}
|
||||
},
|
||||
})
|
||||
})
|
||||
.from_err()
|
||||
let contents = match base64::decode(&body.base64) {
|
||||
Ok(contents) => contents,
|
||||
Err(_) => return Err(http::StatusCode::from_u16(400).unwrap()),
|
||||
};
|
||||
if fs::write(&path, contents).is_err() {
|
||||
return Err(http::StatusCode::from_u16(500).unwrap());
|
||||
}
|
||||
|
||||
match queries::files::replace(id, relative_path, pool) {
|
||||
Ok(file) => Ok(file),
|
||||
Err(_) => Err(http::StatusCode::from_u16(500).unwrap()),
|
||||
}
|
||||
})
|
||||
.await;
|
||||
match result {
|
||||
Ok(file) => Ok(HttpResponse::Created().json(file)),
|
||||
Err(e) => match e {
|
||||
BlockingError::Error(sc) => Err(HttpResponse::new(sc).into()),
|
||||
BlockingError::Canceled => Err(HttpResponse::InternalServerError()
|
||||
.body("Internal server error")
|
||||
.into()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
delete!(files);
|
||||
|
@ -366,26 +361,22 @@ pub mod links {
|
|||
};
|
||||
use actix_identity::Identity;
|
||||
use actix_web::{web, Error, HttpRequest, HttpResponse};
|
||||
use futures::{future, Future};
|
||||
|
||||
select!(links);
|
||||
|
||||
/// GET a link entry and redirect to it
|
||||
pub fn get(
|
||||
pub async fn get(
|
||||
path: web::Path<String>,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
future::result(parse_id(&path))
|
||||
.and_then(move |id| {
|
||||
web::block(move || queries::links::find(id, pool)).then(|result| match result {
|
||||
Ok(link) => Ok(HttpResponse::Found()
|
||||
.header("Location", link.forward)
|
||||
.header("Last-Modified", timestamp_to_last_modified(link.created))
|
||||
.finish()),
|
||||
Err(e) => match_find_error(e),
|
||||
})
|
||||
})
|
||||
.from_err()
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let id = parse_id(&path)?;
|
||||
match web::block(move || queries::links::find(id, pool)).await {
|
||||
Ok(link) => Ok(HttpResponse::Found()
|
||||
.header("Location", link.forward)
|
||||
.header("Last-Modified", timestamp_to_last_modified(link.created))
|
||||
.finish()),
|
||||
Err(e) => match_find_error(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Request body when PUTting links
|
||||
|
@ -395,21 +386,20 @@ pub mod links {
|
|||
}
|
||||
|
||||
/// PUT a new link entry
|
||||
pub fn put(
|
||||
pub async fn put(
|
||||
request: HttpRequest,
|
||||
path: web::Path<String>,
|
||||
body: web::Json<PutLink>,
|
||||
pool: web::Data<Pool>,
|
||||
identity: Identity,
|
||||
password_hash: web::Data<Vec<u8>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
future::result(auth(identity, request, &password_hash))
|
||||
.and_then(move |_| future::result(parse_id(&path)))
|
||||
.and_then(move |id| {
|
||||
web::block(move || queries::links::replace(id, &body.forward, pool))
|
||||
.then(match_replace_result)
|
||||
})
|
||||
.from_err()
|
||||
) -> Result<HttpResponse, Error> {
|
||||
auth(identity, request, &password_hash).await?;
|
||||
|
||||
let id = parse_id(&path)?;
|
||||
match_replace_result(
|
||||
web::block(move || queries::links::replace(id, &body.forward, pool)).await,
|
||||
)
|
||||
}
|
||||
|
||||
delete!(links);
|
||||
|
@ -425,25 +415,21 @@ pub mod texts {
|
|||
};
|
||||
use actix_identity::Identity;
|
||||
use actix_web::{web, Error, HttpRequest, HttpResponse};
|
||||
use futures::{future, Future};
|
||||
|
||||
select!(texts);
|
||||
|
||||
/// GET a text entry and display it
|
||||
pub fn get(
|
||||
pub async fn get(
|
||||
path: web::Path<String>,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
future::result(parse_id(&path))
|
||||
.and_then(move |id| {
|
||||
web::block(move || queries::texts::find(id, pool)).then(|result| match result {
|
||||
Ok(text) => Ok(HttpResponse::Ok()
|
||||
.header("Last-Modified", timestamp_to_last_modified(text.created))
|
||||
.body(text.contents)),
|
||||
Err(e) => match_find_error(e),
|
||||
})
|
||||
})
|
||||
.from_err()
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let id = parse_id(&path)?;
|
||||
match web::block(move || queries::texts::find(id, pool)).await {
|
||||
Ok(text) => Ok(HttpResponse::Ok()
|
||||
.header("Last-Modified", timestamp_to_last_modified(text.created))
|
||||
.body(text.contents)),
|
||||
Err(e) => match_find_error(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Request body when PUTting texts
|
||||
|
@ -453,21 +439,20 @@ pub mod texts {
|
|||
}
|
||||
|
||||
/// PUT a new text entry
|
||||
pub fn put(
|
||||
pub async fn put(
|
||||
request: HttpRequest,
|
||||
path: web::Path<String>,
|
||||
body: web::Json<PutText>,
|
||||
pool: web::Data<Pool>,
|
||||
identity: Identity,
|
||||
password_hash: web::Data<Vec<u8>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
future::result(auth(identity, request, &password_hash))
|
||||
.and_then(move |_| future::result(parse_id(&path)))
|
||||
.and_then(move |id| {
|
||||
web::block(move || queries::texts::replace(id, &body.contents, pool))
|
||||
.then(match_replace_result)
|
||||
})
|
||||
.from_err()
|
||||
) -> Result<HttpResponse, Error> {
|
||||
auth(identity, request, &password_hash).await?;
|
||||
|
||||
let id = parse_id(&path)?;
|
||||
match_replace_result(
|
||||
web::block(move || queries::texts::replace(id, &body.contents, pool)).await,
|
||||
)
|
||||
}
|
||||
|
||||
delete!(texts);
|
||||
|
|
Loading…
Reference in New Issue