From a3902c9662ca91d893de7d97659bfa3d6138d7c1 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Fri, 13 Nov 2026 18:27:16 +0100 Subject: [PATCH] =?UTF-8?q?Ajoute=20le=20d=C3=A9ploiement=20en=20prod=20lo?= =?UTF-8?q?cale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Makefile (setup, deploy avec sauvegarde de la base, status, logs, dev), vhost Apache en reverse proxy sur tempus.loc et service systemd utilisateur, binaire installé dans /var/www/tempus. Co-Authored-By: Claude Opus 5.5 --- Makefile | 53 +++++++++++++++++++++++++++++++++++++++++++ README.md | 17 +++++++++++++- deploy/tempus.service | 15 ++++++++++++ deploy/vh_tempus.conf | 8 +++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 deploy/tempus.service create mode 100644 deploy/vh_tempus.conf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..58eb2ca --- /dev/null +++ b/Makefile @@ -0,0 +1,53 @@ +DEPLOY_DIR ?= /var/www/tempus +SERVICE ?= tempus +HOST ?= tempus.loc +ADDR ?= 127.0.0.1:3110 +UNIT := $(HOME)/.config/systemd/user/$(SERVICE).service +SUBST := sed -e 's|@HOST@|$(HOST)|g' -e 's|@ADDR@|$(ADDR)|g' -e 's|@DEPLOY_DIR@|$(DEPLOY_DIR)|g' + +.PHONY: build setup deploy backup status logs dev + +## Compile le binaire (templates, CSS/JS et migrations sont embarqués). +build: + cargo build --release + +## Une seule fois : dossier, vhost Apache et /etc/hosts (sudo), service systemd utilisateur. +setup: + sudo install -d -o $(USER) -g www-data -m 755 $(DEPLOY_DIR) + $(SUBST) deploy/vh_tempus.conf | sudo tee /etc/apache2/sites-available/vh_tempus.conf >/dev/null + sudo a2ensite -q vh_tempus + sudo systemctl reload apache2 + grep -qw '$(HOST)' /etc/hosts || printf '127.0.0.1\t$(HOST)\n' | sudo tee -a /etc/hosts >/dev/null + install -d $(dir $(UNIT)) + $(SUBST) deploy/tempus.service > $(UNIT) + systemctl --user daemon-reload + systemctl --user enable $(SERVICE) + +## Sauvegarde la base, installe le nouveau binaire et redémarre le service. +deploy: build + systemctl --user stop $(SERVICE) + $(MAKE) --no-print-directory backup + install -m 755 target/release/tempus $(DEPLOY_DIR)/tempus + systemctl --user start $(SERVICE) + @for i in $$(seq 1 50); do \ + curl -sf -o /dev/null http://$(ADDR)/health && echo "Déployé : http://$(HOST)" && exit 0; \ + sleep 0.1; \ + done; echo "Le service ne répond pas : make logs" >&2; exit 1 + +## Copie la base (service arrêté) dans $(DEPLOY_DIR)/backups, garde les 10 dernières. +backup: + @if [ -f $(DEPLOY_DIR)/tempus.db ]; then \ + d=$(DEPLOY_DIR)/backups/$$(date +%Y%m%d-%H%M%S); mkdir -p $$d; \ + cp $(DEPLOY_DIR)/tempus.db* $$d/ && echo "Sauvegarde : $$d"; \ + ls -1dt $(DEPLOY_DIR)/backups/*/ | tail -n +11 | xargs -r rm -r; \ + fi + +status: + systemctl --user status $(SERVICE) --no-pager + +logs: + journalctl --user -u $(SERVICE) -f + +## Dev : recompile, redémarre et recharge le navigateur à chaque modif. +dev: + watchexec -r -e rs,html,css,js,sql -- cargo run --features livereload diff --git a/README.md b/README.md index b942844..cdd89cf 100644 --- a/README.md +++ b/README.md @@ -51,13 +51,28 @@ curl -XPOST localhost:7878/api/clients -H 'content-type: application/json' -d '{ curl -XPOST localhost:7878/api/timer/start -H 'content-type: application/json' -d '{"project_id":1}' ``` +## Production locale + +Binaire dans `/var/www/tempus`, service systemd utilisateur, vhost Apache +(reverse proxy) sur http://tempus.loc → `127.0.0.1:3110`. + +```sh +make setup # une fois (sudo) : dossier, vhost, /etc/hosts, service +make deploy # compile, sauvegarde la base, installe, redémarre +make status # / make logs +``` + +La base de prod est `/var/www/tempus/tempus.db` ; chaque `make deploy` la copie +dans `/var/www/tempus/backups/` (10 dernières conservées). Variables +surchargeables : `DEPLOY_DIR`, `HOST`, `ADDR`, `SERVICE`. + ## Développement Rechargement automatique (recompile, redémarre et rafraîchit le navigateur) : ```sh cargo install watchexec-cli # une fois -watchexec -r -e rs,html,css,js,sql -- cargo run --features livereload +make dev ``` La feature `livereload` injecte un script dans les pages (pas dans les fragments diff --git a/deploy/tempus.service b/deploy/tempus.service new file mode 100644 index 0000000..68a290c --- /dev/null +++ b/deploy/tempus.service @@ -0,0 +1,15 @@ +[Unit] +Description=Tempus (suivi du temps) +After=network.target + +[Service] +Type=simple +WorkingDirectory=@DEPLOY_DIR@ +Environment=TEMPUS_DB=@DEPLOY_DIR@/tempus.db +Environment=TEMPUS_ADDR=@ADDR@ +ExecStart=@DEPLOY_DIR@/tempus +Restart=on-failure +RestartSec=3 + +[Install] +WantedBy=default.target diff --git a/deploy/vh_tempus.conf b/deploy/vh_tempus.conf new file mode 100644 index 0000000..4312154 --- /dev/null +++ b/deploy/vh_tempus.conf @@ -0,0 +1,8 @@ + + ServerName @HOST@ + ProxyPreserveHost On + ProxyPass / http://@ADDR@/ + ProxyPassReverse / http://@ADDR@/ + ErrorLog /var/log/apache2/error.@HOST@.log + CustomLog /var/log/apache2/access.@HOST@.log combined +