# adam-golab/sonoff-server architecture

> Architecture diagram of the GitHub repository adam-golab/sonoff-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adam-golab/sonoff-server
- Repository: https://github.com/adam-golab/sonoff-server

## Overview

##### Main Components
- HTTPS Server and Request Entry Points:
- Files such as app.js, index.js, and wsApp.js serve as the entry points.
- The server is configured to use HTTPS with the provided certificates in the “certs” directory.
- Routing and API Endpoints:
- The “routes” directory contains devicesRouter.js and dispatchRouter.js, which map requests to the proper functionality.
- Service Layer:
- The “services” directory encapsulates helper functionalities such as fileReader.js, logger.js, and wsSender.js. These components likely support logging, file management, and websocket communications respectively.
- State Management:
- The “store” folder (with files like actionTypes.js, actions.js, reducer.js, sideEffects.js, sideEffectsMiddleware.js, etc.) suggests an internal state management pattern. This could be used to track device statuses and handle side effects when actions are dispatched (such as turning devices on or off).
- Configuration:
- The config.js file and certificate files indicate how HTTPS is configured and hints at additional settings the server might need.

##### Relationships and Interactions
- Client Requests > HTTPS Server:
- Incoming HTTPS requests (for endpoints like /devices or /devices/:deviceId/on) are received by app.js or index.js.
- Routing:
- The router components in the “routes” directory parse the URL paths and delegate the work to relevant controllers.
- Service Calls:
- When a route is hit, the corresponding service functions (e.g., wsSender.js for sending commands; logger.js for logging events) are invoked.
- State Management:
- Actions triggered by HTTP requests are dispatched to the state store, where sideEffectsMiddleware might handle asynchronous behavior (e.g., contacting a device through wsSender).
- Websocket Interaction:
- The file wsApp.js and wsSender.js indicate that there is also a websocket (WS) component for real-time interactions with devices.

##### Important Architectural Patterns or Design Principles
- Separation of Concerns:
- The clear delineation among the routes, services, and store demonstrates a layered architectural style.
- Middleware and Side Effects Management:
- The use of “sideEffectsMiddleware.js” in the store folder implies a pattern (similar to Redux middleware) for handling asynchronous operations and side effects.
- Security with HTTPS:
- The server emphasizes secure communication through certificates (even if self-signed).

##### Checklist for the Principal Software Engineer
- Keep it detailed by not only separating the components but also illustrating critical interactions such as middleware activation in state management and the flow of control when a device command is executed.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global External Node
    CR["Client Requests"]:::external

    %% HTTP API Flow Subgraph
    subgraph "HTTP API Flow"
        %% Entry Points Subgraph
        subgraph "Entry Points"
            A1["app.js"]:::entry
            I1["index.js"]:::entry
        end
        HC["HTTPS Config (config.js & certs)"]:::config

        %% Routing Layer Subgraph
        subgraph "Routing Layer"
            DR["devicesRouter.js"]:::route
            DSR["dispatchRouter.js"]:::route
        end

        %% Service Layer Subgraph
        subgraph "Service Layer"
            FR["fileReader.js"]:::service
            LG["logger.js"]:::service
            WS_Sender["wsSender.js"]:::service
        end

        %% State Management
        ST["State Management (Store)"]:::store
    end

    %% WebSocket Flow
    WS["WS Server (wsApp.js)"]:::entry
    SD["Sonoff Devices"]:::external

    %% Connections for HTTP API Flow
    CR --> A1
    HC --> A1
    A1 --> I1
    I1 --> DR
    DR --> DSR
    DSR --> FR
    FR --> LG
    LG --> WS_Sender
    WS_Sender --> ST

    %% Connection for WebSocket Flow
    CR --> WS
    WS --> SD

    %% Click Events for Entry Points and Configurations
    click A1 "https://github.com/adam-golab/sonoff-server/blob/master/app.js"
    click I1 "https://github.com/adam-golab/sonoff-server/blob/master/index.js"
    click WS "https://github.com/adam-golab/sonoff-server/blob/master/wsApp.js"
    click HC "https://github.com/adam-golab/sonoff-server/blob/master/config.js"
    click HC "https://github.com/adam-golab/sonoff-server/tree/master/certs/"

    %% Click Events for Routing
    click DR "https://github.com/adam-golab/sonoff-server/blob/master/routes/devicesRouter.js"
    click DSR "https://github.com/adam-golab/sonoff-server/blob/master/routes/dispatchRouter.js"

    %% Click Events for Service Layer
    click FR "https://github.com/adam-golab/sonoff-server/blob/master/services/fileReader.js"
    click LG "https://github.com/adam-golab/sonoff-server/blob/master/services/logger.js"
    click WS_Sender "https://github.com/adam-golab/sonoff-server/blob/master/services/wsSender.js"

    %% Click Event for State Management
    click ST "https://github.com/adam-golab/sonoff-server/tree/master/store/"

    %% Classes/Styles
    classDef entry fill:#E3F2FD,stroke:#1E88E5,stroke-width:2px;
    classDef config fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef route fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef service fill:#FFCDD2,stroke:#D32F2F,stroke-width:2px;
    classDef store fill:#D1C4E9,stroke:#512DA8,stroke-width:2px;
    classDef external fill:#FFE0B2,stroke:#F57C00,stroke-width:2px;
```
