open ntfy server by default
This commit is contained in:
parent
a6f7b92423
commit
3583b06022
3 changed files with 49 additions and 31 deletions
14
env.example
14
env.example
|
|
@ -1,8 +1,16 @@
|
||||||
# Copy this file to .env and configure your values
|
# Copy this file to .env and configure your values
|
||||||
NTFY_URL=https://your-ntfy-server.com
|
|
||||||
NTFY_USER=your-username
|
# Required: The topic to send messages to
|
||||||
NTFY_PASSWORD=your-password
|
|
||||||
NTFY_TOPIC=emergency
|
NTFY_TOPIC=emergency
|
||||||
|
|
||||||
|
# Optional: ntfy server URL (defaults to public instance https://ntfy.sh)
|
||||||
|
# NTFY_URL=https://your-ntfy-server.com
|
||||||
|
|
||||||
|
# Optional: Authentication (only needed for private ntfy servers)
|
||||||
|
# NTFY_USER=your-username
|
||||||
|
# NTFY_PASSWORD=your-password
|
||||||
|
|
||||||
|
# Optional: Server configuration
|
||||||
PORT=3000
|
PORT=3000
|
||||||
UI_MESSAGE=Send an emergency message
|
UI_MESSAGE=Send an emergency message
|
||||||
|
|
||||||
|
|
|
||||||
48
server.js
48
server.js
|
|
@ -3,15 +3,9 @@ const axios = require('axios');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
function createApp(axiosClient) {
|
function createApp(axiosClient, config) {
|
||||||
const app = express();
|
const app = express();
|
||||||
const UI_MESSAGE = process.env.UI_MESSAGE || 'Emergency Message';
|
const { uiMessage, ntfyUrl, ntfyUser, ntfyPassword, ntfyTopic } = config;
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
|
@ -23,7 +17,7 @@ function createApp(axiosClient) {
|
||||||
|
|
||||||
app.get('/api/config', (req, res) => {
|
app.get('/api/config', (req, res) => {
|
||||||
res.json({
|
res.json({
|
||||||
uiMessage: UI_MESSAGE || 'Emergency Message'
|
uiMessage: uiMessage || 'Emergency Message'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -40,15 +34,20 @@ function createApp(axiosClient) {
|
||||||
|
|
||||||
const fullMessage = `From [${name}], message: ${message}`;
|
const fullMessage = `From [${name}], message: ${message}`;
|
||||||
|
|
||||||
const ntfyResponse = await axiosClient.post(`${NTFY_URL}/${NTFY_TOPIC}`, fullMessage, {
|
const requestOptions = {
|
||||||
auth: {
|
|
||||||
username: NTFY_USER,
|
|
||||||
password: NTFY_PASSWORD
|
|
||||||
},
|
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'text/plain'
|
'Content-Type': 'text/plain'
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (ntfyUser && ntfyPassword) {
|
||||||
|
requestOptions.auth = {
|
||||||
|
username: ntfyUser,
|
||||||
|
password: ntfyPassword
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const ntfyResponse = await axiosClient.post(`${ntfyUrl}/${ntfyTopic}`, fullMessage, requestOptions);
|
||||||
|
|
||||||
if (ntfyResponse.status === 200) {
|
if (ntfyResponse.status === 200) {
|
||||||
res.json({ success: true, message: 'Message sent successfully' });
|
res.json({ success: true, message: 'Message sent successfully' });
|
||||||
|
|
@ -79,18 +78,27 @@ if (require.main === module) {
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
// Environment variables for ntfy configuration
|
// Environment variables for ntfy configuration
|
||||||
const NTFY_URL = process.env.NTFY_URL;
|
const NTFY_URL = process.env.NTFY_URL || 'https://ntfy.sh';
|
||||||
const NTFY_USER = process.env.NTFY_USER;
|
const NTFY_USER = process.env.NTFY_USER;
|
||||||
const NTFY_PASSWORD = process.env.NTFY_PASSWORD;
|
const NTFY_PASSWORD = process.env.NTFY_PASSWORD;
|
||||||
const NTFY_TOPIC = process.env.NTFY_TOPIC;
|
const NTFY_TOPIC = process.env.NTFY_TOPIC;
|
||||||
|
const UI_MESSAGE = process.env.UI_MESSAGE || 'Emergency Message';
|
||||||
|
|
||||||
if (!NTFY_URL || !NTFY_USER || !NTFY_PASSWORD || !NTFY_TOPIC) {
|
if (!NTFY_TOPIC) {
|
||||||
console.error('Missing required environment variables: NTFY_URL, NTFY_USER, NTFY_PASSWORD, NTFY_TOPIC');
|
console.error('Missing required environment variable: NTFY_TOPIC');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create app with real axios
|
// Create app with configuration
|
||||||
const app = createApp(axios);
|
const config = {
|
||||||
|
uiMessage: UI_MESSAGE,
|
||||||
|
ntfyUrl: NTFY_URL,
|
||||||
|
ntfyUser: NTFY_USER,
|
||||||
|
ntfyPassword: NTFY_PASSWORD,
|
||||||
|
ntfyTopic: NTFY_TOPIC
|
||||||
|
};
|
||||||
|
|
||||||
|
const app = createApp(axios, config);
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server running on port ${PORT}`);
|
console.log(`Server running on port ${PORT}`);
|
||||||
|
|
|
||||||
18
test.js
18
test.js
|
|
@ -6,15 +6,17 @@ const createApp = require('./server');
|
||||||
jest.mock('axios');
|
jest.mock('axios');
|
||||||
const mockedAxios = axios;
|
const mockedAxios = axios;
|
||||||
|
|
||||||
// Mock environment variables
|
// Create test configuration
|
||||||
process.env.NTFY_URL = 'https://ntfy.sh';
|
const testConfig = {
|
||||||
process.env.NTFY_USER = 'testuser';
|
uiMessage: 'Test Emergency Message',
|
||||||
process.env.NTFY_PASSWORD = 'testpass';
|
ntfyUrl: 'https://ntfy.sh',
|
||||||
process.env.NTFY_TOPIC = 'test-topic';
|
ntfyUser: 'testuser',
|
||||||
process.env.UI_MESSAGE = 'Test Emergency Message';
|
ntfyPassword: 'testpass',
|
||||||
|
ntfyTopic: 'test-topic'
|
||||||
|
};
|
||||||
|
|
||||||
// Create app with mocked axios
|
// Create app with mocked axios and test config
|
||||||
const app = createApp(mockedAxios);
|
const app = createApp(mockedAxios, testConfig);
|
||||||
|
|
||||||
describe('Emergency Message App', () => {
|
describe('Emergency Message App', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue