add\fix basepath
All checks were successful
Build SMS Gateway / Build image (push) Successful in 11s

This commit is contained in:
2026-01-19 21:04:12 +07:00
parent e3be88efbe
commit 535ea8356f
4 changed files with 26 additions and 8 deletions

View File

@@ -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

View File

@@ -1 +1 @@
0.1.0
0.2.1

View File

@@ -9,4 +9,5 @@ services:
environment:
- SMS_LOGIN=admin
- SMS_PASSWORD=secret
# - BASE_PATH=/sms
restart: unless-stopped

View File

@@ -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}`);
});