From b2d268c5a0eddc21da6fc718a9fc9d8ae867d31b Mon Sep 17 00:00:00 2001 From: Mateusz Date: Sun, 20 Sep 2026 21:07:10 +0200 Subject: [PATCH] server production version - gunicorn --- server/README.md | 22 ++++++++++++++++++++++ server/gunicorn.conf.py | 9 +++++++++ server/requirements.txt | 3 ++- server/yggdrasil.service | 14 ++++++++++++++ thermo_sensor/main/thermo_sensor.c | 3 ++- thermo_sensor/main/ygg_http_req.c | 7 ++++--- thermo_sensor/main/ygg_http_req.h | 3 ++- thermo_sensor/main/ygg_state.h | 1 + 8 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 server/gunicorn.conf.py create mode 100644 server/yggdrasil.service diff --git a/server/README.md b/server/README.md index a89fedd..9a2244a 100644 --- a/server/README.md +++ b/server/README.md @@ -48,12 +48,34 @@ python seed.py ## Running +### Development + ```bash python app.py ``` The server starts on `http://localhost:5000`. +### Production + +```bash +# Install dependencies (includes gunicorn) +pip install -r requirements.txt + +# Run with gunicorn +gunicorn -c gunicorn.conf.py "app:create_app()" +``` + +Gunicorn starts on `http://0.0.0.0:8000` with `cpu * 2 + 1` workers. + +#### systemd service + +```bash +sudo cp yggdrasil.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable --now yggdrasil +``` + | Route | Description | |---|---| | `/sensors` | Dashboard with interactive chart and data table | diff --git a/server/gunicorn.conf.py b/server/gunicorn.conf.py new file mode 100644 index 0000000..a8365eb --- /dev/null +++ b/server/gunicorn.conf.py @@ -0,0 +1,9 @@ +import os + +bind = "0.0.0.0:8000" +workers = min(os.cpu_count() * 2 + 1, 9) +timeout = 30 +keepalive = 5 +errorlog = "-" +accesslog = "-" +loglevel = "info" diff --git a/server/requirements.txt b/server/requirements.txt index 4996433..0854868 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,3 +1,4 @@ flask>=3.0 flask-sqlalchemy>=3.1 -alembic>=1.13 \ No newline at end of file +alembic>=1.13 +gunicorn>=22.0 \ No newline at end of file diff --git a/server/yggdrasil.service b/server/yggdrasil.service new file mode 100644 index 0000000..3628ae3 --- /dev/null +++ b/server/yggdrasil.service @@ -0,0 +1,14 @@ +[Unit] +Description=Yggdrasil Sensor API +After=network.target + +[Service] +User=mateusz +Group=mateusz +WorkingDirectory=/home/mateusz/prog/yggdrasil/server +ExecStart=/home/mateusz/prog/yggdrasil/server/.venv/bin/gunicorn -c gunicorn.conf.py "app:create_app()" +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target diff --git a/thermo_sensor/main/thermo_sensor.c b/thermo_sensor/main/thermo_sensor.c index e4a3984..82c3057 100644 --- a/thermo_sensor/main/thermo_sensor.c +++ b/thermo_sensor/main/thermo_sensor.c @@ -22,7 +22,8 @@ void main_loop(void *pvParameters) { vTaskDelay(pdMS_TO_TICKS(1000)); while (1) { ygg_bmp280_read_sensor(&dev_bmp_280); - ygg_send_sensor_readouts(SENSOR_NAME, + ygg_send_sensor_readouts(BASE_URL, + SENSOR_NAME, CURR_READOUT); vTaskDelay(pdMS_TO_TICKS(10000)); } diff --git a/thermo_sensor/main/ygg_http_req.c b/thermo_sensor/main/ygg_http_req.c index 65a5ef9..65832d3 100644 --- a/thermo_sensor/main/ygg_http_req.c +++ b/thermo_sensor/main/ygg_http_req.c @@ -106,13 +106,14 @@ char* ygg_send_post(const char *url, char *post_data) { int ygg_get_sensor_id(const char *sensor_name, const char *url); -void ygg_send_sensor_readouts(const char *sensor_name, +void ygg_send_sensor_readouts(const char *base_url, + const char *sensor_name, ygg_sensor_readout readout) { char post_data[64]; - int sensor_id = ygg_get_sensor_id(sensor_name, "http://192.168.0.236:5000"); + int sensor_id = ygg_get_sensor_id(sensor_name, base_url); char url[128]; snprintf(url, sizeof(url), - "http://192.168.0.236:5000/api/sensors/%d/readouts", sensor_id); + "%s/api/sensors/%d/readouts", base_url, sensor_id); snprintf(post_data, sizeof(post_data), "{\"type\":\"temperature\", \"value\":%.2f}", readout.temperature); diff --git a/thermo_sensor/main/ygg_http_req.h b/thermo_sensor/main/ygg_http_req.h index 6462765..3660d6a 100644 --- a/thermo_sensor/main/ygg_http_req.h +++ b/thermo_sensor/main/ygg_http_req.h @@ -3,7 +3,8 @@ #include "ygg_state.h" -void ygg_send_sensor_readouts(const char *sensor_name, +void ygg_send_sensor_readouts(const char *base_url, + const char *sensor_name, ygg_sensor_readout readout); #endif diff --git a/thermo_sensor/main/ygg_state.h b/thermo_sensor/main/ygg_state.h index 8bde21c..7098bea 100644 --- a/thermo_sensor/main/ygg_state.h +++ b/thermo_sensor/main/ygg_state.h @@ -9,5 +9,6 @@ typedef struct { extern ygg_sensor_readout CURR_READOUT; static const char *SENSOR_NAME = "BMP_280"; +static const char *BASE_URL = "http://192.168.0.236:5000"; #endif