json/tools/serve_header
dependabot[bot] 9e23b5d61e
Some checks are pending
Code scanning - action / CodeQL-Build (push) Waiting to run
macOS / macos-13 (14.1) (push) Waiting to run
macOS / macos-13 (14.2) (push) Waiting to run
macOS / macos-13 (14.3) (push) Waiting to run
macOS / macos-13 (14.3.1) (push) Waiting to run
macOS / macos-13 (15.0.1) (push) Waiting to run
macOS / macos-13 (15.1) (push) Waiting to run
macOS / macos-13 (15.2) (push) Waiting to run
macOS / macos-14 (15.3) (push) Waiting to run
macOS / macos-14 (15.4) (push) Waiting to run
macOS / macos-15 (16.0) (push) Waiting to run
macOS / macos-15 (16.1) (push) Waiting to run
macOS / xcode_standards (11) (push) Waiting to run
macOS / xcode_standards (14) (push) Waiting to run
macOS / xcode_standards (17) (push) Waiting to run
macOS / xcode_standards (20) (push) Waiting to run
macOS / xcode_standards (23) (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Ubuntu / ci_test_clang (push) Waiting to run
Ubuntu / ci_test_gcc (push) Waiting to run
Ubuntu / ci_static_analysis (ci_cppcheck) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_infer) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_single_binaries) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_test_amalgamation) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_test_valgrind) (push) Waiting to run
Ubuntu / ci_test_single_header (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_cpplint) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_non_git_tests) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_offline_testdata) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_reproducible_tests) (push) Waiting to run
Ubuntu / ci_static_analysis_clang (ci_clang_analyze) (push) Waiting to run
Ubuntu / ci_static_analysis_clang (ci_clang_tidy) (push) Waiting to run
Ubuntu / ci_static_analysis_clang (ci_test_clang_sanitizer) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_cmake_flags) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_diagnostics) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_legacycomparison) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_noexceptions) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_noglobaludls) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_noimplicitconversions) (push) Waiting to run
Ubuntu / ci_test_coverage (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (4.8) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (4.9) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (5) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (6) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (10) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (11) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (12) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (13) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (14) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (7) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (8) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (9) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (latest) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (10) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (11) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (12) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (13) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (14) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (15-bullseye) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (16) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (17) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (18) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (19) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.5) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.6) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.7) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.8) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.9) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (4) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (5) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (6) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (7) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (8) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (9) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (latest) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (11) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (14) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (17) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (20) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (23) (push) Waiting to run
Ubuntu / ci_test_standards_clang (11, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (11, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (14, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (14, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (17, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (17, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (20, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (20, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (23, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (23, libstdcxx) (push) Waiting to run
Ubuntu / ci_cuda_example (push) Waiting to run
Ubuntu / ci_icpc (push) Waiting to run
Ubuntu / ci_reuse_compliance (push) Waiting to run
Ubuntu / ci_test_documentation (ci_test_api_documentation) (push) Waiting to run
Ubuntu / ci_test_documentation (ci_test_examples) (push) Waiting to run
Windows / mingw (x64) (push) Waiting to run
Windows / mingw (x86) (push) Waiting to run
Windows / msvc2019 (Win32, Debug) (push) Waiting to run
Windows / msvc2019 (Win32, Release) (push) Waiting to run
Windows / msvc2019 (x64, Debug) (push) Waiting to run
Windows / msvc2019 (x64, Release) (push) Waiting to run
Windows / msvc2019_latest (push) Waiting to run
Windows / msvc2022 (Win32, Debug) (push) Waiting to run
Windows / msvc2022 (Win32, Release) (push) Waiting to run
Windows / msvc2022 (x64, Debug) (push) Waiting to run
Windows / msvc2022 (x64, Release) (push) Waiting to run
Windows / msvc2022_latest (push) Waiting to run
Windows / clang (11) (push) Waiting to run
Windows / clang (12) (push) Waiting to run
Windows / clang (13) (push) Waiting to run
Windows / clang (14) (push) Waiting to run
Windows / clang (15) (push) Waiting to run
Windows / clang-cl-11 (Win32) (push) Waiting to run
Windows / clang-cl-11 (x64) (push) Waiting to run
⬆️ Bump watchdog from 2.1.7 to 6.0.0 in /tools/serve_header (#4542)
2024-12-15 22:39:16 +00:00
..
demo.png Add serve_header.py for rapid testing on Compiler Explorer (#3456) 2022-05-01 13:52:52 +02:00
README.md Correct a typo in serve_header/README.md (#4143) 2023-09-10 21:49:38 +02:00
requirements.txt ⬆️ Bump watchdog from 2.1.7 to 6.0.0 in /tools/serve_header (#4542) 2024-12-15 22:39:16 +00:00
serve_header.py Miscellaneous small fixes (#3643) 2022-08-07 13:52:43 +02:00
serve_header.yml.example Add serve_header.py for rapid testing on Compiler Explorer (#3456) 2022-05-01 13:52:52 +02:00

serve_header.py

Serves the single_include/nlohmann/json.hpp header file over HTTP(S).

The header file is automatically amalgamated on demand.

serve_header.py demo

Prerequisites

  1. Make sure these Python packages are installed.

    PyYAML
    watchdog
    

    (see tools/serve_header/requirements.txt)

  2. To serve the header over HTTPS (which is required by Compiler Explorer at this time), a certificate is needed. The recommended method for creating a locally-trusted certificate is to use mkcert.

    • Install the mkcert certificate authority into your trust store(s):
      $ mkcert -install
      
    • Create a certificate for localhost:
      $ mkcert localhost
      
      The command will create two files, localhost.pem and localhost-key.pem, in the current working directory. It is recommended to create them in the top level or project root directory.

Usage

serve_header.py has a built-in default configuration that will serve the single_include/nlohmann/json.hpp header file relative to the top level or project root directory it is homed in. The built-in configuration expects the certificate localhost.pem and the private key localhost-key.pemto be located in the top level or project root directory.

To start serving the json.hpp header file at https://localhost:8443/json.hpp, run this command from the top level or project root directory:

$ make serve_header

Open Compiler Explorer and try it out:

#include <https://localhost:8443/json.hpp>
using namespace nlohmann;

#include <iostream>

int main() {
    // these macros are dynamically injected into the header file
    std::cout << JSON_BUILD_TIME << " (" << JSON_BUILD_COUNT << ")\n";

    return 0;
}

serve_header.py dynamically injects the macros JSON_BUILD_COUNT and JSON_BUILD_TIME into the served header file. By comparing build count or time output from the compiled program with the output from serve_header.py, one can be reasonably sure the compiled code uses the expected revision of the header file.

Configuration

serve_header.py will try to read a configuration file serve_header.yml in the top level or project root directory, and will fall back on built-in defaults if the file cannot be read. An annotated example configuration can be found in tools/serve_header/serve_header.yml.example.

Serving json.hpp from multiple project directory instances or working trees

serve_header.py was designed with the goal of supporting multiple project roots or working trees at the same time. The recommended directory structure is shown below but serve_header.py can work with other structures as well, including a nested hierarchy.

json/          ⮜ the parent or web server root directory
├── develop/   ⮜ the main git checkout
│   └── ...
├── feature1/
│   └── ...      any number of additional
├── feature2/  ⮜ working trees (e.g., created
│   └── ...      with git worktree)
└── feature3/
    └── ...

To serve the header of each working tree at https://localhost:8443/<worktree>/json.hpp, a configuration file is needed.

  1. Create the file serve_header.yml in the top level or project root directory of any working tree:

    root: ..
    

    By shifting the web server root directory up one level, the single_include/nlohmann/json.hpp header files relative to each sibling directory or working tree will be served.

  2. Start serve_header.py by running this command from the same top level or project root directory the configuration file is located in:

    $ make serve_header
    

serve_header.py will automatically detect the addition or removal of working trees anywhere within the configured web server root directory.