feat: added static uri support

This commit is contained in:
Wyatt Johnson
2018-10-15 14:56:43 -06:00
parent 9347815276
commit 3633fd7afb
12 changed files with 166 additions and 14 deletions
+4
View File
@@ -1,4 +1,5 @@
import cons from "consolidate";
import cors from "cors";
import { Express } from "express";
import http from "http";
import { Redis } from "ioredis";
@@ -56,6 +57,9 @@ export async function createApp(options: AppOptions): Promise<Express> {
})
);
// Enable CORS headers for media assets, font's require them.
parent.use("/assets/media", cors());
// Static Files
parent.use("/assets", cacheHeadersMiddleware("1w"), serveStatic);
@@ -1,7 +1,7 @@
import { RequestHandler } from "express";
import ms from "ms";
export const nocacheMiddleware: RequestHandler = (req, res, next) => {
export const noCacheMiddleware: RequestHandler = (req, res, next) => {
// Set cache control headers to prevent browsers/cdn's from caching these
// requests.
res.set({ "Cache-Control": "no-cache, no-store, must-revalidate" });
@@ -9,10 +9,12 @@ export const nocacheMiddleware: RequestHandler = (req, res, next) => {
next();
};
export const cacheHeadersMiddleware = (duration?: string): RequestHandler => {
export const cacheHeadersMiddleware = (
duration?: string | false
): RequestHandler => {
const maxAge = duration ? Math.floor(ms(duration) / 1000) : false;
if (!maxAge) {
return nocacheMiddleware;
return noCacheMiddleware;
}
return (req, res, next) => {
+9 -2
View File
@@ -11,10 +11,17 @@ export interface ClientTargetHandlerOptions {
/**
* cacheDuration is the cache duration that a given request should be cached for.
*/
cacheDuration?: string;
cacheDuration?: string | false;
/**
* staticURI is prepended to the static url's that are included on the static
* pages.
*/
staticURI: string;
}
export function createClientTargetRouter({
staticURI,
view,
cacheDuration = "1h",
}: ClientTargetHandlerOptions) {
@@ -22,7 +29,7 @@ export function createClientTargetRouter({
const router = express.Router();
router.get("/", cacheHeadersMiddleware(cacheDuration), (req, res) =>
res.render(view)
res.render(view, { staticURI })
);
return router;
+23 -9
View File
@@ -1,7 +1,7 @@
import express, { Router } from "express";
import { AppOptions } from "talk-server/app";
import { nocacheMiddleware } from "talk-server/app/middleware/cacheHeaders";
import { noCacheMiddleware } from "talk-server/app/middleware/cacheHeaders";
import { installedMiddleware } from "talk-server/app/middleware/installed";
import playground from "talk-server/app/middleware/playground";
import { RouterOptions } from "talk-server/app/router/types";
@@ -14,42 +14,56 @@ export async function createRouter(app: AppOptions, options: RouterOptions) {
// Create a router.
const router = express.Router();
router.use("/api", nocacheMiddleware, await createAPIRouter(app, options));
router.use("/api", noCacheMiddleware, await createAPIRouter(app, options));
// Attach the GraphiQL if enabled.
if (app.config.get("enable_graphiql")) {
attachGraphiQL(router, app);
}
const staticURI = app.config.get("static_uri");
// Add the embed targets.
router.use("/embed/stream", createClientTargetRouter({ view: "stream" }));
router.use("/embed/auth", createClientTargetRouter({ view: "auth" }));
router.use(
"/embed/stream",
createClientTargetRouter({ staticURI, view: "stream" })
);
router.use(
"/embed/auth",
createClientTargetRouter({ staticURI, view: "auth", cacheDuration: false })
);
// Add the standalone targets.
router.use(
"/admin",
// If we aren't already installed, redirect the user to the install page.
installedMiddleware({
tenantCache: app.tenantCache,
}),
createClientTargetRouter({ view: "admin" })
createClientTargetRouter({ staticURI, view: "admin", cacheDuration: false })
);
router.use(
"/install",
// If we're already installed, redirect the user to the admin page.
installedMiddleware({
tenantCache: app.tenantCache,
redirectIfInstalled: true,
redirectURL: "/admin",
}),
createClientTargetRouter({ view: "install", cacheDuration: "" })
createClientTargetRouter({
staticURI,
view: "install",
cacheDuration: false,
})
);
// Handle the root path.
router.get(
"/",
// Redirect the user to the install page if they are not, otherwise redirect
// them to the admin.
installedMiddleware({ tenantCache: app.tenantCache }),
(req, res, next) => {
res.redirect("/admin");
}
(req, res, next) => res.redirect("/admin")
);
return router;
+3
View File
@@ -38,9 +38,12 @@ class Server {
constructor(options: ServerOptions) {
this.parentApp = express();
// Load the configuration.
this.config = config
.load(options.config || {})
.validate({ allowed: "strict" });
logger.debug({ config: this.config.toString() }, "loaded configuration");
// Load the graph schemas.
this.schemas = {