1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
//! Serving contents of static directory. //! //! ```no_run //! extern crate staticdir; //! extern crate iron; //! //! use iron::prelude::*; //! use staticdir::{ StaticDir, AsJson }; //! //! fn main() { //! Iron::new(StaticDir::new(".", AsJson)).http("localhost:3000").unwrap(); //! } //! ``` //! //! This will provide JSON similar to this: //! //!```ignore //! [ //! { //! "file_type": "File", // "File", "Dir" or "Symlink" //! "file_name": ".gitignore", //! "size": 7, //! "creation_time": null, // may be null on some Unix systems //! "last_modification_time": 1451939290, //! "last_access_time": 1451939309 //! }, //! { //! "file_type": "File", //! "file_name": "Cargo.toml", //! "size": 196, //! "creation_time": null, //! "last_modification_time": 1451939547, //! "last_access_time": 1451939547 //! }, //! { //! "file_type": "Dir", //! "file_name": "src", //! "size": 4096, //! "creation_time": null, //! "last_modification_time": 1451939462, //! "last_access_time": 1451939462 //! } //! ] //!``` //! //! Because for different tasks different implementations of response may be required, this crate is designed to provide flexible behavior. //! By default, only JSON response is supported, but different it can be customized with `ResponseStrategy` trait. //! Here is how easily we can provide directory contents as HTML. //! //! ```no_run //! extern crate staticdir; //! extern crate iron; //! //! use iron::prelude::*; //! use iron::status::Status; //! use staticdir::{ StaticDir, ResponseStrategy }; //! use std::fs::ReadDir; //! use iron::mime::Mime; //! //! struct AsHtml; //! //! fn build_html(dir: ReadDir) -> String { //! let mut html = String::new(); //! for entry in dir { //! let entry = entry.unwrap(); //! html = format!("{}<li>{}</li>", html, entry.file_name().into_string().unwrap()); //! } //! format!("<ul>{}</ul>", html) //! } //! //! impl ResponseStrategy for AsHtml { //! fn make_response(&self, dir: ReadDir) -> IronResult<Response> { //! let html = build_html(dir); //! let content_type = "text/html; charset=utf-8".parse::<Mime>().unwrap(); //! Ok(Response::with((Status::Ok, html, content_type))) //! } //! } //! //! fn main() { //! Iron::new(StaticDir::new(".", AsHtml)).http("localhost:3000").unwrap(); //! } //! ``` //! //! `StaticDir` implements `Handler` and `AfterMiddleware` so can be combined with other plugins of `iron` framework like `staticfile` or `mount`: //! //! ```no_run //! extern crate staticdir; //! extern crate iron; //! extern crate mount; //! extern crate staticfile; //! //! use iron::prelude::*; //! use mount::Mount; //! use staticdir::{ StaticDir, AsJson }; //! use staticfile::Static; //! //! //! fn main() { //! let root = "tests/mount"; //! let mut handle_statics = Chain::new(Static::new(root)); //! handle_statics.link_after(StaticDir::new(root, AsJson)); //! //! let mut mount = Mount::new(); //! mount.mount("/static/", handle_statics); //! let mut server = Iron::new(mount).http("localhost:3000").unwrap(); //! } //! ``` extern crate iron; extern crate rustc_serialize; extern crate url; extern crate filetime; pub use self::static_dir::StaticDir; pub use self::static_dir::ResponseStrategy; pub use self::respond_with_dir::AsJson; mod static_dir; pub mod errors; mod respond_with_dir;