This repository was archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Complete refactor of app based on OOP principles #102
Open
DeadEye3164
wants to merge
9
commits into
master
Choose a base branch
from
refactor-routes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8765002
Complete refactor of app based on OOP principles
ef5ee44
Use JSON file instead of a JavaScript variable exported as Node modul…
05c2fda
Removed mysql as a dependency
2732e2f
Removed console.logs for serialization of users
de71df9
Update default cookie name
DeadEye3164 b742a00
Remove https as a module requirement
DeadEye3164 8d1ee06
Remove HTTPS port
DeadEye3164 5179d20
Remove the old HTTPS certificate code
DeadEye3164 73ca47f
chore: remove empty comment
ankush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 +1,4 @@ | ||
| { | ||
| "tabWidth": 4, | ||
| "useTabs": true, | ||
| "printWidth": 150 | ||
| "useTabs": true | ||
| } |
This file contains hidden or 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 hidden or 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,6 @@ | ||
| "use strict"; | ||
|
|
||
| require("dotenv").config(); | ||
|
|
||
| const { Server } = require("./controllers/Server"); | ||
| new Server(); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,26 @@ | ||
| "use strict"; | ||
| const Router = require("./Router"); | ||
|
|
||
| class Courses extends Router { | ||
| constructor(app, passport, db) { | ||
| super(app, passport, db); | ||
| } | ||
|
|
||
| setRoutes(db) { | ||
| this.router.get("/", (req, res) => { | ||
| db.Course.findAll().then((result) => { | ||
| console.log(req.user); | ||
| res.render("index.html", { | ||
| title: "Compass – Courses", | ||
| heading: "Courses", | ||
| courseReviewData: result, | ||
| user: req.user, | ||
| errorMessage: req.flash("error"), | ||
| warningMessage: req.flash("warning"), | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = Courses; |
This file contains hidden or 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,26 @@ | ||
| "use strict"; | ||
|
|
||
| const Router = require("./Router"); | ||
|
|
||
| class Errors extends Router { | ||
| constructor(app, passport, db) { | ||
| super(app, passport, db); | ||
| } | ||
|
|
||
| setRoutes(db) { | ||
| // Handle pages not found. | ||
| this.app.use((req, res) => { | ||
| res.status(404).render("404.html", { | ||
| title: "Compass - Page not found", | ||
| }); | ||
| }); | ||
|
|
||
| // Handle 500 server errors. | ||
| this.app.use((err, req, res) => { | ||
| console.error(err.stack); | ||
| res.status(500).send("<h1>500: Internal server error</h1>"); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = Errors; |
This file contains hidden or 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,37 @@ | ||
| const fs = require("fs"); | ||
| const http = require("http"); | ||
| const https = require("https"); | ||
|
|
||
| class HttpServer { | ||
| constructor(app) { | ||
| this.createHttpServer(app); | ||
| } | ||
|
|
||
| createHttpServer(app) { | ||
| const httpServer = http.createServer(app); | ||
| const httpPort = process.env.HTTP_PORT || "8087"; | ||
| const httpsPort = process.env.HTTPS_PORT || "8443"; | ||
|
DeadEye3164 marked this conversation as resolved.
Outdated
|
||
|
|
||
| httpServer.listen(httpPort, () => { | ||
| console.log(`HTTP Server started at port ${httpPort}`); | ||
| }); | ||
|
|
||
| if (process.env.NODE_ENV === "PROD") { | ||
| const certDir = process.env.TLS_CERTDIR; | ||
| const tlsOptions = { | ||
| key: fs.readFileSync(certDir + "privkey.pem", "utf8"), | ||
| cert: fs.readFileSync(certDir + "cert.pem", "utf8"), | ||
| ca: fs.readFileSync(certDir + "chain.pem", "utf8"), | ||
| }; | ||
|
|
||
| // create HTTPS server | ||
| const httpsServer = https.createServer(tlsOptions, app); | ||
|
|
||
| httpsServer.listen(httpsPort, () => { | ||
| console.log(`HTTPS Server started at port ${httpsPort}!`); | ||
| }); | ||
|
DeadEye3164 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = HttpServer; | ||
This file contains hidden or 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,88 @@ | ||
| "use strict"; | ||
|
|
||
| const passport = require("passport"); | ||
|
|
||
| class Passport { | ||
| constructor(app, db) { | ||
| this.passport = passport; | ||
| this.createPassportSession(app, db); | ||
| } | ||
|
|
||
| createPassportSession(app, db) { | ||
| app.use(passport.initialize(undefined)); | ||
| app.use(passport.session(undefined)); | ||
|
|
||
| // use `id` for serializing and deserializing users in database | ||
| passport.serializeUser(function (user, done) { | ||
| console.log("Serializing " + user.id); | ||
|
ankush marked this conversation as resolved.
Outdated
|
||
| done(null, user.id); | ||
| }); | ||
|
|
||
| passport.deserializeUser(function (id, done) { | ||
| console.log("Deserializing " + id); | ||
|
DeadEye3164 marked this conversation as resolved.
Outdated
|
||
|
|
||
| let result = db.User.findByPk(id) | ||
| .then(done(null, true)) | ||
| .catch((err) => { | ||
| done(err, null, { message: "User does not exist" }); | ||
| }); | ||
| }); | ||
|
|
||
| const SlackStrategy = require("passport-slack").Strategy; | ||
|
|
||
| passport.use( | ||
| "slack", | ||
| new SlackStrategy( | ||
| { | ||
| clientID: process.env.SLACK_CLIENT_ID, | ||
| clientSecret: process.env.SLACK_CLIENT_SECRET, | ||
| scope: [ | ||
| "identity.basic", | ||
| "identity.email", | ||
| "identity.avatar", | ||
| ], | ||
| }, | ||
| (accessToken, refreshToken, profile, done) => { | ||
| db.User.findByPk(profile.user.id) | ||
| .then((user) => { | ||
| if (!user) { | ||
| db.User.create({ | ||
| id: profile.user.id, | ||
| name: profile.user.name, | ||
| email: profile.user.email, | ||
| avatar_url: profile.user.image_512, | ||
| }) | ||
| .then(() => { | ||
| return done(null, user); | ||
| }) | ||
| .catch((err) => { | ||
| console.log( | ||
| "User couldn't be created: " + err | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| return done(null, user); | ||
| }) | ||
| .catch((err) => { | ||
| console.log(err); | ||
| }); | ||
| } | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| checkAuth(req, res, next) { | ||
| if (req.isAuthenticated()) { | ||
| next(); | ||
| } else { | ||
| req.flash( | ||
| "warning", | ||
| "You have to sign in before you can access this page" | ||
| ); | ||
| res.redirect("/courses"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = Passport; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.