api route wip
This commit is contained in:
parent
3913d0d2f7
commit
73b43adb3a
@ -3,7 +3,6 @@ const { Sequelize, DataTypes } = require("sequelize");
|
||||
const { sequelize } = require("../mySQL");
|
||||
|
||||
const IoTModel = sequelize.define("iot-data",{
|
||||
// Model attributes are defined here
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
@ -65,7 +64,6 @@ const IoTModel = sequelize.define("iot-data",{
|
||||
},
|
||||
updatedAt: {
|
||||
type: DataTypes.DATE,
|
||||
//appointmentStart: { type: "DATETIME" } // Do this instead
|
||||
allowNull: true,
|
||||
},
|
||||
},
|
||||
|
62
Database/model/apiLog.js
Normal file
62
Database/model/apiLog.js
Normal file
@ -0,0 +1,62 @@
|
||||
"use strict";
|
||||
const { Sequelize, DataTypes } = require("sequelize");
|
||||
const { sequelize } = require("../mySQL");
|
||||
|
||||
|
||||
sequelize.sync();
|
||||
const api_log_Model = sequelize.define("api-logs",{
|
||||
// Model attributes are defined here
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
primaryKey: true,
|
||||
},
|
||||
ip:{
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 45,
|
||||
},
|
||||
time: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 20,
|
||||
},
|
||||
method: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 10,
|
||||
},
|
||||
host: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 45,
|
||||
},
|
||||
statusCode: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 10,
|
||||
},
|
||||
Responsesize: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 10,
|
||||
},
|
||||
referrer: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 45,
|
||||
},
|
||||
userAgent: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
length: 100,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: false,
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
module.exports = { api_log_Model };
|
39
Web-Server/functions/APIDatabase.js
Normal file
39
Web-Server/functions/APIDatabase.js
Normal file
@ -0,0 +1,39 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
|
||||
|
||||
async function getallData() {
|
||||
try {
|
||||
sequelize.sync();
|
||||
const allData = await IoTModel.findAll({
|
||||
attributes: ['id', 'psiData', 'humidityData', 'o3Data', 'no2Data', 'so2Data', 'coData', 'temperatureData', 'windspeedData', 'currentTime', 'regionData' , 'createdAt' , 'updatedAt'],
|
||||
});
|
||||
return allData;
|
||||
}
|
||||
catch(error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function getLatestData() {
|
||||
try {
|
||||
sequelize.sync();
|
||||
const latestData = await IoTModel.findAll({
|
||||
limit: 1,
|
||||
order: [['createdAt', 'DESC']]
|
||||
});
|
||||
return latestData;
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = { getallData , getLatestData };
|
@ -1,10 +1,11 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { api_log_Model } = require("../../Database/model/apiLog.js");
|
||||
|
||||
async function insertData(data) {
|
||||
console.log(data);
|
||||
function insertData(data) {
|
||||
try {
|
||||
const latestData = await IoTModel.create({
|
||||
//const latestData = await IoTModel.create({
|
||||
IoTModel.create({
|
||||
psiData: data.psi,
|
||||
humidityData: data.humidity,
|
||||
o3Data: data.o3,
|
||||
@ -22,7 +23,27 @@ async function insertData(data) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { insertData };
|
||||
function insertLogData(log){
|
||||
try{
|
||||
api_log_Model.create({
|
||||
ip: log.ip,
|
||||
time: log.time,
|
||||
method: log.method,
|
||||
host: log.host,
|
||||
statusCode: log.statusCode,
|
||||
Responsesize: log.Responsesize,
|
||||
referrer: log.referrer,
|
||||
userAgent: log.userAgent,
|
||||
});
|
||||
}
|
||||
catch
|
||||
(error){
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = { insertData , insertLogData };
|
||||
|
||||
|
||||
|
||||
|
@ -38,7 +38,7 @@ client.on('connect', () => {
|
||||
client.on('end', () => {
|
||||
console.log('Disconnected from MQTT broker');
|
||||
client.reconnect = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
@ -1,19 +1,26 @@
|
||||
const { connect } = require("../routes/test");
|
||||
|
||||
const { insertLogData } = require("../functions/Database.js");
|
||||
const APIlogger = (req, res, next) => {
|
||||
/*
|
||||
console.log("API request received");
|
||||
console.log(req.ip);
|
||||
console.log(req.path);
|
||||
console.log(req.method);
|
||||
console.log(res.statusCode);
|
||||
console.log(req.headers['user-agent']);
|
||||
console.log(req.secure);
|
||||
console.log(req.protocol);
|
||||
console.log(req.get('host'));
|
||||
console.log(new Date());
|
||||
*/
|
||||
|
||||
const log = {
|
||||
ip: req.ip,
|
||||
time: new Date().toUTCString(),
|
||||
method: req.method,
|
||||
//https://stackoverflow.com/questions/10183291/how-to-get-the-full-url-in-express
|
||||
host: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
|
||||
statusCode: res.statusCode,
|
||||
Responsesize: res.get('Content-Length') ? res.get('Content-Length') : 0,
|
||||
referrer: res.get('content-type') ? res.get('content-type') : "none",
|
||||
userAgent: req.headers["user-agent"],
|
||||
};
|
||||
//upload to db logic here for api logs
|
||||
insertLogData(log);
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = { APIlogger };
|
||||
|
||||
|
||||
/*
|
||||
method: req.method,
|
||||
statusCode: res.statusCode,
|
||||
protocol: req.protocol,
|
||||
@ -22,14 +29,4 @@ const APIlogger = (req, res, next) => {
|
||||
ip: req.ip,
|
||||
userAgent: req.headers["user-agent"],
|
||||
host: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
|
||||
};
|
||||
console.log(log);
|
||||
|
||||
//upload to db logic here for api logs
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = { APIlogger };
|
||||
|
||||
|
||||
*/
|
@ -21,15 +21,6 @@ app.use('/api/', APIlogger );
|
||||
//route logic
|
||||
app.use('/api/', require('../routes/api_route.js'));
|
||||
|
||||
|
||||
/*
|
||||
const testRoute = require("../routes/test.js")
|
||||
const latestDataroute = require("../routes/latest-Data.js")
|
||||
|
||||
app.use('/test', testRoute);
|
||||
app.use('/api/latest-data', latestDataroute);
|
||||
*/
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`app listening on port ${port}`);
|
||||
});
|
||||
|
@ -19,5 +19,6 @@ const router = require('express').Router();
|
||||
|
||||
router.use('/test' , require('./test'));
|
||||
router.use('/latest-data', require('./latest-data'));
|
||||
router.use('/:month', require('./monthlyData'));
|
||||
|
||||
module.exports = router;
|
@ -1,9 +1,11 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { getLatestData } = require("../functions/APIDatabase.js");
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
// Logic for model and API by 1
|
||||
/*
|
||||
async function getLatestData() {
|
||||
try {
|
||||
sequelize.sync();
|
||||
@ -18,6 +20,7 @@ async function getLatestData() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
|
21
Web-Server/routes/monthlyData.js
Normal file
21
Web-Server/routes/monthlyData.js
Normal file
@ -0,0 +1,21 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { getallData } = require("../functions/APIDatabase.js");
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
//get month from url
|
||||
console.log(req.params.month);
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
|
||||
// Export the router
|
||||
module.exports = router;
|
@ -1,8 +1,11 @@
|
||||
const { sequelize } = require("../../Database/mySql.js");
|
||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||
const { getallData } = require("../functions/APIDatabase.js");
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
/*
|
||||
async function getallData() {
|
||||
try {
|
||||
sequelize.sync();
|
||||
@ -17,6 +20,7 @@ async function getallData() {
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
|
60
schema/api_logs.sql
Normal file
60
schema/api_logs.sql
Normal file
@ -0,0 +1,60 @@
|
||||
CREATE DATABASE IF NOT EXISTS `eco_saver` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
|
||||
USE `eco_saver`;
|
||||
-- MySQL dump 10.13 Distrib 8.0.34, for Win64 (x86_64)
|
||||
--
|
||||
-- Host: mpsqldatabase.mysql.database.azure.com Database: eco_saver
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 8.0.34
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!50503 SET NAMES utf8 */;
|
||||
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
|
||||
/*!40103 SET TIME_ZONE='+00:00' */;
|
||||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
|
||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
||||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
||||
|
||||
--
|
||||
-- Table structure for table `api-logs`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `api-logs`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!50503 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `api-logs` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`ip` varchar(45) NOT NULL,
|
||||
`time` varchar(30) NOT NULL,
|
||||
`method` varchar(10) NOT NULL,
|
||||
`host` varchar(45) NOT NULL,
|
||||
`statusCode` varchar(10) NOT NULL,
|
||||
`responsesize` varchar(10) NOT NULL,
|
||||
`referrer` varchar(45) NOT NULL,
|
||||
`useragent` varchar(150) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Dumping data for table `api-logs`
|
||||
--
|
||||
|
||||
LOCK TABLES `api-logs` WRITE;
|
||||
/*!40000 ALTER TABLE `api-logs` DISABLE KEYS */;
|
||||
INSERT INTO `api-logs` VALUES (1,'::1','Fri, 29 Dec 2023 09:21:29 GMT','GET','http://localhost/api/test','200','0','none','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'),(2,'::1','Fri, 29 Dec 2023 09:21:49 GMT','GET','http://localhost/api/test','200','0','none','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
|
||||
/*!40000 ALTER TABLE `api-logs` ENABLE KEYS */;
|
||||
UNLOCK TABLES;
|
||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||
|
||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
|
||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2023-12-29 17:25:40
|
Loading…
x
Reference in New Issue
Block a user