Skip to content

Use http types from http crate #2389

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

Merged
merged 6 commits into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 23 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ lettre = "0.9"
lettre_email = "0.9"
failure = "0.1.1"

conduit = "0.9.0-alpha.0"
conduit-conditional-get = "0.9.0-alpha.0"
conduit-cookie = "0.9.0-alpha.0"
conduit = "0.9.0-alpha.2"
conduit-conditional-get = "0.9.0-alpha.2"
conduit-cookie = "0.9.0-alpha.2"
cookie = { version = "0.12", features = ["secure"] }
conduit-middleware = "0.9.0-alpha.0"
conduit-router = "0.9.0-alpha.0"
conduit-static = "0.9.0-alpha.0"
conduit-git-http-backend = "0.9.0-alpha.0"
civet = "0.12.0-alpha.1"
conduit-hyper = "0.3.0-alpha.0"
conduit-middleware = "0.9.0-alpha.2"
conduit-router = "0.9.0-alpha.2"
conduit-static = "0.9.0-alpha.2"
conduit-git-http-backend = "0.9.0-alpha.2"
civet = "0.12.0-alpha.3"
conduit-hyper = "0.3.0-alpha.2"
http = "0.2"

futures-util = "0.3"
futures-channel = { version = "0.3.1", default-features = false }
Expand All @@ -86,7 +87,7 @@ indexmap = "1.0.2"
handlebars = "3.0.1"

