added service api
This commit is contained in:
commit
d59e70a1a8
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules
|
||||||
|
.env
|
|
@ -0,0 +1,43 @@
|
||||||
|
const express = require("express");
|
||||||
|
const session = require("express-session");
|
||||||
|
const mdb = require("./config/mongodb");
|
||||||
|
const mongoStore = require("connect-mongo");
|
||||||
|
const keys = require("./config/keys");
|
||||||
|
const app = express();
|
||||||
|
const axios = require("axios");
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
session({
|
||||||
|
secret: keys.sessionSecret,
|
||||||
|
resave: false,
|
||||||
|
saveUninitialized: false,
|
||||||
|
cookie: {
|
||||||
|
maxAge: 1000 * 60 * 60 * 24 * 7,
|
||||||
|
},
|
||||||
|
store: mongoStore.create({
|
||||||
|
clientPromise: mdb,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Self pinger as status updater
|
||||||
|
setInterval(() => {
|
||||||
|
const config = {
|
||||||
|
url: `http://localhost:${process.env.PORT}/status/ping/${keys.pingusername}`,
|
||||||
|
}
|
||||||
|
axios(config)
|
||||||
|
.then(function (response) {
|
||||||
|
console.log("Self Ping Completed");
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}, 120000);
|
||||||
|
|
||||||
|
app.use("/", require("./routes"));
|
||||||
|
const PORT = process.env.PORT || 5000;
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log("API listening on port "+ PORT + "!");
|
||||||
|
});
|
|
@ -0,0 +1,9 @@
|
||||||
|
require("dotenv").config();
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mongoURI: process.env.MONGO_URI,
|
||||||
|
sessionSecret: process.env.SESSION_SECRET,
|
||||||
|
discordId: process.env.DISCORD_ID,
|
||||||
|
pingAuthKey: process.env.PING_REG_AUTH_KEY,
|
||||||
|
pingusername: process.env.USERNAME_TO_PING,
|
||||||
|
};
|
|
@ -0,0 +1,14 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const keys = require("./keys");
|
||||||
|
|
||||||
|
const db = mongoose
|
||||||
|
.connect(keys.mongoURI)
|
||||||
|
.then(() => {
|
||||||
|
console.log("Connected to MongoDB");
|
||||||
|
return mongoose.connection.getClient();
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = db;
|
|
@ -0,0 +1,8 @@
|
||||||
|
const apiRoot = require("../extras/root.json");
|
||||||
|
|
||||||
|
module.exports.home = async (req, res, next) => {
|
||||||
|
res.json({
|
||||||
|
message: "Welcome to the I-AM-Online Server API!",
|
||||||
|
apiRoot: apiRoot,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,116 @@
|
||||||
|
const keys = require("../config/keys");
|
||||||
|
const UserStatus = require("../models/userStatus");
|
||||||
|
const {
|
||||||
|
format
|
||||||
|
} = require("date-format-parse")
|
||||||
|
const {
|
||||||
|
customAlphabet
|
||||||
|
} = require("nanoid");
|
||||||
|
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 10);
|
||||||
|
|
||||||
|
// Not used
|
||||||
|
// module.exports.getUserStatus = async (req, res, next) => {
|
||||||
|
// const userStatus = await UserStatus.findOne({
|
||||||
|
// username: req.params.username
|
||||||
|
// });
|
||||||
|
// res.json({
|
||||||
|
// userStatus
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
module.exports.checkStatus = async (req, res, next) => {
|
||||||
|
const userStatus = await UserStatus.findOne({
|
||||||
|
username: req.params.username
|
||||||
|
});
|
||||||
|
var lastPingTs = (format(userStatus.lastSeen, "x"));
|
||||||
|
var currentTs = (format(new Date(), "x"));
|
||||||
|
// var diff = currentTs - lastPingTs;
|
||||||
|
// console.log(diff/60000);
|
||||||
|
if (currentTs - lastPingTs > 240000) {
|
||||||
|
const updateUserStatusUpdt = await UserStatus.findOneAndUpdate({
|
||||||
|
username: req.params.username
|
||||||
|
}, {
|
||||||
|
amIOnline: false,
|
||||||
|
status: {
|
||||||
|
type: "offline",
|
||||||
|
updatedBy: "i-am-online-server",
|
||||||
|
via: "web",
|
||||||
|
heartbeat: {
|
||||||
|
key: null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lastSeen: userStatus.lastSeen
|
||||||
|
});
|
||||||
|
res.json({
|
||||||
|
updateUserStatusUpdt
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const updateUserStatusUpdt = await UserStatus.findOneAndUpdate({
|
||||||
|
username: req.params.username
|
||||||
|
}, {
|
||||||
|
amIOnline: true,
|
||||||
|
status: {
|
||||||
|
type: "online",
|
||||||
|
updatedBy: "i-am-online-client",
|
||||||
|
via: "desktop",
|
||||||
|
heartbeat: {
|
||||||
|
key: "self"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lastSeen: userStatus.lastSeen
|
||||||
|
});
|
||||||
|
res.json({
|
||||||
|
updateUserStatusUpdt
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.reportUserStatus = async (req, res, next) => {
|
||||||
|
if(req.query.auth != keys.pingAuthKey){
|
||||||
|
return res.json({
|
||||||
|
error: "Invalid auth key"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const checkIfUserExists = await UserStatus.findOne({
|
||||||
|
username: req.params.username
|
||||||
|
});
|
||||||
|
if (checkIfUserExists) {
|
||||||
|
const updateUserStatus = await UserStatus.findOneAndUpdate({
|
||||||
|
username: req.params.username
|
||||||
|
}, {
|
||||||
|
status: {
|
||||||
|
type: "online",
|
||||||
|
updatedBy: "i-am-online-client",
|
||||||
|
via: "desktop",
|
||||||
|
heartbeat: {
|
||||||
|
key: nanoid(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
amIOnline: true,
|
||||||
|
lastSeen: new Date()
|
||||||
|
}, {
|
||||||
|
new: true
|
||||||
|
});
|
||||||
|
res.json({
|
||||||
|
updateUserStatus
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const userStatus = new UserStatus({
|
||||||
|
username: req.params.username,
|
||||||
|
status: {
|
||||||
|
type: "online",
|
||||||
|
via: "desktop",
|
||||||
|
updatedBy: "i-am-online-client",
|
||||||
|
heartbeat: {
|
||||||
|
key: nanoid(),
|
||||||
|
},
|
||||||
|
amIOnline: true,
|
||||||
|
lastSeen: new Date()
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await userStatus.save();
|
||||||
|
res.json({
|
||||||
|
userStatus
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"name": "i-am-online",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "An API for the i-am-online service"
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const userStatusSchema = mongoose.Schema({
|
||||||
|
username: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
type: {
|
||||||
|
type: String,
|
||||||
|
enum: ['online', 'offline', 'busy', 'away'],
|
||||||
|
},
|
||||||
|
via: {
|
||||||
|
type: String,
|
||||||
|
enum: ['web', 'mobile', 'desktop'],
|
||||||
|
},
|
||||||
|
upadtedBy: {
|
||||||
|
type: String,
|
||||||
|
enum: ['discord', 'i-am-online-client'],
|
||||||
|
},
|
||||||
|
heartbeat: {
|
||||||
|
key: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lastSeen:{
|
||||||
|
type: Date,
|
||||||
|
},
|
||||||
|
amIOnline : {
|
||||||
|
type: Boolean,
|
||||||
|
}
|
||||||
|
},{
|
||||||
|
timestamps: true
|
||||||
|
});
|
||||||
|
|
||||||
|
const UserStatus = mongoose.model('UserStatus', userStatusSchema);
|
||||||
|
|
||||||
|
module.exports = UserStatus;
|
|
@ -0,0 +1,31 @@
|
||||||
|
{
|
||||||
|
"name": "i-am-online-server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "app.js",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nodemon app.js",
|
||||||
|
"start": "node app.js"
|
||||||
|
},
|
||||||
|
"git": {
|
||||||
|
"url": "https://github.com/BRAVO68WEB/i-am-online"
|
||||||
|
},
|
||||||
|
"author": "BRAVO68WEB",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^0.26.1",
|
||||||
|
"chalk": "^4.1.0",
|
||||||
|
"connect-mongo": "^4.6.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"date-format-parse": "^0.2.7",
|
||||||
|
"dotenv": "^16.0.0",
|
||||||
|
"express": "^4.17.3",
|
||||||
|
"express-session": "^1.17.2",
|
||||||
|
"helmet": "^5.0.2",
|
||||||
|
"mongoose": "^6.3.0",
|
||||||
|
"nanoid": "^3.3.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.16"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
const controllers = require("../controllers/home");
|
||||||
|
|
||||||
|
router.get("/", controllers.home);
|
||||||
|
|
||||||
|
module.exports = router;
|
|
@ -0,0 +1,7 @@
|
||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.use("/", require("./home"));
|
||||||
|
router.use("/status", require("./status"));
|
||||||
|
|
||||||
|
module.exports = router;
|
|
@ -0,0 +1,8 @@
|
||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
const controllers = require("../controllers/status");
|
||||||
|
|
||||||
|
router.get("/heartbeat/:username", controllers.reportUserStatus);
|
||||||
|
router.get("/ping/:username", controllers.checkStatus);
|
||||||
|
|
||||||
|
module.exports = router;
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue