From 535ea8356f0ae7647698c2b29a3cb692211e5e45 Mon Sep 17 00:00:00 2001 From: Vassiliy Yegorov Date: Mon, 19 Jan 2026 21:04:12 +0700 Subject: [PATCH] add\fix basepath --- README.md | 10 +++++++++- VERSION | 2 +- docker-compose.yml | 1 + server.js | 21 +++++++++++++++------ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d6a4e0a..ff4eb2b 100644 --- a/README.md +++ b/README.md @@ -127,15 +127,23 @@ curl -X POST http://localhost:3000/clear-all-sms | PORT | 3000 | Порт сервера | | SMS_LOGIN | - | Логин для авторизации (обяз) | | SMS_PASSWORD | - | Пароль для авторизации (обяз)| +| BASE_PATH | / | Базовый путь для всех роутов | -Переменные `SMS_LOGIN` и `SMS_PASSWORD` задаются в `docker-compose.yml`: +Пример настройки в `docker-compose.yml`: ```yaml environment: - SMS_LOGIN=admin - SMS_PASSWORD=secret + - BASE_PATH=/sms ``` +При `BASE_PATH=/sms` все эндпоинты будут доступны по путям: +- `POST /sms/send-msg` +- `GET /sms/view-all-sms` +- `POST /sms/clear-all-sms` +- `GET /sms/` - веб-интерфейс + Изменение порта в docker-compose.yml: ```yaml diff --git a/VERSION b/VERSION index 6c6aa7c..7dff5b8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 \ No newline at end of file +0.2.1 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3682afe..5226cdb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,4 +9,5 @@ services: environment: - SMS_LOGIN=admin - SMS_PASSWORD=secret + # - BASE_PATH=/sms restart: unless-stopped diff --git a/server.js b/server.js index f6bcb15..60facff 100644 --- a/server.js +++ b/server.js @@ -5,15 +5,18 @@ const path = require("path"); const app = express(); const PORT = process.env.PORT || 3000; const BASE_FILE = path.join(__dirname, "data", "base.json"); +const BASE_PATH = (process.env.BASE_PATH || "/").replace(/\/$/, "") || "/"; // Auth credentials from environment const AUTH_LOGIN = process.env.SMS_LOGIN; const AUTH_PASSWORD = process.env.SMS_PASSWORD; +// Router for all endpoints +const router = express.Router(); + // Middleware app.use(express.urlencoded({ extended: true })); app.use(express.json()); -app.use(express.static("public")); // Ensure data directory and base.json exist function ensureBaseFile() { @@ -40,7 +43,7 @@ function writeMessages(messages) { } // POST /send-msg - receive and log SMS -app.post("/send-msg", (req, res) => { +router.post("/send-msg", (req, res) => { const { login, psw, phones, mes } = req.body; if (!login || !psw || !phones || !mes) { @@ -70,25 +73,31 @@ app.post("/send-msg", (req, res) => { }); // GET /view-all-sms - view all logged messages -app.get("/view-all-sms", (req, res) => { +router.get("/view-all-sms", (req, res) => { const messages = readMessages(); res.json({ messages }); }); // POST /clear-all-sms - clear all messages -app.post("/clear-all-sms", (req, res) => { +router.post("/clear-all-sms", (req, res) => { writeMessages([]); console.log("All messages cleared"); res.json({ status: "ok", message: "All messages cleared" }); }); +// Serve static files +router.use(express.static(path.join(__dirname, "public"))); + // GET / - serve main page -app.get("/", (req, res) => { +router.get("/", (req, res) => { res.sendFile(path.join(__dirname, "public", "index.html")); }); +// Mount router at BASE_PATH +app.use(BASE_PATH, router); + // Initialize and start server ensureBaseFile(); app.listen(PORT, () => { - console.log(`SMS OTP Gateway running on port ${PORT}`); + console.log(`SMS OTP Gateway running on port ${PORT}, base path: ${BASE_PATH}`); });