178 lines
8.2 KiB
Markdown
178 lines
8.2 KiB
Markdown
# 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
|
|
- Java 17+ (for Gradle)
|
|
|
|
## Build
|
|
|
|
```bash
|
|
./gradlew build
|
|
```
|
|
|
|
This builds the service Docker image, the Angular webapp, and the combined app image.
|
|
|
|
## Development
|
|
|
|
Start the service in a Docker container with hot-reload:
|
|
|
|
```bash
|
|
./gradlew :service:serve
|
|
```
|
|
|
|
In a separate terminal, start the Angular dev server:
|
|
|
|
```bash
|
|
cd webapp && npm start
|
|
```
|
|
|
|
The webapp will be available at `http://localhost:4200` and proxies API requests to the service at `localhost:8000`.
|
|
|
|
## Run
|
|
|
|
Run the combined app image:
|
|
|
|
```bash
|
|
docker run --rm -p 8080:8000 inse-app:1.0.0 run
|
|
```
|
|
|
|
The app will be available at `http://localhost:8080/inse-webapp/`.
|
|
|
|
## Default Users
|
|
|
|
Defined in `service/src/data/users.json`:
|
|
|
|
| Username | Password | Role |
|
|
| ---------- | ---------- | ----------- |
|
|
| superadmin | superadmin | SUPER_ADMIN |
|
|
| user | user | UNI_ADMIN |
|
|
|
|
## Prettier pre-commit hook
|
|
|
|
- Install Prettier
|
|
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
- To enable automatic code formatting with Prettier on commit, copy the pre-commit hook script to the Git hooks directory and make it executable:
|
|
|
|
```shell
|
|
cp .githooks/pre-commit .git/hooks/pre-commit
|
|
chmod +x .git/hooks/pre-commit
|
|
```
|