2.3 just serve and just stop
Build it yourself. Read the tutorial is the other way to read this part.
This walk goes through rahuldave/cheesecracker on GitHub. This part starts from the code at acee14f7dff4, partway through step-02. The code before this part
This part does not start where the part before it ends, because the walk skips some changes. This command puts the files that this part starts from in place of yours. Run it, and then go on:
git fetch --quiet --tags --force origin; git restore --source step-01 --staged -- :/ && git restore --source acee14f7dff4 --staged --worktree -- :/If you start at this part, make your clone as Get the project says, and then run this command. It puts in your clone the files that this part starts from. A file that the project deleted before this part stays, so delete it if git check lists it:
git rm -rq --cached --ignore-unmatch -- :/ && git restore --source acee14f7dff4 --staged --worktree -- :/just setupThe whole tutorial then adds FastAPI, a library for web APIs, and writes the app with it. It also adds uvicorn, the server that runs the app. Two recipes start the server and stop it.
2.3 just serve and just stop, the server on this machine
The app does nothing until a server runs it. The recipe serve starts uvicorn at port 9000, in the background, and
waits until the server answers. The recipe stop stops it. A server is a fixture of the commands that need it. We start
it before them, and stop it after them.
Apply the patch to justfile, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/commit/69c3bf65c78cf8e96bf8f9ea1dde29279db1351a.diff | git apply --include=justfileApply the patch to .gitignore, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/commit/69c3bf65c78cf8e96bf8f9ea1dde29279db1351a.diff | git apply --include=.gitignoreRead the recipes serve and stop at the end of the justfile. Then read the two new lines of .gitignore.
just serveThe recipe prints serve: http://localhost:9000, its log in .serve.log. Ask the server for its health.
curl -s localhost:9000/healthcurl sends an HTTP request from the terminal, and prints the answer. macOS and most Linux systems
have it. The server answers {"status":"healthy"}. Open http://localhost:9000/docs in a browser to see the pages that
FastAPI makes for each route.
just stopThe recipe prints stop: stopped. Ask for the health again.
curl -s localhost:9000/healthcurl prints nothing, and fails with the code 7, because no server answers.
Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:
git check 69c3bf65c78cIf you are stuck, this command puts the files of this move in place of yours. A file that you made yourself stays:
git fetch --quiet --tags --force origin; git restore --source acee14f7dff4 --staged -- :/ && git restore --source step-02 --staged --worktree -- :/What changed
.gitignore: the log of the server and the number of its process. 4 lines added. The file- look at
.serve.pid
- look at
justfile: the recipesserveandstop. 26 lines added. The file- look at
uv run uvicorn api.service:app --app-dir src/api-service --host 0.0.0.0 --port 9000 --reload > .serve.log 2>&1 &
- look at
The patch to .gitignore
@@ -9,6 +9,10 @@ __pycache__/
htmlcov/
.ruff_cache/
+# What just serve writes: the log of the server, and its process id
+.serve.log
+.serve.pid
+
# Secrets: never commit them
secrets/
.envThe patch to justfile
@@ -14,3 +14,29 @@ setup:
# Run the tests. Extra arguments go to pytest: just test tests/unit
test *args:
uv run pytest "$@"
+
+# Start the server on this machine, in the background, at http://localhost:9000. It restarts when a file of the code changes. just stop stops it
+serve:
+ #!/usr/bin/env sh
+ if curl -sf localhost:9000/health > /dev/null; then echo "serve: a server already answers at http://localhost:9000" >&2; exit 1; fi
+ uv run uvicorn api.service:app --app-dir src/api-service --host 0.0.0.0 --port 9000 --reload > .serve.log 2>&1 &
+ echo $! > .serve.pid
+ for i in $(seq 1 30); do
+ if curl -sf localhost:9000/health > /dev/null; then echo "serve: http://localhost:9000, its log in .serve.log"; exit 0; fi
+ sleep 1
+ done
+ echo "serve: no answer after 30 seconds; read .serve.log" >&2
+ exit 1
+
+# Stop the server of just serve
+stop:
+ #!/usr/bin/env sh
+ if [ ! -f .serve.pid ]; then echo "stop: no server of just serve runs here" >&2; exit 1; fi
+ kill "$(cat .serve.pid)" && rm .serve.pid
+ # Wait until the server no longer answers, so that the port is free for the next one
+ for i in $(seq 1 30); do
+ if ! curl -s localhost:9000/health > /dev/null; then echo "stop: stopped"; exit 0; fi
+ sleep 1
+ done
+ echo "stop: the server still answers after 30 seconds" >&2
+ exit 1This part ends at the code at step-02. The code after this part