static_web_server/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// This file is part of Static Web Server.
3// See https://static-web-server.net/ for more information
4// Copyright (C) 2019-present Jose Quintana <joseluisq.net>
5
6//! # Static Web Server (SWS)
7//!
8//! SWS is a cross-platform, high-performance and asynchronous web server for static files-serving.
9//!
10//! **Considerations:**
11//!
12//! This crate was published to make it possible for users to embed SWS with ease instead of working around it via forks.
13//! For example, allowing users to turn off default features like `tls` and so on.
14//!
15//! **However**, because the library is highly coupled with the SWS binary project at this point,
16//! users might be limited by the exposed APIs, implementations, missing functionality, or dependencies.
17//!
18//! In the future, we will eventually plan to make SWS library independent from the binary project.
19//!
20//! That said, if fine-grained control and flexibility are needed then you could want to look at other alternatives like HTTP libraries or frameworks.
21//!
22//! **Pre-compile binaries:**
23//!
24//! This is the official SWS crate.
25//! If you are looking for platform pre-compile binaries then
26//! take a look at [static-web-server.net/download-and-install](https://static-web-server.net/download-and-install).
27//!
28//! ## Overview
29//!
30//! **Static Web Server** (or **`SWS`** abbreviated) is a very small and fast production-ready web server suitable to serve static web files or assets.
31//!
32//! It is focused on **lightness and easy-to-use** principles while keeping [high performance and safety](https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html) powered by [The Rust Programming Language](https://rust-lang.org).
33//!
34//! Written on top of [Hyper](https://github.com/hyperium/hyper) and [Tokio](https://github.com/tokio-rs/tokio) runtime. It provides [concurrent and asynchronous networking abilities](https://rust-lang.github.io/async-book/01_getting_started/02_why_async.html) as well as the latest HTTP/1 - HTTP/2 implementations.
35//!
36//! It's cross-platform and available for `Linux`, `macOS`, `Windows` and `FreeBSD` (`x86`/`x86_64`,  `ARM`/`ARM64`) as well as `Docker`.
37//!
38//! ![static-web-server](https://github.com/static-web-server/static-web-server/assets/1700322/102bef12-1f30-4054-a1bc-30c650d4ffa7)
39//!
40//! ## Features
41//!
42//! - Built with [Rust](https://rust-lang.org) which is focused on [safety, speed and concurrency](https://kornel.ski/rust-c-speed).
43//! - Memory safe and very reduced CPU and RAM overhead.
44//! - Blazing fast static files-serving and asynchronous powered by latest [Hyper](https://github.com/hyperium/hyper/), [Tokio](https://github.com/tokio-rs/tokio) and a set of [awesome crates](https://github.com/static-web-server/static-web-server/blob/master/Cargo.toml).
45//! - Single __4MB__ (uncompressed) and fully static binary with no dependencies ([Musl libc](https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html)). Suitable for running on [any Linux distro](https://en.wikipedia.org/wiki/Linux_distribution) or [Docker container](https://hub.docker.com/r/joseluisq/static-web-server/tags).
46//! - Optional GZip, Deflate or Brotli compression for text-based web files only.
47//! - Compression on-demand via [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header.
48//! - [Partial Content Delivery](https://en.wikipedia.org/wiki/Byte_serving) support for byte-serving of large files.
49//! - Optional [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) headers for assets.
50//! - [Termination signal](https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html) handling with [graceful shutdown](https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace) ability and grace period.
51//! - [HTTP/2](https://tools.ietf.org/html/rfc7540) and TLS support.
52//! - [Security headers](https://web.dev/security-headers/) for HTTP/2 by default.
53//! - [HEAD](https://tools.ietf.org/html/rfc7231#section-4.3.2) responses.
54//! - Lightweight and configurable logging via [tracing](https://github.com/tokio-rs/tracing) crate.
55//! - Customizable number of worker threads.
56//! - Optional directory listing.
57//! - [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) support.
58//! - Basic HTTP Authentication.
59//! - Customizable HTTP response headers for specific file requests via glob patterns.
60//! - Fallback pages for 404 errors, useful for Single-page applications.
61//! - Run the server as a [Windows Service](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc783643(v=ws.10)).
62//! - Configurable using CLI arguments, environment variables or a TOML file.
63//! - Default and custom error pages.
64//! - Custom URL rewrites and redirects via glob patterns.
65//! - Support for serving pre-compressed (Gzip/Brotli) files.
66//! - First-class [Docker](https://docs.docker.com/get-started/overview/) support. [Scratch](https://hub.docker.com/_/scratch) and latest [Alpine Linux](https://hub.docker.com/_/alpine) Docker images.
67//! - Ability to accept a socket listener as a file descriptor for use in sandboxing and on-demand applications (E.g [systemd](http://0pointer.de/blog/projects/socket-activation.html)).
68//! - Cross-platform. Pre-compiled binaries for Linux, macOS, Windows and FreeBSD (`x86`,`x86_64`,`ARM`,`ARM64`).
69//!
70//! ## Cargo features
71//!
72//! When building from the source, all features are enabled by default.
73//! However, you can disable just the ones you don't need from the lists below.
74//!
75//! Feature | Description
76//! ---------|------
77//! **Default** |
78//! `default` | Activates the default features.
79//! `all` | Activates all features including the default and experimental ones. E.g. this feature is used when building the SWS binaries.
80//! `experimental` | Activates all unstable features.
81//! [**HTTP2/TLS**](https://static-web-server.net/features/http2-tls/) |
82//! `http2` | Activates the HTTP2 and TLS feature.
83//! [**Compression**](https://static-web-server.net/features/compression/) |
84//! `compression` | Activates auto-compression and compression static with all supported algorithms.
85//! `compression-brotli` | Activates auto-compression/compression static with only the `brotli` algorithm.
86//! `compression-deflate` | Activates auto-compression/compression static with only the `deflate` algorithm.
87//! `compression-gzip` | Activates auto-compression/compression static with only the `gzip` algorithm.
88//! `compression-zstd` | Activates auto-compression/compression static with only the `zstd` algorithm.
89//! [**Directory Listing**](https://static-web-server.net/features/directory-listing/) |
90//! `directory-listing` | Activates the directory listing feature.
91//! [**Basic Authorization**](./features/basic-authentication.md) |
92//! `basic-auth` | Activates the Basic HTTP Authorization Schema feature.
93//! [**Fallback Page**](./features/error-pages.md#fallback-page-for-use-with-client-routers) |
94//! `fallback-page` | Activates the Fallback Page feature.
95//!
96
97#![deny(missing_docs)]
98#![forbid(unsafe_code)]
99#![deny(warnings)]
100#![deny(rust_2018_idioms)]
101#![deny(dead_code)]
102#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
103
104// Extern crates
105#[macro_use]
106extern crate anyhow;
107#[macro_use]
108extern crate serde;
109
110// Public modules
111#[macro_use]
112pub mod logger;
113#[cfg(feature = "basic-auth")]
114#[cfg_attr(docsrs, doc(cfg(feature = "basic-auth")))]
115pub mod basic_auth;
116#[cfg(any(
117    feature = "compression",
118    feature = "compression-gzip",
119    feature = "compression-brotli",
120    feature = "compression-zstd",
121    feature = "compression-deflate"
122))]
123#[cfg_attr(
124    docsrs,
125    doc(cfg(any(
126        feature = "compression",
127        feature = "compression-gzip",
128        feature = "compression-brotli",
129        feature = "compression-zstd",
130        feature = "compression-deflate"
131    )))
132)]
133pub mod compression;
134#[cfg(any(
135    feature = "compression",
136    feature = "compression-gzip",
137    feature = "compression-brotli",
138    feature = "compression-zstd",
139    feature = "compression-deflate"
140))]
141#[cfg_attr(
142    docsrs,
143    doc(cfg(any(
144        feature = "compression",
145        feature = "compression-gzip",
146        feature = "compression-brotli",
147        feature = "compression-zstd",
148        feature = "compression-deflate"
149    )))
150)]
151pub mod compression_static;
152pub(crate) mod conditional_headers;
153pub mod control_headers;
154pub mod cors;
155pub mod custom_headers;
156#[cfg(feature = "directory-listing")]
157#[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
158pub mod directory_listing;
159#[cfg(feature = "directory-listing-download")]
160#[cfg_attr(docsrs, doc(cfg(feature = "directory-listing-download")))]
161pub mod directory_listing_download;
162pub mod error_page;
163#[cfg(feature = "fallback-page")]
164#[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))]
165pub mod fallback_page;
166pub(crate) mod fs;
167pub mod handler;
168pub(crate) mod headers_ext;
169pub(crate) mod health;
170#[cfg(feature = "http2")]
171#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
172pub mod https_redirect;
173pub(crate) mod log_addr;
174pub mod maintenance_mode;
175#[cfg(feature = "experimental")]
176pub(crate) mod mem_cache;
177#[cfg(all(unix, feature = "experimental"))]
178pub(crate) mod metrics;
179pub mod redirects;
180pub(crate) mod response;
181pub mod rewrites;
182pub mod security_headers;
183pub mod server;
184pub mod service;
185pub mod settings;
186#[cfg(any(unix, windows))]
187#[cfg_attr(docsrs, doc(cfg(any(unix, windows))))]
188pub mod signals;
189pub mod static_files;
190#[cfg(feature = "http2")]
191#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
192pub mod tls;
193pub mod transport;
194pub(crate) mod virtual_hosts;
195#[cfg(windows)]
196#[cfg_attr(docsrs, doc(cfg(windows)))]
197pub mod winservice;
198#[macro_use]
199pub mod error;
200
201// Private modules
202#[doc(hidden)]
203mod helpers;
204#[doc(hidden)]
205pub mod http_ext;
206#[doc(hidden)]
207pub mod testing;
208
209// Re-exports
210pub use error::*;
211pub use server::Server;
212pub use settings::Settings;