Enabling cache on some views

This commit is contained in:
Fabian Stamm 2020-04-03 15:35:57 +02:00
parent 7071e64f6d
commit f9083a1d27

View File

@ -1,25 +1,19 @@
import { Router, IRouter, Request, static as ServeStatic } from "express"; import { IRouter, Request, Router, static as ServeStatic } from "express";
import GetLoginPage from "./login";
import GetAuthPage from "./authorize";
import promiseMiddleware from "../helper/promiseMiddleware";
import config from "../config";
import * as Handlebars from "handlebars"; 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 * as moment from "moment";
import Permission, { IPermission } from "../models/permissions"; import { GetUserMiddleware, UserMiddleware } from "../api/middlewares/user";
import Client from "../models/client"; import GetAuthRoute from "../api/oauth/auth";
import { Logging } from "@hibas123/nodelogging"; import config from "../config";
import Stacker from "../api/middlewares/stacker"; import { HttpStatusCode } from "../helper/request_error";
import { UserMiddleware, GetUserMiddleware } from "../api/middlewares/user"; import GetAdminPage from "./admin";
// import GetUserPage from "./user"; import GetAuthPage from "./authorize";
import GetRegistrationPage from "./register";
Handlebars.registerHelper("appname", () => config.core.name); Handlebars.registerHelper("appname", () => config.core.name);
const cacheTime = config.core.dev const cacheTime = config.core.dev
? moment.duration(1, "month").asSeconds() ? moment.duration(1, "month").asMilliseconds()
: 10; : 1000;
const ViewRouter: IRouter = Router(); const ViewRouter: IRouter = Router();
ViewRouter.get("/", UserMiddleware, (req, res) => { ViewRouter.get("/", UserMiddleware, (req, res) => {
@ -31,9 +25,15 @@ ViewRouter.get("/register", (req, res) => {
res.send(GetRegistrationPage(req.__)); 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) => { ViewRouter.get("/code", (req, res) => {
res.setHeader("Cache-Control", "no-cache"); res.setHeader("Cache-Control", "no-cache");
@ -53,62 +53,7 @@ ViewRouter.get(
} }
); );
ViewRouter.get("/auth", GetAuthRoute(true));
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<void>[] = [];
// 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));
// })
// );
if (config.core.dev) { if (config.core.dev) {
const logo = const logo =
@ -120,20 +65,20 @@ if (config.core.dev) {
name: "Access Profile", name: "Access Profile",
description: 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.", "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", name: "Test 1",
description: description:
"This is not an real permission. This is used just to verify the layout", "This is not an real permission. This is used just to verify the layout",
logo: logo logo: logo,
}, },
{ {
name: "Test 2", name: "Test 2",
description: description:
"This is not an real permission. This is used just to verify the layout", "This is not an real permission. This is used just to verify the layout",
logo: logo logo: logo,
} },
]) ])
); );
}); });