Changed config to env variables
This commit is contained in:
parent
fe1c8c4c15
commit
4f74804910
@ -1,12 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const path = require("path");
|
||||
|
||||
const pathToFile = path.join(__dirname, "sampleConfig.js")
|
||||
const pathToNewDestination = path.join(__dirname, "config", "config.js")
|
||||
|
||||
try {
|
||||
fs.copyFileSync(pathToFile, pathToNewDestination)
|
||||
console.log("Successfully copied and moved config file!")
|
||||
} catch(err) {
|
||||
throw err
|
||||
}
|
82
own_modules/configManager.mjs
Normal file
82
own_modules/configManager.mjs
Normal file
@ -0,0 +1,82 @@
|
||||
export default class ConfigManager{
|
||||
|
||||
constructor(){}
|
||||
|
||||
init(){
|
||||
console.log("[CONFIG] Reading config environment variables.")
|
||||
|
||||
// Initialize mqtt url
|
||||
var mqttUrlEnv = process.env.MQTT_URL;
|
||||
if(mqttUrlEnv){
|
||||
this.mqttUrl = mqttUrlEnv;
|
||||
console.log("[CONFIG] MQTT_URL set to ["+mqttUrlEnv+"].")
|
||||
}else{
|
||||
this.mqttUrl = "mqtt://localhost:1883"
|
||||
console.log("[CONFIG] MQTT_URL set to default [mqtt://localhost].")
|
||||
}
|
||||
|
||||
// Initialize auth token
|
||||
var mqttAuthTokenEnv = process.env.MQTT_AUTH_TOKEN
|
||||
if(mqttAuthTokenEnv){
|
||||
this.mqttAuthToken = mqttAuthTokenEnv;
|
||||
console.log("[CONFIG] MQTT_AUTH_TOKEN set to ["+mqttAuthTokenEnv+"].")
|
||||
}else{
|
||||
this.mqttAuthToken = "skl20g67bm1";
|
||||
console.log("[CONFIG] MQTT_AUTH_TOKEN set to default value [skl20g67bm1].");
|
||||
}
|
||||
|
||||
// Initialize mqtt options
|
||||
this.mqttOptions = {
|
||||
keepalive: 60,
|
||||
reconnectPeriod: 1000,
|
||||
protocolVersion: 3,
|
||||
protocolId: 'MQIsdp',
|
||||
clean: true,
|
||||
encoding: 'utf8'
|
||||
}
|
||||
|
||||
// Initialize mqtt clientId
|
||||
var mqttClientIdEnv = process.env.MQTT_CLIENTID
|
||||
if(mqttClientIdEnv){
|
||||
this.mqttOptions.clientId = mqttClientIdEnv
|
||||
console.log("[CONFIG] MQTT_CLIENTID read and set to ["+mqttClientIdEnv+"].")
|
||||
}else{
|
||||
this.mqttOptions.clientId = "advancedMqttBridge"
|
||||
console.log("[CONFIG] MQTT_CLIENTID set to default [advancedMqttBridge].")
|
||||
}
|
||||
|
||||
// Initialize mqtt username
|
||||
var mqttUsernameEnv = process.env.MQTT_USERNAME
|
||||
if(mqttUsernameEnv){
|
||||
this.mqttOptions.username = mqttUsernameEnv
|
||||
console.log("[CONFIG] MQTT_USERNAME read and set to ["+mqttUsernameEnv+"].")
|
||||
}else{
|
||||
this.mqttOptions.username = "advancedMqttBridge"
|
||||
console.log("[CONFIG] MQTT_USERNAME set to default [advancedMqttBridge].")
|
||||
}
|
||||
|
||||
// Initialize mqtt password
|
||||
var mqttPasswordEnv = process.env.MQTT_PASSWORD
|
||||
if(mqttPasswordEnv){
|
||||
this.mqttOptions.password = mqttPasswordEnv
|
||||
console.log("[CONFIG] MQTT_PASSWORD read and set to ["+mqttPasswordEnv+"].")
|
||||
}else{
|
||||
this.mqttOptions.password = "mqttPassword#123"
|
||||
console.log("[CONFIG] MQTT_PASSWORD set to default [mqttPassword#123].")
|
||||
}
|
||||
}
|
||||
|
||||
getMqttAuthToken(){
|
||||
return this.mqttAuthToken;
|
||||
}
|
||||
|
||||
getMqttUrl(){
|
||||
return this.mqttUrl;
|
||||
}
|
||||
|
||||
getMqttOptions(){
|
||||
return this.mqttOptions;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,15 @@
|
||||
import mqtt from 'mqtt'
|
||||
import config from '../config/config.js'
|
||||
import request from 'request';
|
||||
|
||||
export default class MqttManager{
|
||||
|
||||
constructor(){}
|
||||
|
||||
init(){
|
||||
init(mqttUrl, mqttOptions){
|
||||
this.messageHandlers = []
|
||||
|
||||
this.client = mqtt.connect(config.mqtt.url, config.mqtt.options)
|
||||
console.log("[MQTT] Connected to MQTT broker ["+config.mqtt.url+"].");
|
||||
this.client = mqtt.connect(mqttUrl, mqttOptions)
|
||||
console.log("[MQTT] Connected to MQTT broker ["+mqttUrl+"].");
|
||||
this.ready = true
|
||||
|
||||
this.client.on('message', (topic, message) => {
|
||||
|
@ -1,7 +1,6 @@
|
||||
import sqlite3 from'sqlite3'
|
||||
import { open } from 'sqlite'
|
||||
|
||||
|
||||
/**
|
||||
* Manager for handling the sqlite database
|
||||
*/
|
||||
@ -13,14 +12,18 @@ export default class SqliteManager{
|
||||
sqlite3.verbose()
|
||||
await open({filename: './database/loginbase.db', driver: sqlite3.Database}).then(
|
||||
(db) => {
|
||||
console.log('[SQLITE] Connected to the loginbase database.');
|
||||
db.run("CREATE TABLE IF NOT EXISTS users (email TEXT PRIMARY KEY, password TEXT)");
|
||||
console.log('[SQLITE] Created users table if it didnt exist.');
|
||||
db.run("CREATE TABLE IF NOT EXISTS handlers (topic TEXT, message TEXT, requestUrl TEXT)");
|
||||
console.log('[SQLITE] Created handlers table if it didnt exist.');
|
||||
this.database = db
|
||||
this.ready = true
|
||||
})
|
||||
console.log('[SQLITE] Connected to the loginbase database.');
|
||||
}
|
||||
)
|
||||
await this.database.run("CREATE TABLE IF NOT EXISTS users (email TEXT PRIMARY KEY, password TEXT)");
|
||||
console.log('[SQLITE] Created users table if it didnt exist.');
|
||||
await this.database.run("CREATE TABLE IF NOT EXISTS handlers (topic TEXT, message TEXT, requestUrl TEXT)");
|
||||
console.log('[SQLITE] Created handlers table if it didnt exist.');
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
async getUser(email){
|
||||
|
1
package-lock.json
generated
1
package-lock.json
generated
@ -5,6 +5,7 @@
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "loginbase",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
|
@ -13,8 +13,7 @@
|
||||
"description": "Login base project",
|
||||
"main": "server.mjs",
|
||||
"scripts": {
|
||||
"start": "node server.mjs",
|
||||
"create_config": "node createConfig.js"
|
||||
"start": "node server.mjs"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -1,21 +0,0 @@
|
||||
var config = {}
|
||||
|
||||
config.mqtt = {}
|
||||
config.sqlite = {}
|
||||
|
||||
config.mqtt.options = {
|
||||
clientId: 'mqttclientid',
|
||||
username: 'mqttusername',
|
||||
password: 'mqttpassword',
|
||||
keepalive: 60,
|
||||
reconnectPeriod: 1000,
|
||||
protocolVersion: 3,
|
||||
protocolId: 'MQIsdp',
|
||||
clean: true,
|
||||
encoding: 'utf8'
|
||||
};
|
||||
config.mqtt.url = 'mqtt://MQTTURL'
|
||||
|
||||
config.auth = "random auth token"
|
||||
|
||||
module.exports = config
|
10
server.mjs
10
server.mjs
@ -6,9 +6,9 @@ import path from 'path'
|
||||
import { dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import crypto from "crypto";
|
||||
import config from './config/config.js'
|
||||
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()
|
||||
@ -23,13 +23,17 @@ app.use(session({
|
||||
// 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();
|
||||
mqttManager.init(configManager.getMqttUrl(), configManager.getMqttOptions());
|
||||
|
||||
// Update mqtt handlers from sqlite
|
||||
initMqttHandlers();
|
||||
@ -103,7 +107,7 @@ app.get('/mqttbridge', (req, res)=>{
|
||||
var message = req.query.message;
|
||||
if(auth == undefined || topic == undefined || message == undefined){
|
||||
res.sendStatus(400);
|
||||
}else if(auth != config.auth){
|
||||
}else if(auth != configManager.getMqttAuthToken()){
|
||||
res.sendStatus(401);
|
||||
}else{
|
||||
mqttManager.publishMessage(topic, message)
|
||||
|
Loading…
Reference in New Issue
Block a user