Skip to content

Commit 07db49e

Browse files
committed
Merge pull request #215 from Turbo87/doc
Documentation
2 parents 4fec724 + c53fa64 commit 07db49e

File tree

8 files changed

+34
-2
lines changed

8 files changed

+34
-2
lines changed

src/app.rs

+9
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ use curl::http;
1212

1313
use {db, Config};
1414

15+
/// The `App` struct holds the main components of the application like
16+
/// the database connection pool and configurations
1517
pub struct App {
18+
/// The database connection pool
1619
pub database: db::Pool,
20+
21+
/// The GitHub OAuth2 configuration
1722
pub github: oauth2::Config,
1823
pub bucket: s3::Bucket,
1924
pub s3_proxy: Option<String>,
2025
pub session_key: String,
2126
pub git_repo: Mutex<git2::Repository>,
2227
pub git_repo_checkout: PathBuf,
28+
29+
/// The server configuration
2330
pub config: Config,
2431
}
2532

33+
/// The `AppMiddleware` injects an `App` instance into the `Request` extensions
2634
pub struct AppMiddleware {
2735
app: Arc<App>
2836
}
@@ -82,6 +90,7 @@ impl Middleware for AppMiddleware {
8290
}
8391
}
8492

93+
/// Adds an `app()` method to the `Request` type returning the global `App` instance
8594
pub trait RequestApp {
8695
fn app(&self) -> &Arc<App>;
8796
}

src/bin/delete-crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::io::prelude::*;
1818

1919
use cargo_registry::Crate;
2020

21+
#[allow(dead_code)]
2122
fn main() {
2223
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
2324
&postgres::SslMode::None).unwrap();
@@ -97,4 +98,3 @@ fn delete(tx: &postgres::Transaction) {
9798
io::stdin().read_line(&mut line).unwrap();
9899
if !line.starts_with("y") { panic!("aborting transaction"); }
99100
}
100-

src/bin/delete-version.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::io::prelude::*;
1818

1919
use cargo_registry::{Crate, Version};
2020

21+
#[allow(dead_code)]
2122
fn main() {
2223
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
2324
&postgres::SslMode::None).unwrap();
@@ -75,4 +76,3 @@ fn delete(tx: &postgres::Transaction) {
7576
io::stdin().read_line(&mut line).unwrap();
7677
if !line.starts_with("y") { panic!("aborting transaction"); }
7778
}
78-

src/bin/migrate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use migrate::Migration;
1212
use cargo_registry::krate::Crate;
1313
use cargo_registry::model::Model;
1414

15+
#[allow(dead_code)]
1516
fn main() {
1617
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
1718
&postgres::SslMode::None).unwrap();

src/bin/populate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::env;
1515
use time::Duration;
1616
use rand::{StdRng, Rng};
1717

18+
#[allow(dead_code)]
1819
fn main() {
1920
let conn = postgres::Connection::connect(&env("DATABASE_URL")[..],
2021
&postgres::SslMode::None).unwrap();

src/bin/server.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::path::PathBuf;
1313
use std::sync::Arc;
1414
use std::sync::mpsc::channel;
1515

16+
#[allow(dead_code)]
1617
fn main() {
1718
env_logger::init().unwrap();
1819
let url = env("GIT_REPO_URL");

src/user/middleware.rs

+13
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ pub struct Middleware;
1313

1414
impl conduit_middleware::Middleware for Middleware {
1515
fn before(&self, req: &mut Request) -> Result<(), Box<Error+Send>> {
16+
// Check if the request has a session cookie with a `user_id` property inside
1617
let id = { req.session().get("user_id").and_then(|s| s.parse().ok()) };
18+
1719
let user = match id {
20+
21+
// `user_id` was found on the session
1822
Some(id) => {
23+
24+
// Look for a user in the database with the given `user_id`
1925
match User::find(try!(req.tx().map_err(std_error)), id) {
2026
Ok(user) => user,
2127
Err(..) => return Ok(()),
2228
}
2329
}
30+
31+
// `user_id` was *not* found on the session
2432
None => {
33+
34+
// Look for an `Authorization` header on the request
2535
let tx = try!(req.tx().map_err(std_error));
2636
match req.headers().find("Authorization") {
2737
Some(headers) => {
38+
39+
// Look for a user in the database with a matching API token
2840
match User::find_by_api_token(tx, &headers[0]) {
2941
Ok(user) => user,
3042
Err(..) => return Ok(())
@@ -35,6 +47,7 @@ impl conduit_middleware::Middleware for Middleware {
3547
}
3648
};
3749

50+
// Attach the `User` model from the database to the request
3851
req.mut_extensions().insert(user);
3952
Ok(())
4053
}

src/user/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub use self::middleware::{Middleware, RequestUser};
2020

2121
pub mod middleware;
2222

23+
/// The model representing a row in the `users` database table.
2324
#[derive(Clone, Debug, PartialEq, Eq)]
2425
pub struct User {
2526
pub id: i32,
@@ -31,6 +32,7 @@ pub struct User {
3132
pub api_token: String,
3233
}
3334

35+
/// The serialization format for the `User` model.
3436
#[derive(RustcDecodable, RustcEncodable)]
3537
pub struct EncodableUser {
3638
pub id: i32,
@@ -41,6 +43,7 @@ pub struct EncodableUser {
4143
}
4244

4345
impl User {
46+
/// Queries the database for a user with a certain `gh_login` value.
4447
pub fn find_by_login(conn: &GenericConnection,
4548
login: &str) -> CargoResult<User> {
4649
let stmt = try!(conn.prepare("SELECT * FROM users
@@ -52,6 +55,7 @@ impl User {
5255
Ok(Model::from_row(&row))
5356
}
5457

58+
/// Queries the database for a user with a certain `api_token` value.
5559
pub fn find_by_api_token(conn: &GenericConnection,
5660
token: &str) -> CargoResult<User> {
5761
let stmt = try!(conn.prepare("SELECT * FROM users \
@@ -62,6 +66,7 @@ impl User {
6266
})
6367
}
6468

69+
/// Updates a user or inserts a new user into the database.
6570
pub fn find_or_insert(conn: &GenericConnection,
6671
login: &str,
6772
email: Option<&str>,
@@ -102,10 +107,12 @@ impl User {
102107
}))))
103108
}
104109

110+
/// Generates a new crates.io API token.
105111
pub fn new_api_token() -> String {
106112
thread_rng().gen_ascii_chars().take(32).collect()
107113
}
108114

115+
/// Converts this `User` model into an `EncodableUser` for JSON serialization.
109116
pub fn encodable(self) -> EncodableUser {
110117
let User { id, email, api_token: _, gh_access_token: _,
111118
name, gh_login, avatar } = self;

0 commit comments

Comments
 (0)