-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
142 lines (110 loc) · 3.44 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const express = require('express');
var logger = require('morgan');
const formData = require('form-data');
const Mailgun = require('mailgun.js');
const { Client } = require('pg');
require('dotenv').config();
const client = new Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
ssl: {
rejectUnauthorized: false,
}
});
const mailgun = new Mailgun(formData);
const mg = mailgun.client({
username: 'api',
key: process.env.MAILGUN_API_KEY,
url: process.env.MAILGUN_URL
});
client.connect()
.then(() => { })
.catch(err => console.error('Connection error', err.stack));
var app = express();
app.set('trust proxy', true);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/api/v1', function (req, res, next) {
const apiKey = process.env.API_KEY;
const authHeader = req.headers.authorization;
if (authHeader && authHeader.startsWith('Basic ')) {
const basicCredentials = authHeader.split(' ')[1];
if (basicCredentials === apiKey) {
return next();
}
}
const authBody = req.body.token;
if (authBody === apiKey) {
return next();
}
res.status(401).end();
});
app.post('/api/v1/telemetry_host', async (req, res) => {
const remoteAddress = req.ip;
const data = req.body;
if (!data.instance) {
return res.status(400).json({ error: 'instance is required' });
}
await client.query('INSERT INTO public.telemetry_host("instance", remote_address, "data") VALUES($1, $2, $3)', [
data.instance,
remoteAddress,
data
])
res.status(202).end();
});
app.post('/api/v1/telemetry_gps', async (req, res) => {
const remoteAddress = req.ip;
const data = req.body;
if (!data.instance) {
return res.status(400).json({ error: 'instance is required' });
}
await client.query('INSERT INTO public.telemetry_gps("instance", remote_address, location, "data") VALUES($1, $2, POINT($3, $4), $5)', [
data.instance,
remoteAddress,
data.lat || 0,
data.lon || 0,
data
])
res.status(202).end();
});
app.post('/api/v1/gateway_sms', async (req, res) => {
const message = req.body.message;
const sender = req.body.sender;
if (!message) {
return res.status(400).send({ error: 'message is required' });
}
await mg.messages.create(process.env.MAILGUN_DOMAIN, {
from: "Laixer Equipment <[email protected]>",
to: process.env.MAILGUN_RECIPIENT,
subject: "SMS Gateway",
text: `Message from ${sender}\n\n${message}`
});
res.status(202).end();
});
app.post('/api/v1/notify', async (req, res) => {
const topic = req.body.topic;
const message = req.body.message;
if (!topic || !message) {
return res.status(400).json({ error: 'type and message are required' });
}
console.log(`NOTIFY ${topic}: ${message}`);
res.status(202).end();
});
app.get('/api/v1/ip', async (req, res) => {
const remoteAddress = req.ip;
res.json({ ip: remoteAddress });
});
// error handler
app.use(function (err, req, res, next) {
console.error(err.message);
res.status(err.status || 500).end();
});
// catch 404 and forward to error handler
app.use(function (req, res) {
res.status(404).end();
});
module.exports = app;