-
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.
Merge branch 'develop' of github.com:vylpes/droplet into develop
- Loading branch information
Showing
114 changed files
with
2,386 additions
and
3,170 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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ yarn-error.log | |
.DS_Store | ||
secret.txt | ||
|
||
.docker | ||
.terraform/ | ||
*.tfvars | ||
*.tfstate |
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,28 +1,9 @@ | ||
import { Router } from "express"; | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
interface IPage { | ||
Route(): any; | ||
OnGet(): any; | ||
OnPost(): any; | ||
} | ||
export default interface Page { | ||
OnGet?(req: Request, res: Response, next: NextFunction): void; | ||
OnPost?(req: Request, res: Response, next: NextFunction): void; | ||
|
||
export class Page implements IPage { | ||
private _router: Router; | ||
|
||
constructor(router: Router) { | ||
this._router = router; | ||
} | ||
|
||
get router() { | ||
return this._router; | ||
} | ||
|
||
Route() { | ||
this.OnGet(); | ||
this.OnPost(); | ||
} | ||
|
||
OnGet() {} | ||
|
||
OnPost() {} | ||
OnGetAsync?(req: Request, res: Response, next: NextFunction): Promise<void>; | ||
OnPostAsync?(req: Request, res: Response, next: NextFunction): Promise<void>; | ||
} |
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,21 +1,71 @@ | ||
import { Router } from "express" | ||
import { NextFunction, Router, Request, Response, response } from "express" | ||
import Page from "./Page"; | ||
import { UserMiddleware } from "../middleware/userMiddleware"; | ||
|
||
interface IRoute { | ||
Route(): Router; | ||
} | ||
|
||
export class Route implements IRoute { | ||
export class Route { | ||
private _router: Router; | ||
private _pages: { path: string, page: Page, authorise: boolean, adminAuthorise: boolean }[]; | ||
private _subRoutes: { path: string, route: Route }[]; | ||
|
||
private _authorise: boolean; | ||
private _adminAuthorise: boolean; | ||
|
||
constructor() { | ||
constructor(authorise: boolean = false, adminAuthorise: boolean = false) { | ||
this._router = Router(); | ||
this._pages = []; | ||
this._subRoutes = []; | ||
|
||
this._authorise = authorise; | ||
this._adminAuthorise = adminAuthorise; | ||
} | ||
|
||
get router() { | ||
return this._router; | ||
} | ||
|
||
public AddPage(path: string, page: Page, authorise: boolean = false, adminAuthorise: boolean = false) { | ||
this._pages.push({ path, page, authorise, adminAuthorise}); | ||
} | ||
|
||
public AddSubRoute(path: string, route: Route) { | ||
this._subRoutes.push({ path, route }); | ||
} | ||
|
||
Route(): Router { | ||
for (let page of this._pages) { | ||
if (page.page.OnGet) { | ||
this._router.get(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnGet); | ||
} else if (page.page.OnGetAsync) { | ||
this._router.get(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnGetAsync); | ||
} | ||
|
||
if (page.page.OnPost) { | ||
this._router.post(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnPost); | ||
} else if (page.page.OnPostAsync) { | ||
this._router.post(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnPostAsync); | ||
} | ||
} | ||
|
||
for (let subRoute of this._subRoutes) { | ||
this._router.use(subRoute.path, subRoute.route.Route()); | ||
} | ||
|
||
return this._router; | ||
} | ||
} | ||
|
||
private DoAuthorise(authorise: boolean, req: Request, res: Response, next: NextFunction) { | ||
if (authorise) { | ||
UserMiddleware.Authorise(req, res, next); | ||
} else { | ||
next(); | ||
} | ||
} | ||
|
||
private DoAdminAuthorise(adminAuthorise: boolean, req: Request, res: Response, next: NextFunction) { | ||
if (adminAuthorise) { | ||
UserMiddleware.AdminAuthorise(req, res, next); | ||
} else { | ||
next(); | ||
} | ||
} | ||
} |
Oops, something went wrong.