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.
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,
}

View File

@ -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;

View File

@ -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::<hyper::Body>::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::<hyper::Body>::new()
.with_header(ContentLength(file.len() as u64))
.with_header(content_type)
.with_body(file)
},
None => Response::new().with_status(StatusCode::NotFound),
}
}