-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4466d92
commit 9bd61bd
Showing
10 changed files
with
97 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.env | ||
config.json | ||
|
||
# Logs | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const visitsBadgeSchema = require('./schema.js'); | ||
|
||
module.exports.visitsBadge = async function (uniqueID) { | ||
const badge = await visitsBadgeSchema.findOneAndUpdate( | ||
{ uniqueID: uniqueID }, | ||
{ | ||
$setOnInsert: { | ||
uniqueID: uniqueID, | ||
}, | ||
$inc: { visitsCount: 1 }, | ||
}, | ||
{ new: true, upsert: true } | ||
).select({ visitsCount: 1 }); | ||
return badge.visitsCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
module.exports = mongoose.model("visitsBadge", new mongoose.Schema({ | ||
uniqueID: String, | ||
visitsCount: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,49 @@ | ||
const express = require("express"); | ||
const cors = require("cors"); | ||
const Visits = require("./mongodb"); | ||
const svgBadge = require("./frontend/src/svgBadge"); | ||
const { port, mongourl } = require('./config.json'); | ||
|
||
const port = process.env.PORT || 3000; | ||
|
||
// Create an express instance and setup middlewares | ||
const app = express(); | ||
app.use(express.json()); | ||
app.use(cors()); | ||
app.use(express.static(__dirname + '/frontend/build')); | ||
|
||
// Initilize mongoDB connection | ||
const mongoose = require('mongoose'); | ||
const database = require('./database/mongo.js'); | ||
mongoose.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }) | ||
.then(() => console.log('Connected to MongoDB.')) | ||
.catch((err) => console.log('Unable to connect to MongoDB.\nError: ' + err)); | ||
|
||
// Disable caching | ||
app.use(function (req, res, next) { | ||
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); | ||
res.header('Cache-Control', 'private, no-cache, no-store, must-revaluniqueIDate'); | ||
res.header('Expires', '-1'); | ||
res.header('Pragma', 'no-cache'); | ||
next() | ||
}); | ||
|
||
async function processSVG(req, res) { | ||
// Getting values from query | ||
const visitsBG = req.query.visitsBG || "484848"; | ||
const countBG = req.query.countBG || "2574EA"; | ||
const visitsText = req.query.visitsText || "FFFFFF"; | ||
const countText = req.query.countText || "FFFFFF"; | ||
const textShadow = req.query.textShadow || "1"; | ||
const visitsValue = req.query.textContent || "VISITS"; | ||
const userName = req.params.userName; | ||
|
||
// Database Operations | ||
const visit = await Visits.findOneAndUpdate( | ||
{ userName: userName }, | ||
{ $inc: { visits: 1 } }, | ||
{ new: true } | ||
).select({ visits: 1 }); | ||
let visits = 1; | ||
if (visit != null) visits = visit.visits; | ||
else { | ||
const visit = new Visits({ | ||
userName: userName, | ||
visits: 1, | ||
}); | ||
await visit.save(); | ||
} | ||
|
||
// Creating the SVG Badge | ||
const svg = svgBadge(visitsValue, textShadow, visitsBG, countBG, visitsText, countText, visits); | ||
|
||
// Sending the SVG Badge | ||
// Get values from query and parameter | ||
const labelBGColor = req.query.labelBGColor || "484848"; | ||
const countBGColor = req.query.countBGColor || "2574EA"; | ||
const labelTextColor = req.query.labelTextColor || "FFFFFF"; | ||
const countTextColor = req.query.countTextColor || "FFFFFF"; | ||
const shadow = req.query.shadow || "1"; | ||
const label = req.query.label || "VISITS"; | ||
const uniqueID = req.params.uniqueID; | ||
|
||
// Get the current visits count | ||
const visits = await database.visitsBadge(uniqueID); | ||
|
||
// Create the SVG Badge | ||
const svg = svgBadge(label, shadow, labelBGColor, countBGColor, labelTextColor, countTextColor, visits); | ||
|
||
// Send the SVG Badge | ||
res.setHeader("Content-Type", "image/svg+xml"); | ||
res.send(svg); | ||
} | ||
|
||
app.get("/:userName", (req, res) => processSVG(req, res)); | ||
app.listen(port, () => console.log(`Running on port ${port}...`)); | ||
app.get("/:uniqueID", (req, res) => processSVG(req, res)); | ||
app.listen(port, () => console.log(`Ready on port ${port}.`)); |
This file was deleted.
Oops, something went wrong.