Move extern statements to modules where they are used

This will allow for easier feature configuration in the future.
This commit is contained in:
James 2018-01-28 18:03:43 +11:00
parent 4ed50c885b
commit d86077d929
3 changed files with 25 additions and 27 deletions

View File

@ -1,10 +1,10 @@
/// Serves static files from a asset directory. /// Serves static files from a asset directory.
extern crate mime_guess;
use std::borrow::Cow; use std::borrow::Cow;
use hyper::header::ContentType; use assets::mime_guess::{get_mime_type, octet_stream};
use mime_guess::{get_mime_type, octet_stream};
// Include built-in files // Include built-in files
include!(concat!(env!("OUT_DIR"), "/data.rs")); 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. /// Returns a static file based upon a given String as a Path.
/// ///
/// file_path: String path, beginning with a / /// 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(".") { let guessed_mime = match file_path.rfind(".") {
Some(ext_ptr) => { Some(ext_ptr) => {
let ext = &file_path[ext_ptr + 1..]; 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 string_mime = guessed_mime.to_string();
let content_type = ContentType(string_mime.parse().unwrap());
// We already get the / from the HTTP request. // We already get the / from the HTTP request.
match FILES.get(&format!("static{}", file_path)) { 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 // Only error is a not found one
Err(_) => None, Err(_) => None,
} }

View File

@ -2,12 +2,6 @@
extern crate web_view; 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 includedir;
extern crate phf; extern crate phf;
@ -17,8 +11,6 @@ extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
extern crate toml; extern crate toml;
extern crate nfd;
mod assets; mod assets;
mod rest; mod rest;
mod config; mod config;

View File

@ -3,18 +3,23 @@
/// Provides a HTTP/REST server for both frontend<->backend communication, as well /// Provides a HTTP/REST server for both frontend<->backend communication, as well
/// as talking to external applications. /// as talking to external applications.
use nfd; extern crate futures;
use nfd::Response as NfdResponse; extern crate hyper;
extern crate hyper_tls;
extern crate tokio_core;
extern crate nfd;
use rest::nfd::Response as NfdResponse;
use serde_json; use serde_json;
use futures::future; use rest::futures::future;
use futures::future::FutureResult; use rest::futures::future::FutureResult;
use hyper; use rest::hyper::{Error as HyperError, Get, StatusCode};
use hyper::{Error as HyperError, Get, StatusCode}; use rest::hyper::header::{ContentLength, ContentType};
use hyper::header::{ContentLength, ContentType}; use rest::hyper::server::{Http, Request, Response, Service};
use hyper::server::{Http, Request, Response, Service};
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::thread::{self, JoinHandle}; use std::thread::{self, JoinHandle};
@ -149,10 +154,13 @@ impl Service for WebService {
println!("Trying {} => {}", req.path(), path); println!("Trying {} => {}", req.path(), path);
match assets::file_from_string(&path) { match assets::file_from_string(&path) {
Some((content_type, file)) => Response::<hyper::Body>::new() Some((content_type, file)) => {
.with_header(ContentLength(file.len() as u64)) let content_type = ContentType(content_type.parse().unwrap());
.with_header(content_type) Response::<hyper::Body>::new()
.with_body(file), .with_header(ContentLength(file.len() as u64))
.with_header(content_type)
.with_body(file)
},
None => Response::new().with_status(StatusCode::NotFound), None => Response::new().with_status(StatusCode::NotFound),
} }
} }