forked from namandureja/ojingeo
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
86 additions
and
102 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,67 +1,31 @@ | ||
const express = require("express"); | ||
const mongoose = require("mongoose"); | ||
const crypto = require("crypto") | ||
const cookieParser = require("cookie-parser"); | ||
const bodyParser = require("body-parser"); | ||
const accessCode = require("./middleware/accessCode.js"); | ||
const User = require("./models/User.js"); | ||
const Player = require("./models/Player.js"); | ||
const Game = require("./models/Game.js"); | ||
const Worker = require("./models/Worker.js"); | ||
|
||
const db = require("./db") | ||
require("dotenv").config(); | ||
|
||
const app = express(); | ||
|
||
app.set("view engine", "ejs"); | ||
app.use(express.static("public")); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(cookieParser(process.env.COOKIE_SECRET)) | ||
app.use(cookieParser(process.env.COOKIE_SECRET)); | ||
app.use(accessCode); | ||
|
||
app.use("/auth", require("./routes/auth.js")); | ||
app.use("/", require("./routes/dash.js")); | ||
app.use("/admin", require("./routes/admin.js")); | ||
|
||
app.get("/", (req, res) => { | ||
return res.send("index"); | ||
}); | ||
|
||
|
||
const initDatabase = async () => { | ||
let admin = new User({ | ||
_id: "frontman", | ||
password: crypto.createHash('sha256').update(process.env.FRONTMAN_PASS).digest('hex'), | ||
isFrontman: true | ||
}); | ||
await admin.save(); | ||
|
||
const players = require("./data/players.json") | ||
for (let i = 0; i < players.rows.length; i++) { | ||
const record = new Player({ _id: i + 1, ...players.rows[i] }); | ||
await record.save() | ||
} | ||
|
||
const games = require("./data/games.json") | ||
for (let game of games.rows) { | ||
const uuid = game.uuid | ||
delete game.uuid | ||
const record = new Game({ _id: uuid, ...game }); | ||
await record.save() | ||
} | ||
|
||
const workers = require("./data/workers.json") | ||
for (let i = 1; i <= workers.rows.length; i++) { | ||
const record = new Worker({ ...workers.rows[i] }); | ||
await record.save() | ||
} | ||
} | ||
|
||
const isProduction = process.env.NODE_ENV == "production"; | ||
mongoose.connect(isProduction ? process.env.DB_URL : "mongodb://localhost:27017/iitg-2021", async () => { | ||
process.env.PORT = isProduction ? (process.env.PORT || 5000) : 5000; | ||
|
||
let admin = await User.findOne({ _id: "frontman" }); | ||
if (!admin) initDatabase(); | ||
|
||
if (!process.env.PORT) process.env.PORT = isProduction ? 80 : 3000; | ||
db(() => { | ||
app.listen(process.env.PORT, () => { | ||
console.log(`server is listening on port ${process.env.PORT}`); | ||
}); | ||
}); | ||
}); |
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,45 @@ | ||
const mongoose = require("mongoose"); | ||
const crypto = require("crypto"); | ||
const User = require("./models/User.js"); | ||
const Player = require("./models/Player.js"); | ||
const Game = require("./models/Game.js"); | ||
const Worker = require("./models/Worker.js"); | ||
|
||
const initDatabase = async () => { | ||
let admin = new User({ | ||
_id: "frontman", | ||
password: crypto.createHash('sha256').update(process.env.FRONTMAN_PASS).digest('hex'), | ||
isFrontman: true | ||
}); | ||
await admin.save(); | ||
|
||
const players = require("./data/players.json") | ||
for (let i = 0; i < players.rows.length; i++) { | ||
let firstName = players.rows[i].name.split(" ")[0]; | ||
// let gender = ["men","women"][Math.floor(Math.random()*2)] | ||
let gender = ["a", "e", "i", "o", "u"].includes(firstName[firstName.length - 1]) ? "women" : "men"; // statistics :+1: | ||
const record = new Player({ _id: i + 1, ...players.rows[i], pfp: `https://randomuser.me/api/portraits/${gender}/${i % 100}.jpg` }); | ||
await record.save() | ||
} | ||
|
||
const games = require("./data/games.json") | ||
for (let game of games.rows) { | ||
const record = new Game({ _id: game.uuid, ...game }); | ||
await record.save() | ||
} | ||
|
||
const workers = require("./data/workers.json") | ||
for (let i = 1; i <= workers.rows.length; i++) { | ||
const record = new Worker({ ...workers.rows[i] }); | ||
await record.save() | ||
} | ||
} | ||
|
||
|
||
module.exports = callback => { | ||
mongoose.connect(process.env.NODE_ENV == "production" ? process.env.DB_URL : "mongodb://localhost:27017/ojingeo", async () => { | ||
let admin = await User.findOne({ _id: "frontman" }); | ||
if (!admin) await initDatabase(); | ||
callback(); | ||
}); | ||
} |
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,4 +1,4 @@ | ||
module.exports = (req,res,next) => { | ||
if(req.user && req.user.isFrontman) next(); | ||
module.exports = (req, res, next) => { | ||
if (req.user && req.user.isFrontman) return next(); | ||
return res.send("access denied"); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,15 +1,32 @@ | ||
const express = require("express"); | ||
const isLoggedIn = require("../middleware/isLoggedIn") | ||
const isFrontman = require("../middleware/isFrontman") | ||
const isLoggedIn = require("../middleware/isLoggedIn"); | ||
const isFrontman = require("../middleware/isFrontman"); | ||
const Player = require("../models/Player"); | ||
|
||
const router = express.Router(); | ||
|
||
router.use("/",isLoggedIn) | ||
router.use("/",isFrontman) | ||
router.use("/", isLoggedIn); | ||
router.use("/", isFrontman); | ||
|
||
router.get("/", (req, res) => { | ||
console.log(req.user); | ||
return res.send("dash") | ||
}) | ||
router.get("/players", async (req, res) => { | ||
const players = await Player.find({}); | ||
console.log(players); | ||
return res.send("players") | ||
}); | ||
|
||
router.post("/player/new",async (req,res) => { | ||
|
||
}); | ||
|
||
router.post("/player/edit",async (req,res) => { | ||
|
||
}); | ||
|
||
router.get("/players", (req, res) => { | ||
return res.send("players") | ||
}); | ||
router.get("/players", (req, res) => { | ||
return res.send("players") | ||
}); | ||
|
||
module.exports = router; |
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