From d86077d929c8ce389d0ae7a55d5997dbe4a93204 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 28 Jan 2018 18:03:43 +1100 Subject: [PATCH] Move extern statements to modules where they are used This will allow for easier feature configuration in the future. --- src/assets.rs | 12 +++++------- src/main.rs | 8 -------- src/rest.rs | 32 ++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index d24e4a2..7d9611e 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,10 +1,10 @@ /// Serves static files from a asset directory. +extern crate mime_guess; + use std::borrow::Cow; -use hyper::header::ContentType; - -use mime_guess::{get_mime_type, octet_stream}; +use assets::mime_guess::{get_mime_type, octet_stream}; // Include built-in files include!(concat!(env!("OUT_DIR"), "/data.rs")); @@ -12,7 +12,7 @@ include!(concat!(env!("OUT_DIR"), "/data.rs")); /// Returns a static file based upon a given String as a Path. /// /// file_path: String path, beginning with a / -pub fn file_from_string(file_path: &str) -> Option<(ContentType, Cow<'static, [u8]>)> { +pub fn file_from_string(file_path: &str) -> Option<(String, Cow<'static, [u8]>)> { let guessed_mime = match file_path.rfind(".") { Some(ext_ptr) => { let ext = &file_path[ext_ptr + 1..]; @@ -24,11 +24,9 @@ pub fn file_from_string(file_path: &str) -> Option<(ContentType, Cow<'static, [u let string_mime = guessed_mime.to_string(); - let content_type = ContentType(string_mime.parse().unwrap()); - // We already get the / from the HTTP request. match FILES.get(&format!("static{}", file_path)) { - Ok(val) => Some((content_type, val)), + Ok(val) => Some((string_mime, val)), // Only error is a not found one Err(_) => None, } diff --git a/src/main.rs b/src/main.rs index 4d5af29..83a22de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,6 @@ extern crate web_view; -extern crate futures; -extern crate hyper; -extern crate hyper_tls; -extern crate mime_guess; -extern crate tokio_core; - extern crate includedir; extern crate phf; @@ -17,8 +11,6 @@ extern crate serde_derive; extern crate serde_json; extern crate toml; -extern crate nfd; - mod assets; mod rest; mod config; diff --git a/src/rest.rs b/src/rest.rs index 086b39a..e05702a 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -3,18 +3,23 @@ /// Provides a HTTP/REST server for both frontend<->backend communication, as well /// as talking to external applications. -use nfd; -use nfd::Response as NfdResponse; +extern crate futures; +extern crate hyper; +extern crate hyper_tls; +extern crate tokio_core; + +extern crate nfd; + +use rest::nfd::Response as NfdResponse; use serde_json; -use futures::future; -use futures::future::FutureResult; +use rest::futures::future; +use rest::futures::future::FutureResult; -use hyper; -use hyper::{Error as HyperError, Get, StatusCode}; -use hyper::header::{ContentLength, ContentType}; -use hyper::server::{Http, Request, Response, Service}; +use rest::hyper::{Error as HyperError, Get, StatusCode}; +use rest::hyper::header::{ContentLength, ContentType}; +use rest::hyper::server::{Http, Request, Response, Service}; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::thread::{self, JoinHandle}; @@ -149,10 +154,13 @@ impl Service for WebService { println!("Trying {} => {}", req.path(), path); match assets::file_from_string(&path) { - Some((content_type, file)) => Response::::new() - .with_header(ContentLength(file.len() as u64)) - .with_header(content_type) - .with_body(file), + Some((content_type, file)) => { + let content_type = ContentType(content_type.parse().unwrap()); + Response::::new() + .with_header(ContentLength(file.len() as u64)) + .with_header(content_type) + .with_body(file) + }, None => Response::new().with_status(StatusCode::NotFound), } }