Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:vylpes/droplet into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Vylpes committed Feb 18, 2024
2 parents 768d3e6 + 6a1a482 commit 35ae169
Show file tree
Hide file tree
Showing 114 changed files with 2,386 additions and 3,170 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ EMAIL_TEMPLATE_PASSWORDRESET_RESETLINK=http://localhost:3000/auth/password-reset
EMAIL_TEMPLATE_VERIFYUSER_VERIFYLINK=http://localhost:3000/auth/verify?token={token}

DB_HOST=
DB_ROOT_HOST=
DB_PORT=
DB_NAME=
DB_AUTH_USER=
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ yarn-error.log
.DS_Store
secret.txt

.docker
.terraform/
*.tfvars
*.tfstate
26 changes: 13 additions & 13 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import connectFlash from "connect-flash";
import { PugMiddleware } from "./middleware/pugMiddleware";
import AppDataSource from "./database/dataSources/appDataSource";

import { AuthRouter } from "./routes/auth";
import { DashboardRouter } from "./routes/dashboard";
import { IndexRouter } from "./routes";
import SettingsRouter from "./routes/settingsRouter";
import ItemsRouter from "./routes/itemsRouter";
import ItemPurchasesRouter from "./routes/itemPurchasesRouter";
import SuppliesRouters from "./routes/suppliesRouter";
import SupplyPurchasesRouter from "./routes/SupplyPurchasesRouter";
import ListingsRouter from "./routes/listingsRouter";
import OrdersRouter from "./routes/ordersRouter";
import StorageRouter from "./routes/storageRouter";
import ReturnsRouter from "./routes/ReturnsRouter";
import PostagePolicyRouter from "./routes/postagePolicyRouter";
import AuthRouter from "./routes/auth";
import DashboardRouter from "./routes/dashboard";
import IndexRouter from "./routes/index";
import SettingsRouter from "./routes/settings";
import ItemsRouter from "./routes/items";
import ItemPurchasesRouter from "./routes/itemPurchases";
import SuppliesRouters from "./routes/supplies";
import SupplyPurchasesRouter from "./routes/supplyPurchases";
import ListingsRouter from "./routes/listings";
import OrdersRouter from "./routes/orders";
import StorageRouter from "./routes/storage";
import ReturnsRouter from "./routes/returns";
import PostagePolicyRouter from "./routes/postagePolicy";

export class App {
private _app: Express;
Expand Down
31 changes: 6 additions & 25 deletions src/contracts/Page.ts
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>;
}
66 changes: 58 additions & 8 deletions src/contracts/Route.ts
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();
}
}
}
Loading

0 comments on commit 35ae169

Please sign in to comment.