diff --git a/README.md b/README.md index a4af4a5c..6bf00309 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,117 @@ -# My App +# SmartGUIDE: INSE A minimal full-stack web application using Docker, Gradle, FastAPI and Angular. +--- + ## Modules - **service**: Python REST API using FastAPI, SQLAlchemy (SQLite) and JWT authentication. Runs in a Docker container. - **webapp**: Angular 18 single-page application with Angular Material. Built via Node.js and Gradle. - **app**: Combined Docker image that serves both the service and the webapp. +## Build System: Gradle (Root) + +| File | Purpose | +|------|---------| +| build.gradle | Root config defining Docker image name (`inse`), prefix, and version (`1.0.0`) | +| settings.gradle | Declares the three submodules: `:service`, `:webapp`, `:app` | +| libs.versions.toml | Centralized dependency version catalog | + +Gradle orchestrates the entire build, including Docker image creation and the Angular frontend. + +--- + +## service — Python Backend (FastAPI) + +A REST API running in Docker, exposing **three mounted FastAPI sub-applications**: + +| Mount path | Description | +|---|---| +| `/inse-service` | Main resource API (items CRUD) | +| `/inse-webapp` | Serves the static Angular SPA | +| `/inse-account` | Authentication service (login/tokens) | + +### Key source files in inse: + +| File | Role | +|---|---| +| main.py | FastAPI app bootstrap, mounts the three sub-apps | +| service.py | CRUD REST endpoints for `Item` resources (GET, POST, PUT, DELETE), protected by JWT roles | +| model.py | Pydantic response models: `Item`, `ItemCreate`, `ItemPage` (generic pagination) | +| schema.py | SQLAlchemy ORM table definitions — `Item` table in SQLite | +| logic.py | Business logic layer (called by the service) | +| database.py | SQLAlchemy engine/session setup, reads from `config` | +| security.py | JWT decoding, `Principal` / `Account` models, role-based access via `roles_allowed()` | +| config.py | App settings via env vars prefixed `inse_` (DB URL, auth secret, feature flags) | +| webapp.py | Mounts the static Angular build as a `StaticFiles` app | +| util.py | Utility helpers (e.g. `raise_not_found_if_none`) | + +### `account/` sub-package (account): + +Handles authentication independently: +- account/service.py — `POST /auth/id` (username+password → identity token) and `POST /auth/access` (ID token → access token) +- account/logic.py — validates users from users.json +- account/model.py — Pydantic models for `IdentityGrant`, `AccessGrant` +- account/security.py — JWT signing + +### Data & Config: +- users.json — Hardcoded user accounts (superadmin/user) +- log-config.yaml — Logging configuration +- entrypoint.sh — Docker entrypoint script +- Dockerfile — Python 3.12 slim image, installs FastAPI, SQLAlchemy, PyJWT, uvicorn, etc. + +--- + +## webapp — Angular Frontend + +An **Angular 18 SPA** using Angular Material, built with Node.js + Gradle. + +### App structure in app: + +| Directory/File | Role | +|---|---| +| app.routes.ts | Routes: `/login` → `AppLoginComponent`, `/items` → `ListItemComponent` (guarded), `**` → redirect to login | +| app.config.ts | Angular app bootstrap config (providers, interceptors) | +| `app-login/` | Login form component | +| `app-layout/` | Shell/layout wrapper component | +| `app-menu/` | Navigation menu component | +| `list-item/` | The main feature page — lists `Item` resources with pagination | +| `confirmation-dialog/` | Reusable dialog for destructive actions | +| `guards/` | Route guards: `user.guard.ts` (authenticated), `admin.guard.ts` (admin role) | +| `interceptors/` | authentication.interceptor.ts — attaches JWT Bearer token to all requests except auth endpoints | +| `services/` | api.service.ts (CRUD calls to `/inse-service`), `credential.service.ts` (token management), `pagination.service.ts` | +| `pipes/` | `truncate.pipe.ts` — truncates long strings in templates | +| `utils/` | Utility helpers | + +--- + +## app — Combined Docker Image + +Dockerfile extends the **service image** (`inse-service:latest`) and copies the compiled Angular build output (`build/webapp/`) into `/home/webapp`. The result is a single container serving both the API and the SPA at port `8000`. + +--- + +## How It All Fits Together + +``` +Browser + └──> Angular SPA (served from /inse-webapp/) + └──> API calls to /inse-service/items (JWT Bearer) + └──> Auth calls to /inse-account/auth/id & /auth/access + ┌────────────────────────────────┐ + │ FastAPI (uvicorn :8000) │ + │ /inse-service → service.py │ + │ /inse-webapp → static files │ + │ /inse-account → account/ │ + └─────────────┬──────────────────┘ + │ + SQLite DB (SQLAlchemy) +``` + +**Auth flow**: The Angular interceptor calls `/inse-account/auth/id` with credentials → receives an ID token → exchanges it for an access JWT via `/inse-account/auth/access` → attaches the access token as `Authorization: Bearer` on all subsequent requests. The backend's security.py decodes and validates this JWT to enforce role-based access control (`SUPER_ADMIN`, `UNI_ADMIN`). + + ## Prerequisites - Docker