| 1 | serve_header.py |
| 2 | =============== |
| 3 | |
| 4 | Serves the `single_include/nlohmann/json.hpp` header file over HTTP(S). |
| 5 | |
| 6 | The header file is automatically amalgamated on demand. |
| 7 | |
| 8 |  |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | 1. Make sure these Python packages are installed. |
| 13 | ``` |
| 14 | PyYAML |
| 15 | watchdog |
| 16 | ``` |
| 17 | (see `tools/serve_header/requirements.txt`) |
| 18 | |
| 19 | 2. To serve the header over HTTPS (which is required by Compiler Explorer at this time), a certificate is needed. |
| 20 | The recommended method for creating a locally-trusted certificate is to use [`mkcert`](https://github.com/FiloSottile/mkcert). |
| 21 | - Install the `mkcert` certificate authority into your trust store(s): |
| 22 | ``` |
| 23 | $ mkcert -install |
| 24 | ``` |
| 25 | - Create a certificate for `localhost`: |
| 26 | ``` |
| 27 | $ mkcert localhost |
| 28 | ``` |
| 29 | 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. |
| 30 | |
| 31 | ## Usage |
| 32 | |
| 33 | `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. |
| 34 | The built-in configuration expects the certificate `localhost.pem` and the private key `localhost-key.pem`to be located in the top level or project root directory. |
| 35 | |
| 36 | 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: |
| 37 | ``` |
| 38 | $ make serve_header |
| 39 | ``` |
| 40 | |
| 41 | Open [Compiler Explorer](https://godbolt.org/) and try it out: |
| 42 | ```cpp |
| 43 | #include <https://localhost:8443/json.hpp> |
| 44 | using namespace nlohmann; |
| 45 | |
| 46 | #include <iostream> |
| 47 | |
| 48 | int main() { |
| 49 | // these macros are dynamically injected into the header file |
| 50 | std::cout << JSON_BUILD_TIME << " (" << JSON_BUILD_COUNT << ")\n"; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | > `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. |
| 57 | |
| 58 | ## Configuration |
| 59 | |
| 60 | `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. |
| 61 | An annotated example configuration can be found in `tools/serve_header/serve_header.yml.example`. |
| 62 | |
| 63 | ## Serving `json.hpp` from multiple project directory instances or working trees |
| 64 | |
| 65 | `serve_header.py` was designed with the goal of supporting multiple project roots or working trees at the same time. |
| 66 | The recommended directory structure is shown below but `serve_header.py` can work with other structures as well, including a nested hierarchy. |
| 67 | ``` |
| 68 | json/ ⮜ the parent or web server root directory |
| 69 | ├── develop/ ⮜ the main git checkout |
| 70 | │ └── ... |
| 71 | ├── feature1/ |
| 72 | │ └── ... any number of additional |
| 73 | ├── feature2/ ⮜ working trees (e.g., created |
| 74 | │ └── ... with git worktree) |
| 75 | └── feature3/ |
| 76 | └── ... |
| 77 | ``` |
| 78 | |
| 79 | To serve the header of each working tree at `https://localhost:8443/<worktree>/json.hpp`, a configuration file is needed. |
| 80 | 1. Create the file `serve_header.yml` in the top level or project root directory of any working tree: |
| 81 | ```yaml |
| 82 | root: .. |
| 83 | ``` |
| 84 | 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. |
| 85 | |
| 86 | 2. Start `serve_header.py` by running this command from the same top level or project root directory the configuration file is located in: |
| 87 | ``` |
| 88 | $ make serve_header |
| 89 | ``` |
| 90 | |
| 91 | `serve_header.py` will automatically detect the addition or removal of working trees anywhere within the configured web server root directory. |