From f9083a1d270b55edefb3aa2e31b15bb37dc0f490 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Fri, 3 Apr 2020 15:35:57 +0200 Subject: [PATCH] Enabling cache on some views --- src/views/views.ts | 101 +++++++++++---------------------------------- 1 file changed, 23 insertions(+), 78 deletions(-) diff --git a/src/views/views.ts b/src/views/views.ts index f808679..50fa5f2 100644 --- a/src/views/views.ts +++ b/src/views/views.ts @@ -1,25 +1,19 @@ -import { Router, IRouter, Request, static as ServeStatic } from "express"; -import GetLoginPage from "./login"; -import GetAuthPage from "./authorize"; -import promiseMiddleware from "../helper/promiseMiddleware"; -import config from "../config"; +import { IRouter, Request, Router, static as ServeStatic } from "express"; import * as Handlebars from "handlebars"; -import GetRegistrationPage from "./register"; -import GetAdminPage from "./admin"; -import { HttpStatusCode } from "../helper/request_error"; import * as moment from "moment"; -import Permission, { IPermission } from "../models/permissions"; -import Client from "../models/client"; -import { Logging } from "@hibas123/nodelogging"; -import Stacker from "../api/middlewares/stacker"; -import { UserMiddleware, GetUserMiddleware } from "../api/middlewares/user"; -// import GetUserPage from "./user"; +import { GetUserMiddleware, UserMiddleware } from "../api/middlewares/user"; +import GetAuthRoute from "../api/oauth/auth"; +import config from "../config"; +import { HttpStatusCode } from "../helper/request_error"; +import GetAdminPage from "./admin"; +import GetAuthPage from "./authorize"; +import GetRegistrationPage from "./register"; Handlebars.registerHelper("appname", () => config.core.name); const cacheTime = config.core.dev - ? moment.duration(1, "month").asSeconds() - : 10; + ? moment.duration(1, "month").asMilliseconds() + : 1000; const ViewRouter: IRouter = Router(); ViewRouter.get("/", UserMiddleware, (req, res) => { @@ -31,9 +25,15 @@ ViewRouter.get("/register", (req, res) => { res.send(GetRegistrationPage(req.__)); }); -ViewRouter.use("/login", ServeStatic("./views_repo/build/login")); +ViewRouter.use( + "/login", + ServeStatic("./views_repo/build/login", { maxAge: cacheTime }) +); -ViewRouter.use("/user", ServeStatic("./views_repo/build/user")); +ViewRouter.use( + "/user", + ServeStatic("./views_repo/build/user", { maxAge: cacheTime }) +); ViewRouter.get("/code", (req, res) => { res.setHeader("Cache-Control", "no-cache"); @@ -53,62 +53,7 @@ ViewRouter.get( } ); - -import GetAuthRoute from "../api/oauth/auth"; - -ViewRouter.get("/auth", GetAuthRoute(true)) - -// ViewRouter.get( -// "/auth", -// Stacker(GetUserMiddleware(false, true), async (req, res) => { -// let { -// scope, -// redirect_uri, -// state, -// client_id -// }: { [key: string]: string } = req.query; -// const sendError = type => { -// res.redirect((redirect_uri += `?error=${type}&state=${state}`)); -// }; - -// let client = await Client.findOne({ client_id: client_id }); -// if (!client) { -// return sendError("unauthorized_client"); -// } - -// let permissions: IPermission[] = []; -// let proms: PromiseLike[] = []; -// if (scope) { -// for (let perm of scope.split(";").filter(e => e !== "read_user")) { -// proms.push( -// Permission.findById(perm).then(p => { -// if (!p) return Promise.reject(new Error()); -// permissions.push(p); -// }) -// ); -// } -// } -// let err = false; -// await Promise.all(proms).catch(e => { -// err = true; -// }); -// Logging.debug(err); -// if (err) { -// return sendError("invalid_scope"); -// } -// let scopes = await Promise.all( -// permissions.map(async perm => { -// let client = await Client.findById(perm.client); -// return { -// name: perm.name, -// description: perm.description, -// logo: client.logo -// }; -// }) -// ); -// res.send(GetAuthPage(req.__, client.name, scopes)); -// }) -// ); +ViewRouter.get("/auth", GetAuthRoute(true)); if (config.core.dev) { const logo = @@ -120,20 +65,20 @@ if (config.core.dev) { name: "Access Profile", description: "It allows the application to know who you are. Required for all applications. And a lot of more Text, because why not? This will not stop, till it is multiple lines long and maybe kill the layout, so keep reading as long as you like, but I promise it will get boring after some time. So this should be enougth.", - logo: logo + logo: logo, }, { name: "Test 1", description: "This is not an real permission. This is used just to verify the layout", - logo: logo + logo: logo, }, { name: "Test 2", description: "This is not an real permission. This is used just to verify the layout", - logo: logo - } + logo: logo, + }, ]) ); });