mirror of
https://github.com/DerTyp7/time-tracking.git
synced 2025-10-30 21:07:11 +01:00
table and entries
This commit is contained in:
34
backend/handlers/mysql_handler.js
Normal file
34
backend/handlers/mysql_handler.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Handles the connection to the MySQL database
|
||||
* - Connects to the database
|
||||
*/
|
||||
|
||||
// Dependencies
|
||||
const mysql = require("mysql");
|
||||
const configFile = require("../config");
|
||||
const logger = require("../logger");
|
||||
|
||||
// Global variables
|
||||
const config = configFile.config;
|
||||
|
||||
// Connection to the database
|
||||
const con = mysql.createConnection({
|
||||
host: config.db.host,
|
||||
user: config.db.user,
|
||||
password: config.db.password,
|
||||
database: config.db.database,
|
||||
});
|
||||
|
||||
con.connect(function (err) {
|
||||
if (err) throw err;
|
||||
logger.info(
|
||||
`Connected to MySQL`,
|
||||
`${config.db.user}@${config.db.host}/${config.db.database}`
|
||||
);
|
||||
});
|
||||
|
||||
con.on("error", () => {
|
||||
logger.error("MySQL Error");
|
||||
});
|
||||
|
||||
module.exports = { con };
|
||||
25
backend/handlers/request_handler.js
Normal file
25
backend/handlers/request_handler.js
Normal file
@@ -0,0 +1,25 @@
|
||||
// Handlers which can be included into the request handler chain.
|
||||
|
||||
// Dependencies
|
||||
const logger = require("../logger");
|
||||
|
||||
// better logging of requests
|
||||
function LoggerHandler(req, res, next) {
|
||||
const ip = (req.headers["x-forwarded-for"] || req.socket.remoteAddress || "")
|
||||
.split(",")[0]
|
||||
.trim()
|
||||
.split(":")[3]; // get the ip of the client
|
||||
|
||||
if (req.method == "GET") {
|
||||
// If the request is a GET request
|
||||
logger.get(req.originalUrl, ip); // Log the request
|
||||
} else if (req.method == "POST") {
|
||||
// If the request is a POST request
|
||||
logger.post(req.originalUrl, ip); // Log the request
|
||||
}
|
||||
next(); // Continue to the next handler
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LoggerHandler,
|
||||
};
|
||||
Reference in New Issue
Block a user