3.6 KiB
CurseTechnique Codebase Walkthrough
This document is a practical map of the app, aimed at learning-oriented Rust reading.
1. High-level architecture
The app uses a lightweight 4-layer layout:
src/main.rs- startup and route wiringsrc/handlers.rs- HTTP handlers and input validationsrc/db.rs- SQLite queries and data structssrc/views.rs- HTML rendering (server-side string rendering)
The goal is to keep each file focused and easy to read without adding extra frameworks.
2. Request flow (end-to-end)
Example: open GET /day/2026-02-07
- Route is defined in
src/main.rsand points tohandlers::show_day_entries. show_day_entriesinsrc/handlers.rsvalidates the date path param.- It calls
db::fetch_day_entriesinsrc/db.rsto load rows from SQLite. - It passes those rows to
views::render_day_pageinsrc/views.rs. - The rendered HTML is returned to the browser.
Example: submit add-entry form POST /day/{date}/add
- Route points to
handlers::create_entry. - Handler validates date and form fields (
name,calories). - Handler calls
db::insert_entry. - Handler redirects back to
/day/{date}.
3. File-by-file reading guide
src/main.rs
Read this file first. It is intentionally small.
- Creates data directory and opens SQLite DB.
- Calls DB init/seed helpers.
- Builds Axum router.
- Starts Tokio TCP listener.
If you ever wonder "where is this endpoint connected?", this file is the source of truth.
src/handlers.rs
This is the HTTP boundary.
Core concepts here:
AppStateholds shared DB connection behindArc<Mutex<Connection>>.- Each handler receives typed route/form data.
- Handlers do three steps:
- Validate input
- Call DB function
- Return HTML or redirect
Useful helper functions:
validate_dateensuresYYYY-MM-DDformat.parse_entry_form_fieldsvalidatesname+calories.month_boundscomputes first/next month boundaries.
src/db.rs
This file contains all SQL.
Data structs:
FoodEntryrepresents one row.DaySummaryrepresents aggregated values for calendar cards.
Functions are split by intent:
- Setup:
init_db,seed_db_if_empty - Reads:
fetch_month_summaries,fetch_day_entries - Writes:
insert_entry,update_entry,delete_entry
If behavior looks wrong (totals, filtering, ordering), start debugging here.
src/views.rs
This file builds HTML strings for SSR.
render_calendar_pagerenders/.render_day_pagerenders/day/{date}.- Small helpers reduce clutter:
render_day_cardrender_entry_rowsentry_count_labelescape_html
Important: escape_html is used when rendering user-provided food names.
4. Key Rust patterns used
Result<T, E>for explicit error handling.?operator to propagate errors.Arc<Mutex<_>>for shared mutable state across async handlers.- Simple structs over tuple-heavy values for readability (
DaySummary). - Module organization (
mod db; mod handlers; mod views;) to keep files focused.
5. How to add a feature safely
Use this checklist:
- Add/adjust SQL function in
db.rs. - Add or update handler logic in
handlers.rs. - Update route wiring in
main.rsif endpoint path changes. - Update HTML output/forms in
views.rs. - Run:
cargo fmtcargo check
6. Suggested beginner exercises
- Add a "notes" text column to food entries.
- Show count of entries in the day header.
- Add confirmation step on delete (simple query param or separate page).
- Add a helper in handlers to prevent editing future dates.
These exercises touch all 4 layers and are great practice for understanding the whole flow.