Skip to content

Commit

Permalink
Remove dead code and make some functions private
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Mar 21, 2021
1 parent 228ee9f commit ea6e643
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
37 changes: 5 additions & 32 deletions src/web/file.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! Database based file handler
use crate::storage::{Blob, Storage};
use crate::{error::Result, Config, Metrics};
use iron::{status, Handler, IronResult, Request, Response};
use crate::{error::Result, Config};
use iron::{status, Response};

#[derive(Debug)]
pub(crate) struct File(pub(crate) Blob);

impl File {
/// Gets file from database
pub fn from_path(storage: &Storage, path: &str, config: &Config) -> Result<File> {
pub(super) fn from_path(storage: &Storage, path: &str, config: &Config) -> Result<File> {
let max_size = if path.ends_with(".html") {
config.max_file_size_html
} else {
Expand All @@ -20,7 +20,7 @@ impl File {
}

/// Consumes File and creates a iron response
pub fn serve(self) -> Response {
pub(super) fn serve(self) -> Response {
use iron::headers::{CacheControl, CacheDirective, ContentType, HttpDate, LastModified};

let mut response = Response::with((status::Ok, self.0.content));
Expand All @@ -44,38 +44,11 @@ impl File {
}

/// Checks if mime type of file is "application/x-empty"
pub fn is_empty(&self) -> bool {
pub(super) fn is_empty(&self) -> bool {
self.0.mime == "application/x-empty"
}
}

/// Database based file handler for iron
///
/// This is similar to staticfile crate, but its using getting files from database.
pub struct DatabaseFileHandler;

impl Handler for DatabaseFileHandler {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let path = req.url.path().join("/");
let storage = extension!(req, Storage);
let config = extension!(req, Config);
if let Ok(file) = File::from_path(&storage, &path, &config) {
let metrics = extension!(req, Metrics);

// Because all requests that don't hit another handler go through here, we will get all
// requests successful or not recorded by the RequestRecorder, so we inject an extra
// "database success" route to keep track of how often we succeed vs 404
metrics
.routes_visited
.with_label_values(&["database success"])
.inc();
Ok(file.serve())
} else {
Err(super::error::Nope::CrateNotFound.into())
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions src/web/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use iron::status::Status;
use prometheus::{Encoder, HistogramVec, TextEncoder};
use std::time::{Duration, Instant};

pub fn metrics_handler(req: &mut Request) -> IronResult<Response> {
pub(super) fn metrics_handler(req: &mut Request) -> IronResult<Response> {
let metrics = extension!(req, Metrics);
let pool = extension!(req, Pool);
let queue = extension!(req, BuildQueue);
Expand All @@ -30,7 +30,7 @@ fn duration_to_seconds(d: Duration) -> f64 {
d.as_secs() as f64 + nanos
}

pub struct RequestRecorder {
pub(super) struct RequestRecorder {
handler: Box<dyn iron::Handler>,
route_name: String,
}
Expand Down

0 comments on commit ea6e643

Please sign in to comment.