Fixing cache control
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Fabian Stamm 2020-08-02 16:42:05 +02:00
parent cece103504
commit f69e40ae9e
3 changed files with 37 additions and 10 deletions

View File

@ -8,12 +8,21 @@ import db, { IPackage } from "../db.ts";
import { v4 } from "https://deno.land/std/uuid/mod.ts";
export default function api(g: ABC.Group) {
g.get("/", (ctx) => {
return { version: "1" };
});
const cacheControl = (next) => (ctx) => {
ctx.response.headers.set("cache-control", "private");
next(ctx);
};
g.get(
"/",
(ctx) => {
return { version: "1" };
},
cacheControl
);
// g.post("/getapikey", getApiKey, basicauth("api"));
g.post("/package/:name", uploadPackage, basicauth("api"));
g.post("/package/:name", uploadPackage, cacheControl, basicauth("api"));
}
// async function getApiKey(ctx: ABC.Context) {

View File

@ -1,6 +1,9 @@
import { ABC } from "../deps.ts";
import { extractPackagePath, getFile } from "../utils.ts";
const MAX_FIXED_CACHE_AGE = 60 * 60 * 24 * 365;
const MAX_FLOATING_CACHE_AGE = 60 * 30;
export default function raw(g: ABC.Group) {
g.get("/:package/*path", async (ctx) => {
console.log(ctx.params, ctx.path);
@ -18,7 +21,21 @@ export default function raw(g: ABC.Group) {
packageVersion,
ctx.params.path
);
if (packageVersion && result) {
ctx.response.headers.set(
"cache-control",
"public, max-age=" + MAX_FIXED_CACHE_AGE
);
} else {
ctx.response.headers.set(
"cache-control",
"no-cache, max-age=" + MAX_FLOATING_CACHE_AGE
);
}
if (!result) return E404();
ctx.response.headers.set("e-tag", result.etag);
return result.data;
});

View File

@ -23,6 +23,11 @@ export default function views(g: ABC.Application) {
packages = await db.package.find({});
}
ctx.render("index", {
packages,
search,
});
const etag =
"W/" +
Hash.createHash("sha3-256")
@ -36,12 +41,8 @@ export default function views(g: ABC.Application) {
)
.toString("base64");
ctx.response.headers.set("e-tag", etag);
return ctx.render("index", {
packages,
search,
});
ctx.response.headers.set("cache-control", CACHE_CONTROL);
ctx.response.headers.set("E-Tag", etag);
});
g.get("/package/:package", async (ctx) => {
let [packageName, packageVersion] = extractPackagePath(