2024-08-27 08:37:01 +08:00
|
|
|
import express from "express";
|
|
|
|
import bodyParser from "body-parser";
|
|
|
|
import mongoose from "mongoose";
|
|
|
|
import cors from "cors";
|
|
|
|
import dotenv from "dotenv";
|
|
|
|
import multer from "multer";
|
|
|
|
import helmet from "helmet";
|
|
|
|
import morgan from "morgan";
|
|
|
|
import path from "path";
|
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
import authRoutes from "./routes/auth.js";
|
|
|
|
import userRoutes from "./routes/users.js";
|
2024-08-27 17:49:07 +08:00
|
|
|
import collectionRoutes from "./routes/collection.js";
|
|
|
|
import endpointRoutes from "./routes/endpoint.js";
|
2024-08-27 08:37:01 +08:00
|
|
|
|
|
|
|
// import webemailRoutes from "./routes/webemail.js";
|
|
|
|
import axios from "axios";
|
|
|
|
import WebSocket, { WebSocketServer } from "ws";
|
|
|
|
|
|
|
|
// import nodemailer from "nodemailer";
|
|
|
|
// export const ws = new WebSocket("ws://192.168.50.15:4028/sT1eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY2NDE3OTI4MmE3M2ZlZWQ3MjgyM2ViOCIsImlhdCI6MTcxNTY2ODI4Nn0.ziEOLreXbCJRlyjRyIVLDsJNpeIvk73rf3kU7_HtO8E"
|
|
|
|
// );
|
|
|
|
// ws.onopen = () => {
|
|
|
|
// console.log("WebSocket connected------------------------------------------");
|
|
|
|
|
|
|
|
// // You can send initial messages after the connection is established if needed
|
|
|
|
// // ws.send("Hello, server!");
|
|
|
|
// // const userId = "meeeee2";
|
|
|
|
// ws.send(JSON.stringify({ type: "join", userId: "obnPay_test" }));
|
|
|
|
|
|
|
|
// // ws.send(encryptedMessage2);
|
|
|
|
// };
|
|
|
|
// ws.onclose = () => {
|
|
|
|
// console.log("Connection closed");
|
|
|
|
// };
|
|
|
|
/* CONFIGURATIONS */
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(helmet());
|
|
|
|
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
|
|
|
|
app.use(morgan("common"));
|
|
|
|
app.use(bodyParser.json({ limit: "30mb", extended: true }));
|
|
|
|
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
|
|
|
|
app.use(cors());
|
|
|
|
app.use("/assets", express.static(path.join(__dirname, "upload_files")));
|
|
|
|
|
|
|
|
/* FILE STORAGE */
|
|
|
|
const storage = multer.diskStorage({
|
|
|
|
destination: (req, file, cb) => {
|
|
|
|
const category = req.body.category;
|
|
|
|
// if (category == "message") {
|
|
|
|
// cb(null, "message_uploads");
|
|
|
|
// } else if (category == "user") {
|
|
|
|
// cb(null, "user_uploads");
|
|
|
|
// } else {
|
|
|
|
cb(null, "upload_files");
|
|
|
|
// }
|
|
|
|
},
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
const image_id = req.body.image_id;
|
|
|
|
cb(null, image_id + "-" + file.originalname);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const upload = multer({ storage });
|
|
|
|
|
|
|
|
/* ROUTES */
|
|
|
|
app.use("/api/auth", authRoutes);
|
|
|
|
app.use("/api/users", userRoutes);
|
2024-08-27 17:49:07 +08:00
|
|
|
app.use("/api/collections", collectionRoutes);
|
|
|
|
app.use("/api/endpoints", endpointRoutes);
|
2024-08-27 08:37:01 +08:00
|
|
|
|
|
|
|
// app.use("/api/web-emails", webemailRoutes);
|
|
|
|
app.post("/api/upload_images", upload.single("image"), async (req, res) => {
|
|
|
|
try {
|
|
|
|
const { filename } = req.file;
|
|
|
|
|
|
|
|
const { image_id, category } = req.body;
|
|
|
|
|
|
|
|
res.status(201).send({ filename, category, image_id });
|
|
|
|
} catch (error) {
|
|
|
|
res.status(400).send(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
function sendEmail(req, res, next) {
|
|
|
|
const apiKey =
|
|
|
|
"ODA4MDc4ZThjMDA4NjVhYzU4MTcyNDJjNTMxY2JlZGU6MGQ4ODg3ZTdiZjY1ZWNkMmQ0NzdiOWJhZGIyYTJhY2Q="; // Replace with your Mailjet API key
|
|
|
|
const apiUrl = "https://api.mailjet.com/v3.1/send";
|
|
|
|
|
|
|
|
// const otp = generateOTP(6); // You should have a function to generate the OTP
|
|
|
|
const email2 = "kramblooda@gmail.com";
|
|
|
|
const min = 100000; // Minimum 6-digit number
|
|
|
|
const max = 999999; // Maximum 6-digit number
|
|
|
|
const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
|
|
const requestData = {
|
|
|
|
Messages: [
|
|
|
|
{
|
|
|
|
From: {
|
|
|
|
Email: "webdev@obanana.com",
|
|
|
|
Name: "Obanana B2B",
|
|
|
|
},
|
|
|
|
To: [
|
|
|
|
{
|
|
|
|
Email: req.body.email,
|
|
|
|
Name: "Subscriber",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Subject: "Obanana OTP",
|
|
|
|
TextPart: "Greetings from Obanana!",
|
|
|
|
HTMLPart: req.body.html,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Basic ${apiKey}`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(apiUrl, requestData, config)
|
|
|
|
.then((response) => {
|
|
|
|
// const status = response.data.Messages[0].Status;
|
|
|
|
// console.log(response.data.Messages[0].Status);
|
|
|
|
// console.log(randomNumber);
|
|
|
|
// setotpSent(randomNumber);
|
|
|
|
res.status(200).json(response.data);
|
|
|
|
|
|
|
|
// return `${status},${randomNumber}`;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
res.status(404).json({ message: error });
|
|
|
|
|
|
|
|
// console.error("Error sending OTP email:", error);
|
|
|
|
// Handle the error here
|
|
|
|
});
|
|
|
|
}
|
|
|
|
app.post("/api/send-email/", sendEmail);
|
|
|
|
/* MONGOOSE SETUP */
|
|
|
|
const PORT = process.env.PORT || 3000;
|
2024-08-27 17:49:07 +08:00
|
|
|
// mongoose
|
|
|
|
// .connect(process.env.MONGO_URL, {
|
|
|
|
// useNewUrlParser: true,
|
|
|
|
// useUnifiedTopology: true,
|
|
|
|
// })
|
|
|
|
// .then(() => {
|
|
|
|
// app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
|
|
|
|
|
|
|
|
// // /* ADD DATA ONE TIME */
|
|
|
|
// })
|
|
|
|
// .catch((error) => console.log(`${error} did not connect`));
|
|
|
|
|
2024-08-27 08:37:01 +08:00
|
|
|
mongoose
|
2024-08-27 17:49:07 +08:00
|
|
|
.connect(process.env.MONGO_URL)
|
2024-08-27 08:37:01 +08:00
|
|
|
.then(() => {
|
|
|
|
app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
|
|
|
|
|
|
|
|
// /* ADD DATA ONE TIME */
|
|
|
|
})
|
|
|
|
.catch((error) => console.log(`${error} did not connect`));
|