Skip to content

Commit

Permalink
Preparations for version 0.2 (#10)
Browse files Browse the repository at this point in the history
Breaking changes:
* changes to project structure
* target directory can now be specified and accepts relative paths
* corrections to unit and integration tests
* the content returned is returned as read without trying to convert to utf8; this makes i.e. WebAssembly binaries and other types of files fully usable
  • Loading branch information
NicolaeIotu authored Dec 27, 2024
1 parent e43cc1e commit 4580d9d
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 211 deletions.
64 changes: 32 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "http-mini"
version = "0.1.10"
version = "0.2.0"
edition = "2021"
description = "Mini HTTP server"
license = "MIT"
Expand All @@ -14,7 +14,7 @@ keywords = [
authors = ["Iotu Nicolae <nicolae.g.iotu@gmail.com>"]
homepage = "https://github.com/NicolaeIotu/http-mini"
repository = "https://github.com/NicolaeIotu/http-mini"
documentation = "https://docs.rs/crate/http-mini/latest"
documentation = "https://docs.rs/http-mini"
categories = ["web-programming::http-server"]
include = [
"/Cargo.toml",
Expand All @@ -23,10 +23,6 @@ include = [
"/src/**",
"/tasks/**",
]
exclude = [
"/generated/**",
"/target/**",
]

[dependencies]

Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
# Mini HTTP server
# HTTP-Mini server

A simple single threaded server which can be used during development, or for light traffic applications.
A simple HTTP server which can be used during development, or for light traffic applications.

The application will only serve content which is located in the same directory as own executable.
This includes any content in subdirectories.
The application will only serve content in a target directory which must be specified at startup.

By default, the application starts listening on all available interfaces and associated addresses, on port 8080.

Procedure:
* Copy **mini-http** executable to target directory: `cp mini-http /path/to/target/directory/`
* Start **mini-http**: `/path/to/target/directory/mini-http`
* Open browser i.e. http://localhost:8080/index.html

The port and the address can be provided as command line arguments:
> mini-http 192.168.1.23 8090
>
> mini-http 8090 192.168.1.23
Arguments can be provided in any order:
> http-mini /path/to/target/directory 192.168.1.23 8090
Features:
* directory listing
Expand Down
23 changes: 23 additions & 0 deletions src/errors/missing_source_directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fmt;

const ERR_MS: &str = "Critical: missing absolute path to HTTP server source directory";

pub struct MissingSourceDirectoryError;

impl fmt::Display for MissingSourceDirectoryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ERR_MS)
}
}

impl fmt::Debug for MissingSourceDirectoryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{{ message: {}, file: {}, line: {} }}",
ERR_MS,
file!(),
line!()
)
}
}
1 change: 1 addition & 0 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod missing_source_directory;
63 changes: 35 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
mod errors;
mod traits;
mod utils;
pub mod utils;

use crate::utils::{app, fs};
use crate::utils::app;
use std::env;
use std::process::exit;
use utils::http_server;

/// # Start mini-http server
///
/// Example:
/// # Using http-mini library:
///
/// **File main.rs**
/// ```
/// use std::io::Error;
/// use std::net::TcpListener;
/// use std::process::exit;
/// use std::thread;
/// use std::thread::sleep;
/// use std::time::Duration;
///
/// extern crate http_mini_lib;
///
/// fn main() {
/// println!("Starting server...");
/// let t = thread::spawn(move || {
/// http_mini_lib::start();
/// });
///
/// sleep(Duration::new(1, 0));
/// drop(t);
/// http_mini_lib::start();
/// }
/// ```
///
/// **shell**:
/// > rustc --extern mini_http=src/external/libmini_http_lib.rlib ./src/main.rs
///
/// > main
// grcov-excl-start
pub fn start() {
let binding = fs::get_app_dir().unwrap();
let app_dir = binding.as_path();
let (address, port) = app::get_params();
let get_params_result = app::get_params();
if get_params_result.is_err() {
println!("{}", get_params_result.err().unwrap());
exit(1);
}
let (address, port, source_dir) = get_params_result.unwrap();

let executable_path = env::current_exe().unwrap();
let executable_name = executable_path.file_name().unwrap();
Expand All @@ -67,10 +51,33 @@ pub fn start() {
port
);
let link_addr = llv_link_addr.as_str();
let source_dir_path = source_dir.as_path();

for stream in listener.incoming() {
let stream = stream.unwrap();
http_server::handle_connection(stream, app_dir, executable_name, link_addr);
http_server::handle_connection(stream, source_dir_path, executable_name, link_addr);
}
}
// grcov-excl-stop

#[cfg(test)]
mod tests {
use crate::start;
use std::thread;
use std::thread::sleep;
use std::time::Duration;

#[test]
fn test_start() {
let no_panic = true;

let t = thread::spawn(move || {
start();
});

sleep(Duration::new(1, 0));
drop(t);

assert_eq!(no_panic, true);
}
}
Loading

0 comments on commit 4580d9d

Please sign in to comment.