Skip to content

Commit b3a8829

Browse files
committed
docs: document flags, installing and options
1 parent 7aa5949 commit b3a8829

File tree

10 files changed

+187
-51
lines changed

10 files changed

+187
-51
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ version = "0.0.9"
44
authors = ["Esteban Borai <estebanborai@gmail.com>"]
55
edition = "2018"
66
license = "MIT"
7-
description = "Zero-configuration command-line HTTP server"
7+
description = "Command-line HTTP Server"
88
readme = "README.md"
99
repository = "https://github.com/EstebanBorai/http-server"
10-
categories = ["authentication", "encoding", "web-programming", "web-programming::http-server"]
10+
categories = ["web-programming", "web-programming::http-server"]
1111

1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,72 @@
33
<img src="https://raw.githubusercontent.com/EstebanBorai/http-server/main/docs/http-logo.png" height="120" width="200" />
44
</div>
55
<h1 align="center">http-server</h1>
6-
<h4 align="center">Zero-configuration command-line HTTP server</h4>
6+
<h4 align="center">Command-line HTTP Server</h4>
77
</div>
88

99
<div align="center">
1010

11+
[![Crates.io](https://img.shields.io/crates/v/http-server.svg)](https://crates.io/crates/http-server)
12+
[![Documentation](https://docs.rs/http-server/badge.svg)](https://docs.rs/http-server)
1113
![Build](https://github.com/EstebanBorai/http-server/workflows/build/badge.svg)
1214
![Lint](https://github.com/EstebanBorai/http-server/workflows/clippy/fmt/badge.svg)
1315
![Tests](https://github.com/EstebanBorai/http-server/workflows/tests/badge.svg)
1416

1517
</div>
1618

17-
## Modules
19+
## Index
1820

19-
This project is composed by 3 main modules:
21+
- [Installation](#installation)
22+
- [Usage](#usage)
23+
- [Flags](#flags)
24+
- [Options](#options)
25+
- [Contributing](#contributing)
26+
- [License](#license)
27+
- [Contribution](#contribution)
2028

21-
### The `cli` module
29+
## Installation
2230

23-
Which is in charge of gathering command-line arguments and options
31+
```bash
32+
cargo install http-server
33+
```
2434

25-
### The `config` module
35+
Check for the installation to be successful.
2636

27-
Defines the shape of the HTTP server configuration. This module acts on an input
28-
(the `cli` for instance), to build an instance of `Config`, which is then passed to
29-
the `server` module
37+
```bash
38+
http-server --help
39+
```
3040

31-
### The `server` module
41+
## Usage
3242

33-
The main logic for the _HTTP Server_. This module must receive a `Config` instance
34-
which is used to build an _HTTP Server_ instance, and then binds the server process
35-
to the specified socket address.
43+
```
44+
http-server [FLAGS] [OPTIONS] [root_dir]
45+
```
46+
47+
### Flags
48+
49+
Flags are provided without any values. For example:
50+
51+
```
52+
http-server --help
53+
```
54+
55+
Name | Short | Long | Description
56+
--- | --- | --- | ---
57+
Help | `h` | `help` | Prints help information
58+
Version | `V` | `version` | Prints version information
59+
60+
### Options
61+
62+
Options are provided with a value and also have default values. For example:
63+
64+
```
65+
http-server --address 127.0.0.1
66+
```
67+
68+
Name | Short | Long | Description | Default Value
69+
--- | --- | --- | --- | ---
70+
Address | `a` | `address` | Address to bind the server | `0.0.0.0`
71+
Port | `p` | `port` | Port to bind the server | `7878`
3672

3773
## Contributing
3874

src/cli/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clap::{crate_version, App};
44
/// Creates a CLAP `App` instance
55
pub fn make_app() -> App<'static, 'static> {
66
App::new("http-server")
7-
.about("A simple, zero-configuration command-line HTTP server")
7+
.about("Command-line HTTP Server")
88
.author("Authors: https://github.com/EstebanBorai/http-server/blob/main/AUTHORS")
99
.version(crate_version!())
1010
.setting(clap::AppSettings::ColoredHelp)

src/cli/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ pub fn make_args() -> Vec<Arg<'static, 'static>> {
5454
ROOT_DIR
5555
.into_positional(1)
5656
.help("Directory to serve files from"),
57-
SILENT.into_arg().help("Disable outputs").takes_value(false),
57+
// SILENT.into_arg().help("Disable outputs").takes_value(false),
5858
]
5959
}

src/file_explorer/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use std::cmp::Ordering;
12
use std::fs::{DirEntry, Metadata};
23
use std::path::PathBuf;
34
use std::time::SystemTime;
4-
use std::cmp::Ordering;
55

66
/// `FileExplorer` entry containing it's path (`PathBuf`) and
77
/// it's `Metadata`

src/file_explorer/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::io::Error as IOError;
44
/// errors
55
#[derive(Debug)]
66
pub enum Error {
7-
/// The path provided doesn't exists
8-
NoExists(String),
9-
/// A generic IO Error
10-
IOError(IOError)
7+
/// The path provided doesn't exists
8+
NoExists(String),
9+
/// A generic IO Error
10+
IOError(IOError),
1111
}

src/file_explorer/explorer.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::file_explorer::{Entry, Error};
2-
use std::io::ErrorKind;
32
use std::env::current_dir;
43
use std::fs::metadata;
4+
use std::io::ErrorKind;
55
use std::path::PathBuf;
66
use std::str::FromStr;
77

@@ -36,7 +36,7 @@ impl FileExplorer {
3636

3737
Self {
3838
root_dir: final_path.clone(),
39-
root_dir_string: final_path.to_str().unwrap().to_string()
39+
root_dir_string: final_path.to_str().unwrap().to_string(),
4040
}
4141
}
4242

@@ -46,12 +46,10 @@ impl FileExplorer {
4646

4747
match metadata(path.clone()) {
4848
Ok(meta) => Ok(Entry::new(path, meta)),
49-
Err(err) => {
50-
match err.kind() {
51-
ErrorKind::NotFound => Err(Error::NoExists(path.to_str().unwrap().to_string())),
52-
_ => Err(Error::IOError(err)),
53-
}
54-
}
49+
Err(err) => match err.kind() {
50+
ErrorKind::NotFound => Err(Error::NoExists(path.to_str().unwrap().to_string())),
51+
_ => Err(Error::IOError(err)),
52+
},
5553
}
5654
}
5755

@@ -76,15 +74,16 @@ impl FileExplorer {
7674
}
7775

7876
// retrieve every fragment of the path (directory).
79-
let sanitized_path = path.split('/')
77+
let sanitized_path = path
78+
.split('/')
8079
.collect::<Vec<&str>>()
8180
.into_iter()
8281
.filter(|&e| e != "")
8382
.collect::<Vec<&str>>()
8483
.join("/");
8584

8685
let mut next_path = self.root_dir.clone();
87-
86+
8887
next_path.push(PathBuf::from_str(&sanitized_path).unwrap());
8988

9089
next_path
@@ -100,7 +99,10 @@ mod tests {
10099
let file_explorer = FileExplorer::new(PathBuf::from_str("").unwrap());
101100
let relative = file_explorer.build_relative_path("/");
102101

103-
assert_eq!(file_explorer.root_dir.to_str().unwrap(), relative.to_str().unwrap());
102+
assert_eq!(
103+
file_explorer.root_dir.to_str().unwrap(),
104+
relative.to_str().unwrap()
105+
);
104106
}
105107

106108
#[test]
@@ -112,7 +114,10 @@ mod tests {
112114

113115
let relative = file_explorer.build_relative_path("/music/favorites");
114116

115-
assert_eq!(root_dir_on_music.to_str().unwrap(), relative.to_str().unwrap());
117+
assert_eq!(
118+
root_dir_on_music.to_str().unwrap(),
119+
relative.to_str().unwrap()
120+
);
116121
}
117122

118123
#[test]
@@ -124,6 +129,9 @@ mod tests {
124129

125130
let relative = file_explorer.build_relative_path("//music/favorites");
126131

127-
assert_eq!(root_dir_on_music.to_str().unwrap(), relative.to_str().unwrap());
132+
assert_eq!(
133+
root_dir_on_music.to_str().unwrap(),
134+
relative.to_str().unwrap()
135+
);
128136
}
129137
}

src/handler/static_fs/handler.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ pub fn static_fs(request: Request, file_explorer: &FileExplorer) {
3333

3434
let entries = read_dir(entry.path).unwrap();
3535

36-
let html = build_html(dirname, &file_explorer.root_dir_string, &file_explorer, entries);
36+
let html = build_html(
37+
dirname,
38+
&file_explorer.root_dir_string,
39+
&file_explorer,
40+
entries,
41+
);
3742
let mime_type_value: AsciiString = AsciiString::from_ascii("text/html").unwrap();
3843

3944
request

src/handler/static_fs/html/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::file_explorer::{FileExplorer, Entry};
1+
use crate::file_explorer::{Entry, FileExplorer};
22
use std::fs::ReadDir;
33

44
pub const HTML_DOCUMENT_START: &str = r#"
@@ -146,11 +146,7 @@ pub const FOLDER_ICON: &str = r##"<svg height='20px' width='30px' fill="#437CB0
146146
pub const FILE_ICON: &str = r##"<svg height='20px' width='30px' fill="#437CB0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.2" baseProfile="tiny" x="0px" y="0px" viewBox="0 0 80 80" xml:space="preserve"><polygon points="65,7.5 65,25 82.5,25"></polygon><polygon points="17.5,7.5 17.5,92.5 82.5,92.5 82.5,30 60,30 60,7.5"></polygon></svg>"##;
147147

148148
fn make_header(dirname: &str, root_dir: &str) -> String {
149-
let dirname = if dirname == "" {
150-
"/"
151-
} else {
152-
dirname
153-
};
149+
let dirname = if dirname == "" { "/" } else { dirname };
154150

155151
format!(
156152
r##"<header id="current-directory">
@@ -189,22 +185,26 @@ fn make_html_table_row(fsystem: &FileExplorer, fs_entry: &Entry) -> String {
189185
)
190186
}
191187

192-
pub fn build_html(dirname: &str, root_dir: &str, fsystem: &FileExplorer, entries: ReadDir) -> String {
188+
pub fn build_html(
189+
dirname: &str,
190+
root_dir: &str,
191+
fsystem: &FileExplorer,
192+
entries: ReadDir,
193+
) -> String {
193194
let mut html = String::from(HTML_DOCUMENT_START);
194195

195196
html.push_str(make_header(dirname, root_dir).as_str());
196197
html.push_str(MAIN_INIT);
197198

198-
let mut entries = entries.into_iter()
199-
.map(|dir_entry| Entry::from(dir_entry.unwrap()))
200-
.collect::<Vec<Entry>>();
199+
let mut entries = entries
200+
.map(|dir_entry| Entry::from(dir_entry.unwrap()))
201+
.collect::<Vec<Entry>>();
201202

202-
entries.sort();
203+
entries.sort();
203204

204-
entries.into_iter()
205-
.for_each(|entry| {
206-
html.push_str(make_html_table_row(fsystem, &entry).as_str());
207-
});
205+
entries.into_iter().for_each(|entry| {
206+
html.push_str(make_html_table_row(fsystem, &entry).as_str());
207+
});
208208

209209
html.push_str(TABLE_END);
210210
html.push_str(HTML_FOOTER);

src/main.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,90 @@
1+
//! <div>
2+
//! <div align="center" style="display: block; text-align: center;">
3+
//! <img src="https://raw.githubusercontent.com/EstebanBorai/http-server/main/docs/http-logo.png" height="120" width="200" />
4+
//! </div>
5+
//! <h1 align="center">http-server</h1>
6+
//! <h4 align="center">Command-line HTTP Server</h4>
7+
//! </div>
8+
//!
9+
//! <div align="center">
10+
//!
11+
//! [![Crates.io](https://img.shields.io/crates/v/http-server.svg)](https://crates.io/crates/http-server)
12+
//! [![Documentation](https://docs.rs/http-server/badge.svg)](https://docs.rs/http-server)
13+
//! ![Build](https://github.com/EstebanBorai/http-server/workflows/build/badge.svg)
14+
//! ![Lint](https://github.com/EstebanBorai/http-server/workflows/clippy/fmt/badge.svg)
15+
//! ![Tests](https://github.com/EstebanBorai/http-server/workflows/tests/badge.svg)
16+
//!
17+
//! </div>
18+
//!
19+
//! ## Index
20+
//!
21+
//! - [Installation](#installation)
22+
//! - [Usage](#usage)
23+
//! - [Flags](#flags)
24+
//! - [Options](#options)
25+
//! - [Contributing](#contributing)
26+
//! - [License](#license)
27+
//! - [Contribution](#contribution)
28+
//!
29+
//! ## Installation
30+
//!
31+
//! ```bash
32+
//! cargo install http-server
33+
//! ```
34+
//!
35+
//! Check for the installation to be successful.
36+
//!
37+
//! ```bash
38+
//! http-server --help
39+
//! ```
40+
//!
41+
//! ## Usage
42+
//!
43+
//! ```
44+
//! http-server [FLAGS] [OPTIONS] [root_dir]
45+
//! ```
46+
//!
47+
//! ### Flags
48+
//!
49+
//! Flags are provided without any values. For example:
50+
//!
51+
//! ```
52+
//! http-server --help
53+
//! ```
54+
//!
55+
//! Name | Short | Long | Description
56+
//! --- | --- | --- | ---
57+
//! Help | `h` | `help` | Prints help information
58+
//! Version | `V` | `version` | Prints version information
59+
//!
60+
//! ### Options
61+
//!
62+
//! Options are provided with a value and also have default values. For example:
63+
//!
64+
//! ```
65+
//! http-server --address 127.0.0.1
66+
//! ```
67+
//!
68+
//! Name | Short | Long | Description | Default Value
69+
//! --- | --- | --- | --- | ---
70+
//! Address | `a` | `address` | Address to bind the server | `0.0.0.0`
71+
//! Port | `p` | `port` | Port to bind the server | `7878`
72+
//!
73+
//! ## Contributing
74+
//!
75+
//! Every contribution to this project is welcome. Feel free to open a pull request,
76+
//! an issue or just by starting this project.
77+
//!
78+
//! ## License
79+
//!
80+
//! Licensed under the MIT License
81+
//!
82+
//! ### Contribution
83+
//!
84+
//! Unless you explicitly state otherwise, any contribution intentionally submitted for
85+
//! inclusion in http-server by you, shall be dual licensed as above, without any additional
86+
//! terms or conditions.
87+
//!
188
mod cli;
289
mod config;
390
mod file_explorer;

0 commit comments

Comments
 (0)