|
| 1 | +[](https://www.rust-lang.org) |
| 2 | + |
| 3 | +[](https://github.com/noprd/example-rust/actions/workflows/manual.yaml) |
| 4 | +[](https://github.com/noprd/example-rust/actions/workflows/manual.yaml) |
| 5 | + |
| 6 | +[](https://github.com/noprd/example-rust/actions/workflows/auto.yaml) |
| 7 | +[](https://github.com/noprd/example-rust/actions/workflows/auto.yaml) |
| 8 | + |
1 | 9 | # Example Rust # |
| 10 | + |
| 11 | +This repository provides a simple Hello-World example of an http-server |
| 12 | +written in [rust](https://www.rust-lang.org), |
| 13 | +which in recent years has gained importance. |
| 14 | + |
| 15 | +## Requirements ## |
| 16 | + |
| 17 | +- A bash terminal. |
| 18 | + Windows users can e.g. install this via <https://gitforwindows.org>. |
| 19 | + |
| 20 | +- The [justfile](https://github.com/casey/just?tab=readme-ov-file#installation) tool. |
| 21 | + |
| 22 | +- [Rust](https://www.rust-lang.org/tools/install) `~0.18.0` incl. the cargo package manager. |
| 23 | + |
| 24 | +> [!IMPORTANT] |
| 25 | +> We also require an installation of [Zig](https://ziglang.org) for cross-compilation. |
| 26 | +> This avoids gcc-compiler issues on your local machine, |
| 27 | +> which the rust compiler requires to install some dependencies. |
| 28 | +
|
| 29 | +Ensure paths to the binaries have been set. |
| 30 | + |
| 31 | +> [!TIP] |
| 32 | +> To verify, open a bash terminal and call. |
| 33 | +> |
| 34 | +> ```bash |
| 35 | +> just --version |
| 36 | +> # rust |
| 37 | +> rustup --version |
| 38 | +> rustc --version |
| 39 | +> cargo --version |
| 40 | +> # zig |
| 41 | +> zig version |
| 42 | +> # docker |
| 43 | +> docker version |
| 44 | +> docker compose version |
| 45 | +> ``` |
| 46 | +
|
| 47 | +### Optional requirements ### |
| 48 | +
|
| 49 | +- [Postman](https://www.postman.com/downloads), |
| 50 | + also available as a [VSCode extension](https://marketplace.visualstudio.com/items?itemName=Postman.postman-for-vscode). |
| 51 | +- (necessary for [docker usage](#via-docker)) |
| 52 | + [docker](https://docs.docker.com/engine/install) incl. the docker CLI. |
| 53 | +
|
| 54 | +## Usage ## |
| 55 | +
|
| 56 | +Open a bash terminal and run: |
| 57 | +
|
| 58 | +```bash |
| 59 | +just setup |
| 60 | +``` |
| 61 | +
|
| 62 | +This is only needed once. |
| 63 | +After this, adjust the values in the newly created **.env** files |
| 64 | +based on the [template](templates/template.env). |
| 65 | +
|
| 66 | +### Direct usage ### |
| 67 | +
|
| 68 | +Open a bash terminal and run: |
| 69 | +
|
| 70 | +```bash |
| 71 | +# compiles code and creates binary artefact in ./dist folder |
| 72 | +just build |
| 73 | +# execution |
| 74 | +just run-binary # runs from binary |
| 75 | +just run # runs from source code (this may involve recompiling dependencies / code) |
| 76 | +# tests |
| 77 | +just tests-unit |
| 78 | +# linting |
| 79 | +just prettify |
| 80 | +``` |
| 81 | +
|
| 82 | +### Via docker ### |
| 83 | +
|
| 84 | +Start up the docker engine. |
| 85 | +
|
| 86 | +```bash |
| 87 | +# compiles code and creates binary artefact in ./dist folder |
| 88 | +just docker-build |
| 89 | +# execution |
| 90 | +just docker-run-binary # runs from binary created in container |
| 91 | +just docker-run # runs from source code (this may involve recompiling dependencies / code) |
| 92 | +# tests |
| 93 | +just docker-utests # runs unit-tests within container |
| 94 | +# linting |
| 95 | +just prettify |
| 96 | +# exploration |
| 97 | +just explore {service} # service = one of build, run, run-binary, utests |
| 98 | +``` |
| 99 | +
|
| 100 | +> [!TIP] |
| 101 | +> Due to the volume mounting (see [docker-compose.yaml](docker-compose.yaml)), |
| 102 | +> whilst "exploring" you can alter the source code |
| 103 | +> and run the tests using the commands exactly as stated in [Direct Usage](#direct-usage). |
| 104 | +
|
| 105 | +> [!NOTE] |
| 106 | +> Changes performed whilst exploring the container will mostly be reverted |
| 107 | +> (bar those performed on mounted volumes), |
| 108 | +> once the container is exited. |
| 109 | +
|
| 110 | +## Interaction with the Http-Server ## |
| 111 | +
|
| 112 | +Whilst "running" the compiled code |
| 113 | +(via either |
| 114 | +`just run-binary`, |
| 115 | +`just run`, |
| 116 | +`just docker-run-binary`, |
| 117 | +`just docker-run`; |
| 118 | +see above), |
| 119 | +the endpoints are exposed to the `HTTP_PORT` value set in your **.env**-file. |
| 120 | +
|
| 121 | +The exposed endpoints of the application can now be spoken to, e.g. |
| 122 | +
|
| 123 | +```bash |
| 124 | +curl --request GET 'http://{HTTP_IP}:{HTTP_PORT}/api/ping' |
| 125 | +curl --request POST 'http://{HTTP_IP}:{HTTP_PORT}/api/token' \ |
| 126 | + --data '{"key1": value1, "key2", value2, ...}' |
| 127 | +``` |
| 128 | +
|
| 129 | +where `{HTTP_IP}` and `{HTTP_PORT}` are to be replaced by the values set in your **.env**-file. |
| 130 | +Alternatively, we recommend using [Postman](https://www.postman.com/downloads). |
| 131 | +
|
| 132 | +### Clean up ### |
| 133 | +
|
| 134 | +To clean up build artefacts and dangling docker images, |
| 135 | +simply run |
| 136 | +
|
| 137 | +```bash |
| 138 | +just clean |
| 139 | +``` |
| 140 | +
|
| 141 | +To be more thorough, you may wish to run |
| 142 | +
|
| 143 | +```bash |
| 144 | +docker system prune |
| 145 | +``` |
| 146 | +
|
| 147 | +> [!WARNING] |
| 148 | +> The latter command clears _all_ docker images, containers, etc. |
| 149 | +> not just for this project. |
0 commit comments