Update dependencies and fix file paths

This commit is contained in:
newtbot
2024-01-24 04:23:54 +08:00
parent 7e9b6af0ba
commit 4aea6a8e4c
16 changed files with 405 additions and 196 deletions

109
webserver/bin/www Normal file
View File

@ -0,0 +1,109 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../../webserver/modules/express.js');
const index = require("../index.js")
var debug = require('debug')('proxy-api:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || '80');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
const io = require('socket.io')(server
, {
cors: {
//replace with domain name when deployed
origin: 'http://localhost:3000', // client!
methods: ["GET"],
allowedHeaders: ["my-custom-header"],
credentials: true
}
});
app.io = io;
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
for(let listener of app.onListen){
listener()
}
}

View File

@ -23,11 +23,15 @@ async function insertLogData(log) {
async function insertDatatoDB(data) {
try {
const app = require("../modules/express.js");
sensorDataModel.create({
sensorid: data.sensorid,
locationid: data.locationid,
measurement: data.measurement,
});
//ws broadcast event except to the sender.
app.io.emit("sensorData:new", data);
} catch (error) {
console.error(error);
}
@ -37,15 +41,15 @@ async function checkAPikey(SuppliedKey, rowid) {
try {
const retrivedKey = await apikeyModel.findOne({
raw: true,
attributes: ["apikey" , "permission"],
attributes: ["apikey", "permission"],
where: {
userid: rowid,
id: rowid,
},
});
//console.log(retrivedKey.apikey);
if (compareAPIKey(SuppliedKey, retrivedKey.apikey)) {
//return true;
return retrivedKey.permission;
return retrivedKey.permission;
}
} catch (error) {
console.error(error);

View File

@ -1,4 +1,3 @@
const { app } = require("./modules/express.js");
const client = require("./modules/mqtt");
const { isJson, isNumber } = require("./functions/validateData.js");
const { insertDatatoDB } = require("./functions/database.js");

View File

@ -1,13 +1,24 @@
const express = require("express");
const helmet = require("helmet");
//const helmet = require("helmet");
var cors = require('cors');
const { rateLimit } = require("express-rate-limit");
const { APIlogger } = require('../middleware/apiLogger.js');
const { apikeyCheck } = require('../middleware/apiKey.js');
const app = express();
app.use(helmet());
//app.use(cors());
//allow cors from localhost:3000
//app.use(helmet({ crossOriginResourcePolicy: { policy: "cross-origin" } }));
const port = 80;
// Hold list of functions to run when the server is ready
app.onListen = [function(){console.log('hello')}];
module.exports = app;
//handle cors
//express-rate-limit stolen from docs
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
@ -73,8 +84,8 @@ app.use(function(err, req, res, next) {
keyErrors,
});
});
/*
app.listen(port, () => {
console.log(`app listening on port ${port}`);
});
module.exports = { app };
*/

View File

@ -10,8 +10,8 @@ const options = {
username: process.env.MQTT_USER,
password: process.env.MQTT_PASS,
protocol: 'mqtts', // Use MQTT over TLS
key: fs.readFileSync(path.resolve(__dirname, '../../cert/privkey.pem')),
cert: fs.readFileSync(path.resolve(__dirname, '../../cert/cert.pem')),
key: fs.readFileSync(path.resolve(__dirname, '../cert/privkey.pem')),
cert: fs.readFileSync(path.resolve(__dirname, '../cert/cert.pem')),
};