ntfy-emergency-app/server.js

101 lines
3.1 KiB
JavaScript
Raw Normal View History

2025-10-16 09:50:36 +02:00
const express = require('express');
const axios = require('axios');
const path = require('path');
2025-10-17 19:35:19 +02:00
require('dotenv').config();
2025-10-16 09:50:36 +02:00
2025-10-18 19:05:34 +02:00
function createApp(axiosClient) {
const app = express();
const UI_MESSAGE = process.env.UI_MESSAGE || 'Emergency Message';
2025-10-16 09:50:36 +02:00
2025-10-18 19:05:34 +02:00
// Environment variables for ntfy configuration
const NTFY_URL = process.env.NTFY_URL;
const NTFY_USER = process.env.NTFY_USER;
const NTFY_PASSWORD = process.env.NTFY_PASSWORD;
const NTFY_TOPIC = process.env.NTFY_TOPIC;
2025-10-16 09:50:36 +02:00
2025-10-18 19:05:34 +02:00
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
2025-10-16 09:50:36 +02:00
2025-10-18 19:05:34 +02:00
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
2025-10-16 09:50:36 +02:00
2025-10-18 19:05:34 +02:00
app.get('/api/config', (req, res) => {
res.json({
uiMessage: UI_MESSAGE || 'Emergency Message'
});
2025-10-18 18:13:47 +02:00
});
2025-10-18 19:05:34 +02:00
app.post('/send-message', async (req, res) => {
try {
const { name, message } = req.body;
if (!name || !message) {
return res.status(400).json({
success: false,
error: 'Name and message are required'
});
}
const fullMessage = `From [${name}], message: ${message}`;
const ntfyResponse = await axiosClient.post(`${NTFY_URL}/${NTFY_TOPIC}`, fullMessage, {
auth: {
username: NTFY_USER,
password: NTFY_PASSWORD
},
headers: {
'Content-Type': 'text/plain'
}
2025-10-16 09:50:36 +02:00
});
2025-10-18 19:05:34 +02:00
if (ntfyResponse.status === 200) {
res.json({ success: true, message: 'Message sent successfully' });
} else {
res.status(500).json({
success: false,
error: 'Error sending message'
});
2025-10-16 09:50:36 +02:00
}
2025-10-18 19:05:34 +02:00
} catch (error) {
console.error('Error sending message:', error.message);
2025-10-16 09:50:36 +02:00
res.status(500).json({
success: false,
2025-10-18 19:05:34 +02:00
error: 'Internal server error'
2025-10-16 09:50:36 +02:00
});
}
2025-10-18 19:05:34 +02:00
});
return app;
}
// Export the createApp function for testing
module.exports = createApp;
// Only start server if this file is run directly (not imported)
if (require.main === module) {
const PORT = process.env.PORT || 3000;
// Environment variables for ntfy configuration
const NTFY_URL = process.env.NTFY_URL;
const NTFY_USER = process.env.NTFY_USER;
const NTFY_PASSWORD = process.env.NTFY_PASSWORD;
const NTFY_TOPIC = process.env.NTFY_TOPIC;
if (!NTFY_URL || !NTFY_USER || !NTFY_PASSWORD || !NTFY_TOPIC) {
console.error('Missing required environment variables: NTFY_URL, NTFY_USER, NTFY_PASSWORD, NTFY_TOPIC');
process.exit(1);
2025-10-16 09:50:36 +02:00
}
2025-10-18 19:05:34 +02:00
// Create app with real axios
const app = createApp(axios);
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`ntfy URL: ${NTFY_URL}`);
console.log(`ntfy Topic: ${NTFY_TOPIC}`);
});
}