Skip to main content

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 (tokio runtime metrics).
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 with all supported algorithms.
85//! `compression-brotli` | Activates auto-compression with only the `brotli` algorithm.
86//! `compression-deflate` | Activates auto-compression with only the `deflate` algorithm.
87//! `compression-gzip` | Activates auto-compression with only the `gzip` algorithm.
88//! `compression-zstd` | Activates auto-compression 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//! **Metrics** |
96//! `metrics` | Activates the Prometheus metrics endpoint (`/metrics`). Enabled by default but requires the `--metrics` flag at runtime. Tokio runtime metrics are additionally available via the `experimental` feature.
97//! **In-Memory Cache** |
98//! `mem-cache` | Activates the in-memory file cache with LFU admission and LRU eviction policies. Enabled by default and configured via TOML `[advanced.memory-cache]`.
99//!
100
101#![deny(missing_docs)]
102#![forbid(unsafe_code)]
103#![deny(warnings)]
104#![deny(rust_2018_idioms)]
105#![deny(dead_code)]
106#![cfg_attr(docsrs, feature(doc_cfg))]
107
108// Extern crates
109#[macro_use]
110extern crate anyhow;
111#[macro_use]
112extern crate serde;
113
114// Public modules
115#[macro_use]
116pub mod logger;
117#[cfg(feature = "basic-auth")]
118#[cfg_attr(docsrs, doc(cfg(feature = "basic-auth")))]
119pub mod basic_auth;
120pub mod body;
121#[cfg(any(
122    feature = "compression",
123    feature = "compression-gzip",
124    feature = "compression-brotli",
125    feature = "compression-zstd",
126    feature = "compression-deflate"
127))]
128#[cfg_attr(
129    docsrs,
130    doc(cfg(any(
131        feature = "compression",
132        feature = "compression-gzip",
133        feature = "compression-brotli",
134        feature = "compression-zstd",
135        feature = "compression-deflate"
136    )))
137)]
138pub mod compression;
139pub mod compression_static;
140pub(crate) mod conditional_headers;
141pub mod control_headers;
142pub mod cors;
143pub mod custom_headers;
144#[cfg(feature = "directory-listing")]
145#[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
146pub mod directory_listing;
147pub mod error_page;
148pub(crate) mod etag;
149pub mod exts;
150#[cfg(feature = "fallback-page")]
151#[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))]
152pub mod fallback_page;
153pub(crate) mod fs;
154pub mod handler;
155pub(crate) mod health;
156#[cfg(feature = "tls")]
157#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
158pub mod https_redirect;
159pub(crate) mod log_addr;
160pub mod maintenance_mode;
161pub(crate) mod markdown;
162#[cfg(feature = "mem-cache")]
163#[cfg_attr(docsrs, doc(cfg(feature = "mem-cache")))]
164pub mod mem_cache;
165#[cfg(feature = "metrics")]
166pub(crate) mod metrics;
167pub mod redirects;
168pub(crate) mod response;
169pub mod rewrites;
170pub mod security_headers;
171pub mod server;
172pub mod service;
173pub mod settings;
174#[cfg(any(unix, windows))]
175#[cfg_attr(docsrs, doc(cfg(any(unix, windows))))]
176pub mod signals;
177pub mod static_files;
178pub(crate) mod text_charset;
179#[cfg(feature = "tls")]
180#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
181pub mod tls;
182pub(crate) mod virtual_hosts;
183#[cfg(windows)]
184#[cfg_attr(docsrs, doc(cfg(windows)))]
185pub mod winservice;
186#[macro_use]
187pub mod error;
188
189// Private modules
190#[doc(hidden)]
191mod helpers;
192#[doc(hidden)]
193pub mod testing;
194
195// Re-exports
196pub use error::*;
197pub use server::Server;
198pub use settings::Settings;