README.md# GeoControl API
## Installation and Setup
### Prerequisites
Before starting, ensure you have the following installed on your system:
- **Node.js** (Recomended version: latest LTS)
- **npm** (Node Package Manager, included with Node.js)
### Installing Dependencies
Run the following command to install all required dependencies:
```sh
npm install
To start the server, run:
npm start
By default, the server runs on
For development with hot reloading, use:
npm run dev
This mode restarts the server automatically when code changes.
For debugging with hot reloading enabled:
On Windows:
npm run debug-win
On Unix/Linux:
npm run debug-unix
To create the SQLite database file and att to it an admin user with credentials root:rootpassword, execute:
npm run create-root
If you encounter an execution policy error like:
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
Run the following command before executing scripts:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
The API follows an OpenAPI specification, with the definition stored in:
doc/swagger_v1.yaml
Once the application is running, the Swagger UI is available at:
**http://localhost:5000/api/v1/doc**
The Swagger documentation provides a complete definition and description of the system, including object schemas, detailed endpoint specifications with input and output parameters, error definitions, and a brief functional overview of each API operation. It serves as the authorative reference for understanding how the system is designed to function.
The project follows a modular architecture, ensuring maintainability, separation of concerns, and scalability. Each module is structured to serve a specific purpose.
/coverage/data/doc/dockerContains everything needed to containerize the application and run it with Docker Compose.
To run the following commands, Docker Desktop is required (On Linux, you can install Docker Engine and CLI from your distribution's package manager too)
/backend
Dockerfile used to build the backend Node.js application.
When the container starts, it first runs the create-root script that waits for the database to become available and creates the default admin user if it does not already exist.
After that, it launches the actual backend server.Build the backend image manually (from the project root):
docker build -t geocontrol-backend -f docker/backend/Dockerfile .
Run the container after building the image:
docker run -d --name geocontrol-backend -p 5000:5000 geocontrol-backend
/db
init.sql script used to initialize the MySQL database.
The script creates the database, user, and password based on the environment variables defined in the docker-compose.yml file.
This initialization is executed only on the first startup, if the database volume is empty.docker-compose.yml
** Run the Docker Compose (from /docker folder):**
docker-compose up
Run the Docker Compose with automatic rebuild of the backend image:
docker-compose up --build
Stop and remove containers:
docker-compose down
Stop, remove containers and reset volumes (reset the DB):
docker-compose down -v
/logs/scripts/src - Main source folderContains all the following subfolders with the source code.
/config
/controllers
/database
/middlewares
/models
/dao: Respresents database entities using TypeORM./dto: Contains Data Transfer Objects (DTOs) generated automatically from OpenAPI./errors: Contains all the custom error classes./Repositories
/routes
/services
/utils.ts
/test - Test folderContains all unit tests and integration tests for the project. The tests are written using Jest, a popular JavaScript testing framework.
/e2e
/integration
userControlled integration with mapperService and the second one for the integration of userRoutes with its middleware layer./postman_collection
GeoControl API Full Test Suite.postman_collection.json) and run the requests against the API./setup
/unit
UserRepository class:
one using full mocking, and one using the actual in-memory datasource which will be used by the final end-to-end tests.To avoid relative imports, TypeScript path aliases are defined in tsconfig.json>
"paths": {
"@models/*": ["models/*"],
"@errors/*": ["models/errors/*"],
"@dao/*": ["models/dao/*"],
"@dto/*": ["models/dto/*"],
"@repositories/*": ["repositories/*"],
"@services/*": ["services/*"],
"@routes/*": ["routes/*"],
"@controllers/*": ["controllers/*"],
"@middlewares/*": ["middlewares/*"],
"@database": ["database/connection.ts"],
"@config": ["config/config.ts"],
"@utils": ["utils.ts"],
"@app": ["app.ts"],
"@test/*": ["../test/*"]
}
This allows importing modules like:
import { UserRepository } from "@repositories/UserRepository";
instead of using relative paths like:
import { UserRepository } from "../repositories/UserRepository";
All API endpoints include /v1/ in their URL paths (e.g., /api/v1/users).
This approact allows for backward compatibility when introducing breaking changes in the future. If a newer version of an endpoint requires different input parameters or returns a different response structure, a new version (e.g., /api/v2/users) can be created while keeping the old version operational. This prevents service disruptions for existing clients that depend on previous API versions.
## Punto di ingresso
Il **README.md** è il riferimento da consultare per primo: descrive prerequisiti, comandi e struttura del progetto. È quindi il “manuale d’uso” che sostituisce le spiegazioni frammentarie del prof.
## Installazione delle dipendenze
1. Verificare di avere **Node ≥ LTS** e **npm**.
2. Eseguire `npm install` per scaricare tutte le dipendenze definite nel progetto - operazione da ripetere anche se pensiamo di aver già installato qualcosa, perché il pacchetto fornito contiene tutte le librerie necessarie.
## Avvio del backend
- **Modalità standard**: `npm start` avvia il server su <http://localhost:5000>.
- **Modalità sviluppo con hot-reload**: `npm run dev` ricarica il codice a ogni salvataggio, evitando riavvii manuali.
- **Debug con hot-reload**:
- Windows → `npm run debug-win`
- Unix → `npm run debug-unix`
Il prof insiste che questi script sono già configurati: basta lanciarli, senza modifiche.
## Primo accesso: creazione utente amministratore
Per effettuare chiamate autenticate serve un utente. Il comando `npm run create-root` popola il database (SQLite di default) con l’utente `root : rootpassword` (**password lunga ≥ 5 caratteri**, per rispettare la validazione dell’entità `User`).
## Architettura del progetto
Il repository segue una **struttura modulare**:
- `/src` - codice applicativo suddiviso in **config**, **controllers**, **models (DAO/DTO/errors)**, **repositories**, **services**, **routes**, **middlewares** e **utils**.
- `/test` - suite Jest con unit, integration, e2e e una collezione Postman completa.
- Cartelle di servizio: `/coverage` (report test), `/data` (SQLite), `/logs` (file di log), `/docker` (containerizzazione), `/doc` (OpenAPI).
- **Path aliases** (definiti in `tsconfig.json`) eliminano i percorsi relativi, migliorando leggibilità e refactoring.
## Comandi utili (recap)