151 lines
4.1 KiB
JavaScript
151 lines
4.1 KiB
JavaScript
// Libraries
|
|
import express from 'express'
|
|
import bcrypt from 'bcrypt'
|
|
import session from 'express-session'
|
|
import path from 'path'
|
|
import { dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import crypto from "crypto";
|
|
import SqliteManager from "./own_modules/sqliteManager.mjs"
|
|
import MqttManager from "./own_modules/mqttManager.mjs"
|
|
import ConfigManager from "./own_modules/configManager.mjs"
|
|
|
|
// Setup express
|
|
const app = express()
|
|
app.use(express.json())
|
|
var randomSecret = crypto.randomBytes(20).toString('hex');
|
|
app.use(session({
|
|
secret: randomSecret,
|
|
resave: true,
|
|
saveUninitialized: true
|
|
}));
|
|
|
|
// Setup path
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
// Setup config manager
|
|
var configManager = new ConfigManager();
|
|
configManager.init();
|
|
|
|
// Setup database manager
|
|
var databaseManager = new SqliteManager();
|
|
await databaseManager.init();
|
|
|
|
// Setup mqtt manager
|
|
var mqttManager = new MqttManager();
|
|
mqttManager.init(configManager.getMqttUrl(), configManager.getMqttOptions());
|
|
|
|
// Update mqtt handlers from sqlite
|
|
initMqttHandlers();
|
|
|
|
// Register route
|
|
app.post('/register', async (req, res) => {
|
|
var password = req.body.password
|
|
var email = req.body.email
|
|
|
|
if(password && email){
|
|
const hashedPassword = await bcrypt.hash(req.body.password, 10);
|
|
let inserted = await databaseManager.addUser(email, hashedPassword)
|
|
if(inserted){
|
|
req.session.loggedin = true
|
|
req.session.email = email
|
|
res.sendStatus(201)
|
|
}else{
|
|
res.sendStatus(400)
|
|
}
|
|
}else{
|
|
res.sendStatus(400)
|
|
}
|
|
})
|
|
|
|
// Default GET route, redirects to login when not logged in or to the homepage when logged in
|
|
app.get('/', function(req, res) {
|
|
if(req.session.loggedin){
|
|
res.redirect("/home")
|
|
}else{
|
|
res.redirect("/login")
|
|
}
|
|
});
|
|
|
|
// Login GET route
|
|
app.get('/login', function(request, response) {
|
|
response.sendFile(path.join(__dirname + '/public/html/login.html'));
|
|
});
|
|
|
|
// Login POST route
|
|
app.post('/login', async (req,res) => {
|
|
const user = await databaseManager.getUser(req.body.email);
|
|
|
|
if(user == null){
|
|
return res.sendStatus(400)
|
|
}
|
|
try{
|
|
if(await bcrypt.compare(req.body.password, user.password)){
|
|
req.session.loggedin = true
|
|
req.session.email = user.email
|
|
res.sendStatus(200);
|
|
}else{
|
|
res.sendStatus(401)
|
|
}
|
|
}catch{
|
|
res.status(500).send()
|
|
}
|
|
})
|
|
|
|
// Home GET route
|
|
app.get('/home', function(request, response) {
|
|
if (request.session.loggedin) {
|
|
response.sendFile(path.join(__dirname + '/public/html/home.html'));
|
|
} else {
|
|
response.redirect('/login');
|
|
}
|
|
});
|
|
|
|
app.get('/mqttbridge', (req, res)=>{
|
|
var auth = req.query.auth;
|
|
var topic = req.query.topic;
|
|
var message = req.query.message;
|
|
if(auth == undefined || topic == undefined || message == undefined){
|
|
res.sendStatus(400);
|
|
}else if(auth != configManager.getMqttAuthToken()){
|
|
res.sendStatus(401);
|
|
}else{
|
|
mqttManager.publishMessage(topic, message)
|
|
res.sendStatus(200);
|
|
}
|
|
})
|
|
|
|
app.get('/handlers', async (req, res)=>{
|
|
var answer = await databaseManager.getHandlers();
|
|
res.json(answer);
|
|
})
|
|
|
|
app.post('/handlers/remove', async (req,res) =>{
|
|
let topic = req.body.topic;
|
|
let message = req.body.message;
|
|
let requestUrl = req.body.requestUrl;
|
|
|
|
await databaseManager.removeHandler(topic, message, requestUrl)
|
|
res.sendStatus(200)
|
|
mqttManager.removeMessageHandler({topic: topic, message: message, requestUrl: requestUrl})
|
|
})
|
|
|
|
app.post('/handlers/add', async (req,res) => {
|
|
let topic = req.body.topic;
|
|
let message = req.body.message;
|
|
let requestUrl = req.body.requestUrl;
|
|
|
|
await databaseManager.addHandler(topic, message, requestUrl)
|
|
res.sendStatus(200)
|
|
mqttManager.addMessageHandler({topic: topic, message: message, requestUrl: requestUrl})
|
|
})
|
|
|
|
async function initMqttHandlers(){
|
|
console.log("[MAIN] Initializing mqtt handlers from database.")
|
|
let handlers = await databaseManager.getHandlers();
|
|
handlers.forEach(handler => {
|
|
mqttManager.addMessageHandler(handler)
|
|
});
|
|
}
|
|
|
|
app.listen(3000) |