[dev-dependencies]
conduit-test = "0.9.0-alpha.0"
conduit-test = "0.9.0-alpha.2"
hyper-tls = "0.4"
lazy_static = "1.0"
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
Expand Down
37 changes: 16 additions & 21 deletions src/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ mod prelude {
pub use super::helpers::ok_true;
pub use diesel::prelude::*;

pub use conduit::{Request, Response};
pub use conduit::{header, RequestExt, StatusCode};
pub use conduit_router::RequestParams;

pub use crate::db::RequestTransaction;
pub use crate::util::errors::{cargo_err, AppError, AppResult, ChainError}; // TODO: Remove cargo_err from here

pub use crate::middleware::app::RequestApp;

use std::collections::HashMap;
use std::io;
pub use crate::util::errors::{cargo_err, AppError, AppResult, ChainError}; // TODO: Remove cargo_err from here
pub use crate::util::{AppResponse, EndpointResult};

use indexmap::IndexMap;
use serde::Serialize;
Expand All @@ -33,18 +30,18 @@ mod prelude {
}

pub trait RequestUtils {
fn redirect(&self, url: String) -> Response;
fn redirect(&self, url: String) -> AppResponse;

fn json<T: Serialize>(&self, t: &T) -> Response;
fn json<T: Serialize>(&self, t: &T) -> AppResponse;
fn query(&self) -> IndexMap<String, String>;
fn wants_json(&self) -> bool;
fn query_with_params(&self, params: IndexMap<String, String>) -> String;

fn log_metadata<V: std::fmt::Display>(&mut self, key: &'static str, value: V);
}

impl<'a> RequestUtils for dyn Request + 'a {
fn json<T: Serialize>(&self, t: &T) -> Response {
impl<'a> RequestUtils for dyn RequestExt + 'a {
fn json<T: Serialize>(&self, t: &T) -> AppResponse {
crate::util::json_response(t)
}

Expand All @@ -54,21 +51,19 @@ mod prelude {
.collect()
}

fn redirect(&self, url: String) -> Response {
let mut headers = HashMap::new();
headers.insert("Location".to_string(), vec![url]);
Response {
status: (302, "Found"),
headers,
body: Box::new(io::empty()),
}
fn redirect(&self, url: String) -> AppResponse {
conduit::Response::builder()
.status(StatusCode::FOUND)
.header(header::LOCATION, url)
.body(conduit::Body::empty())
.unwrap() // Should not panic unless url contains "\r\n"
}

fn wants_json(&self) -> bool {
self.headers()
.find("Accept")
.map(|accept| accept.iter().any(|s| s.contains("json")))
.unwrap_or(false)
.get_all(header::ACCEPT)
.iter()
.any(|val| val.to_str().unwrap_or_default().contains("json"))
}

fn query_with_params(&self, new_params: IndexMap<String, String>) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::schema::categories;
use crate::views::{EncodableCategory, EncodableCategoryWithSubcategories};

/// Handles the `GET /categories` route.
pub fn index(req: &mut dyn Request) -> AppResult<Response> {
pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
let conn = req.db_conn()?;
let query = req.query();
// FIXME: There are 69 categories, 47 top level. This isn't going to
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn index(req: &mut dyn Request) -> AppResult<Response> {
}

/// Handles the `GET /categories/:category_id` route.
pub fn show(req: &mut dyn Request) -> AppResult<Response> {
pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
let slug = &req.params()["category_id"];
let conn = req.db_conn()?;
let cat = Category::by_slug(slug).first::<Category>(&*conn)?;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn show(req: &mut dyn Request) -> AppResult<Response> {
}

/// Handles the `GET /category_slugs` route.
pub fn slugs(req: &mut dyn Request) -> AppResult<Response> {
pub fn slugs(req: &mut dyn RequestExt) -> EndpointResult {
let conn = req.db_conn()?;
let slugs = categories::table
.select((categories::slug, categories::slug, categories::description))
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::schema::{crate_owner_invitations, crate_owners};
use crate::views::{EncodableCrateOwnerInvitation, InvitationResponse};

/// Handles the `GET /me/crate_owner_invitations` route.
pub fn list(req: &mut dyn Request) -> AppResult<Response> {
pub fn list(req: &mut dyn RequestExt) -> EndpointResult {
let conn = &*req.db_conn()?;
let user_id = req.authenticate(conn)?.user_id();

Expand All @@ -31,7 +31,7 @@ struct OwnerInvitation {
}

/// Handles the `PUT /me/crate_owner_invitations/:crate_id` route.
pub fn handle_invite(req: &mut dyn Request) -> AppResult<Response> {
pub fn handle_invite(req: &mut dyn RequestExt) -> EndpointResult {
let mut body = String::new();
req.body().read_to_string(&mut body)?;

Expand All @@ -51,7 +51,7 @@ pub fn handle_invite(req: &mut dyn Request) -> AppResult<Response> {
}

/// Handles the `PUT /me/crate_owner_invitations/accept/:token` route.
pub fn handle_invite_with_token(req: &mut dyn Request) -> AppResult<Response> {
pub fn handle_invite_with_token(req: &mut dyn RequestExt) -> EndpointResult {
let conn = req.db_conn()?;
let req_token = &req.params()["token"];

Expand All @@ -72,11 +72,11 @@ pub fn handle_invite_with_token(req: &mut dyn Request) -> AppResult<Response> {
}

fn accept_invite(
req: &dyn Request,
req: &dyn RequestExt,
conn: &PgConnection,
crate_invite: InvitationResponse,
user_id: i32,
) -> AppResult<Response> {
) -> EndpointResult {
use diesel::{delete, insert_into};

conn.transaction(|| {
Expand Down Expand Up @@ -110,10 +110,10 @@ fn accept_invite(
}

fn decline_invite(
req: &dyn Request,
req: &dyn RequestExt,
conn: &PgConnection,
crate_invite: InvitationResponse,
) -> AppResult<Response> {
) -> EndpointResult {
use diesel::delete;

let user_id = req.authenticate(conn)?.user_id();
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::util::{errors::AppResult, json_response};
use conduit::Response;
use crate::util::{json_response, EndpointResult};

pub(crate) mod pagination;

pub(crate) use self::pagination::Paginate;

pub fn ok_true() -> AppResult<Response> {
pub fn ok_true() -> EndpointResult {
#[derive(Serialize)]
struct R {
ok: bool,
Expand Down
Loading