-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/complete project improvement #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
14fefab
feat: implement authentication endpoint with validation for headers a…
devZenta 255e06f
feat: enable tests in Dockerfile and update entrypoint in compose.yam…
devZenta 2458df2
feat: enhance biome configuration with additional linter rules and fo…
devZenta 360fe25
feat: update dependencies and scripts in package.json and bun.lock fo…
devZenta 14f050f
feat: add authentication middleware for client validation and token d…
devZenta 3b9d3b6
feat: add EventType enum for event categorization
devZenta 4a6c300
feat: integrate CORS support and enhance server initialization with e…
devZenta dd09ae4
feat: update entrypoint in Docker Compose to include database seeding
devZenta 297706e
feat: update Prisma schema to include History model and refine EventT…
devZenta caf4f28
feat: add migration for History table and EventType enum
devZenta 320f63b
feat: add seed script for initial database setup and data seeding
devZenta 9faaaa5
feat: implement event bus with initial event definitions
devZenta cfdbf7f
feat: add encryption and decryption utility functions
devZenta 6b4120f
feat: add system health check route with database connection status
devZenta b206942
feat: implement client registration endpoint with database integration
devZenta 4bc5dc2
feat: implement home state management and rule engine with WebSocket …
devZenta 221a441
fix: handle token decryption errors and return appropriate response
devZenta 365378e
refactor: clean up route middleware usage for better readability
devZenta b9bb414
refactor: remove unnecessary comments and clean up WebSocket connecti…
devZenta 0ae64cb
feat: add Prisma generate command to Dockerfile for database setup
devZenta bd2f488
feat: add root client initialization to seed script
devZenta 36c4b12
refactor: remove previewFeatures from Prisma client generator
devZenta 770896f
feat: update server startup logs to include dynamic port configuration
devZenta bc2c0d2
refactor: remove unnecessary comments from homeState service
devZenta eca0a11
feat: enhance API responses with detailed error handling and add hist…
devZenta e54a7e5
fix: change noExplicitAny rule from warn to error in linter configura…
devZenta 362e630
feat: add comprehensive test cases for authentication, check, feature…
devZenta fcf019b
feat: configure CORS settings for enhanced security and flexibility
devZenta 5445546
refactor: streamline WebSocket connection handling and improve logging
devZenta ba5ef0c
feat: enhance rule descriptions and conditions for heating and lighti…
devZenta f1b6fa7
refactor: remove redundant comments in rule engine initialization
devZenta 6f9a920
feat: allow dynamic salt configuration for encryption
devZenta dd8f2e4
feat: add example environment configuration and documentation
devZenta ba0085b
docs: add comprehensive documentation for BunServer setup and API usage
devZenta 2aa54a9
refactor: restructure and enhance README documentation for better cla…
devZenta 2738784
feat: add end-to-end integration tests for authentication and state m…
devZenta 1d92f4f
feat: add unit tests for authentication middleware with various crede…
devZenta 71eece5
feat: add comprehensive unit tests for HomeState service methods and …
devZenta 471d849
test: add unit tests for eventBus functionality and event emission
devZenta 5110ebe
feat: enhance authentication tests and add history route tests with e…
devZenta ff80ca8
feat: add snapshot tests for history route with various data scenarios
devZenta 4a9c8ea
feat: add unit tests for rule definitions and enhance rule engine tests
devZenta c55ba07
feat: add CI and PR workflows for automated testing and linting
devZenta eab516e
feat: update biome check to enforce error on warnings for src and tests
devZenta 5a73957
feat: enhance test setup with beforeEach for consistent mock state in…
devZenta 1d34277
feat: add beforeEach hooks for consistent mock state initialization i…
devZenta 3c5d692
feat: enhance mockPrisma setup in tests for improved route handling
devZenta 974d724
feat: update mock state temperature and door status in tests for cons…
devZenta 5e78035
refactor: centralize Prisma mock for test isolation
devZenta 866aec1
chore: remove accidental error file
devZenta 6ad6f78
chore: clean up test files for improved readability and maintainability
devZenta fc26c9c
feat: update mock state handling to prevent mutation without valid data
devZenta 4bd34a9
feat: enhance CI workflow by adding Prisma Client generation step and…
devZenta 678c2cd
feat: add DATABASE_URL environment variable for Prisma Client generat…
devZenta 6c093c3
feat: enhance CI configuration with encryption keys and improve WebSo…
devZenta 5c3eb06
refactor: update prisma mock module paths for cross-platform compatib…
devZenta 37774dc
feat: enhance test environment setup by improving PrismaClient initia…
devZenta d53e8d7
feat: refactor Prisma initialization and enhance mock integration for…
devZenta 0cdcf50
chore: skip flaky tests that fail in CI Linux environment
devZenta 4bf09b0
fix: add missing token for Codecov action in CI workflow
devZenta 9ae98c1
fix: add missing slug parameter to Codecov action in CI workflow
devZenta 7dbfdd7
feat: add Docker workflow for building and pushing images
devZenta 91a521a
fix: add Prisma generate and env vars to PR workflow
devZenta 8095330
fix: add DATABASE_URL env var for Knip in PR workflow
devZenta 059f8a8
fix: exclude ~/.bun cache from Knip scan
devZenta b56da21
chore: remove unused dotenv dependency and fix knip config
devZenta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| DATABASE_URL="postgresql://root:root@localhost:5432/myhouse" | ||
| DATABASE_URL="postgresql://root:root@localhost:5432/myhouse" | ||
|
|
||
| # Security | ||
| ENCRYPTION_KEY="MySuperSecretPasswordFixed123!" | ||
| ENCRYPTION_SALT="MyFixedSalt" | ||
|
|
||
| # Ports | ||
| PORT_BUN_SERVER=3000 | ||
| PORT_WEB_SERVER=8080 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main, feature/*] | ||
| pull_request: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Test & Coverage | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
|
|
||
| - name: Install dependencies | ||
| run: bun install | ||
|
|
||
| - name: Generate Prisma Client | ||
| run: bunx --bun prisma generate | ||
| env: | ||
| DATABASE_URL: postgresql://user:password@localhost:5432/test | ||
|
|
||
| - name: Run tests with coverage | ||
| run: bun test --coverage --coverage-reporter=lcov --timeout 5000 | ||
| env: | ||
| ENCRYPTION_KEY: TestEncryptionKey12345678901234 | ||
| ENCRYPTION_SALT: TestSalt | ||
|
|
||
| - name: Upload coverage to Codecov | ||
| uses: codecov/codecov-action@v4 | ||
| if: always() | ||
| with: | ||
| token: ${{ secrets.CODECOV_TOKEN }} | ||
| slug: ${{ github.repository }} | ||
| files: ./coverage/lcov.info | ||
| fail_ci_if_error: false | ||
|
|
||
| lint: | ||
| name: Lint & Format Check | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
|
|
||
| - name: Install dependencies | ||
| run: bun install | ||
|
|
||
| - name: Run Biome check | ||
| run: bunx --bun biome check --error-on-warnings src tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| name: Docker | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| env: | ||
| REGISTRY: ghcr.io | ||
| IMAGE_NAME: ${{ github.repository }} | ||
|
|
||
| jobs: | ||
| build-and-push: | ||
| name: Build & Push Docker Image | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Log in to Container Registry | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ${{ env.REGISTRY }} | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Extract metadata (tags, labels) | ||
| id: meta | ||
| uses: docker/metadata-action@v5 | ||
| with: | ||
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | ||
| tags: | | ||
| type=raw,value=latest | ||
| type=sha,prefix= | ||
|
|
||
| - name: Build and push Docker image | ||
| uses: docker/build-push-action@v6 | ||
| with: | ||
| context: . | ||
| push: true | ||
| tags: ${{ steps.meta.outputs.tags }} | ||
| labels: ${{ steps.meta.outputs.labels }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| name: Pull Request Checks | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| jobs: | ||
| validate: | ||
| name: Validate PR | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
|
|
||
| - name: Install dependencies | ||
| run: bun install | ||
|
|
||
| - name: Generate Prisma Client | ||
| run: bunx --bun prisma generate | ||
| env: | ||
| DATABASE_URL: postgresql://user:password@localhost:5432/test | ||
|
|
||
| - name: Run all checks | ||
| run: | | ||
| echo "Running Biome checks..." | ||
| bunx --bun biome check --error-on-warnings src tests | ||
|
|
||
| echo "Running tests..." | ||
| bun test --timeout 5000 | ||
|
|
||
| echo "Running Knip (unused dependencies check)..." | ||
| bun run knip | ||
| env: | ||
| ENCRYPTION_KEY: TestEncryptionKey12345678901234 | ||
| ENCRYPTION_SALT: TestSalt | ||
| DATABASE_URL: postgresql://user:password@localhost:5432/test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,126 @@ | ||
| # BunServer | ||
| # 🏠 BunServer - Backend Domotique (MyHouse OS) | ||
|
|
||
| To install dependencies: | ||
| Ce projet est un serveur backend pour un système domotique, conçu pour être rapide, modulaire et orienté événements. Il gère l'état de la maison (lumières, chauffage, portes, température), conserve un historique des événements et automatise certaines tâches via un moteur de règles intelligent. | ||
|
|
||
| ```bash | ||
| bun install | ||
| ``` | ||
| ## 🛠 Stack Technique | ||
|
|
||
| To install Lefthook git hooks: | ||
| * **Runtime:** [Bun](https://bun.sh/) (JavaScript/TypeScript runtime ultra-rapide) | ||
| * **Framework Web:** [ElysiaJS](https://elysiajs.com/) | ||
| * **Base de données:** PostgreSQL | ||
| * **ORM:** [Prisma](https://www.prisma.io/) | ||
| * **Outils:** Biome (Linter/Formatter), Lefthook (Git Hooks), Docker | ||
|
|
||
| ```bash | ||
| bunx lefthook install | ||
| ``` | ||
| ## 🚀 Installation et Démarrage | ||
|
|
||
| To run: | ||
| ### Prérequis | ||
| * Bun installé (`curl -fsSL https://bun.sh/install | bash`) | ||
| * Docker et Docker Compose (pour la base de données) | ||
|
|
||
| ```bash | ||
| bun run start | ||
| ``` | ||
| ### Configuration | ||
|
|
||
| Or with Docker: | ||
| 1. **Cloner le projet** et installer les dépendances : | ||
| ```bash | ||
| bun install | ||
| ``` | ||
|
|
||
| ```bash | ||
| docker compose up --build | ||
| ``` | ||
| 2. **Configurer les variables d'environnement** : | ||
| Copiez le fichier d'exemple et adaptez-le (notamment l'URL de la base de données). | ||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| To run knip to check for unused dependencies: | ||
| 3. **Démarrer la base de données** : | ||
| ```bash | ||
| docker compose up -d | ||
| ``` | ||
|
|
||
| ```bash | ||
| bun knip | ||
| ``` | ||
| ### Lancer le serveur | ||
|
|
||
| To format code: | ||
| * **Mode développement** (avec rechargement automatique) : | ||
| ```bash | ||
| bun run dev | ||
| ``` | ||
| * **Mode production** : | ||
| ```bash | ||
| bun start | ||
| ``` | ||
|
|
||
| ```bash | ||
| bun format | ||
| ``` | ||
| ## 🔐 Authentification | ||
|
|
||
| To lint code: | ||
| L'API utilise un système d'authentification personnalisé basé sur un couple `ClientID` et `ClientToken`. | ||
|
|
||
| ```bash | ||
| bun lint | ||
| ``` | ||
| * **Header requis :** `Authorization` | ||
| * **Format :** `ClientID:ClientToken` | ||
| * **Validation :** Le serveur vérifie que le `ClientID` existe et que le token fourni correspond au token chiffré stocké en base. | ||
|
|
||
| > ⚠️ **Note :** La route `/status` est publique. Toutes les autres routes (`/check`, `/history`, `/temp`, `/toggle`, `/auth`) sont protégées par le middleware d'authentification. | ||
|
|
||
| ## 📡 API Reference | ||
|
|
||
| ### Endpoints REST | ||
|
|
||
| #### Système | ||
| * `GET /status` : Vérifier l'état du serveur (Public). | ||
| * `GET /check` : Vérification de santé avancée (Protégé). | ||
|
|
||
| #### Contrôle (Toggle) | ||
| Ces routes permettent de modifier l'état des appareils. | ||
| * `POST /toggle/light` : Allumer/Éteindre la lumière. | ||
| * `POST /toggle/door` : Ouvrir/Fermer la porte. | ||
| * `POST /toggle/heat` : Activer/Désactiver le chauffage. | ||
|
|
||
| #### Température | ||
| * `POST /temp` : Mettre à jour la température actuelle de la maison. | ||
| * *Body :* `{ "temp": "number" }` | ||
|
|
||
| To check code: | ||
| #### Historique | ||
| * `GET /history` : Récupérer l'historique des événements (changements d'état, règles déclenchées). | ||
|
|
||
| ```bash | ||
| bun check | ||
| ### WebSocket (`/ws`) | ||
|
|
||
| Le serveur expose un endpoint WebSocket pour les mises à jour en temps réel. | ||
| * **Topic :** `home-updates` | ||
| * **Fonctionnement :** Le dashboard reçoit automatiquement les changements d'état (nouvelle température, lumière allumée, etc.) dès qu'ils se produisent. | ||
|
|
||
| ## 🧠 Moteur de Règles (Automation) | ||
|
|
||
| Le système intègre un moteur de règles (`src/rules/engine.ts`) qui réagit aux changements d'état (`EVENTS.STATE_CHANGE`). | ||
|
|
||
| ### Règles Actives (`src/rules/definitions.ts`) | ||
|
|
||
| 1. **HEAT_ON_COLD (Chauffage Auto)** | ||
| * *Condition :* Température < 19°C **ET** Porte fermée **ET** Chauffage éteint. | ||
| * *Action :* Allume le chauffage. | ||
|
|
||
| 2. **HEAT_OFF_HOT (Économie Chauffage)** | ||
| * *Condition :* Température > 23°C **ET** Chauffage allumé. | ||
| * *Action :* Éteint le chauffage. | ||
|
|
||
| 3. **LIGHT_ON_ENTRY (Lumière Entrée)** | ||
| * *Condition :* Porte ouverte **ET** Lumière éteinte. | ||
| * *Action :* Allume la lumière (Bienvenue !). | ||
|
|
||
| 4. **ECO_GUARD_DOOR (Sécurité Énergie)** | ||
| * *Condition :* Porte ouverte **ET** Chauffage allumé. | ||
| * *Action :* Coupe le chauffage pour ne pas chauffer l'extérieur. | ||
|
|
||
| ## 📂 Architecture du Code | ||
|
|
||
| ``` | ||
| . | ||
| ├── prisma/ # Schéma DB, Migrations et Seeds | ||
| ├── src/ | ||
| │ ├── middleware/ # AuthMiddleware (vérification token) | ||
| │ ├── routes/ # Définition des routes API (Elysia) | ||
| │ ├── rules/ # Moteur de règles et définitions | ||
| │ ├── services/ # Logique métier (HomeStateService) | ||
| │ ├── utils/ # Utilitaires (Crypto, EventBus) | ||
| │ ├── enums.ts # Types d'événements (TEMPERATURE, LIGHT...) | ||
| │ └── index.ts # Point d'entrée serveur | ||
| └── tests/ # Tests unitaires et d'intégration | ||
| ``` | ||
|
|
||
| This project was created using `bun init` in bun v1.3.4. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. | ||
| ## ✅ Tests et Qualité | ||
|
|
||
| * **Linter le code :** `bun run lint` (via Biome) | ||
| * **Lancer les tests :** `bun test` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Dockerfile attempts to copy a file
prisma.config.tsthat does not appear to exist in the repository. This will cause the Docker build to fail. Either create this file or remove this line from the Dockerfile.