Support cache-control

This commit is contained in:
ryo-ma 2020-08-23 23:07:27 +09:00
parent 7c88457692
commit 6cb75d54f3
2 changed files with 16 additions and 4 deletions

View file

@ -5,15 +5,21 @@ import "https://deno.land/x/dotenv@v0.5.0/load.ts";
const s = serve({ port: 8080 }); const s = serve({ port: 8080 });
const client = new GithubAPIClient(); const client = new GithubAPIClient();
const cacheMaxAge = 7200;
for await (const req of s) { for await (const req of s) {
const username = new URLSearchParams(req.url.split("?")[1]).get("username") const username = new URLSearchParams(req.url.split("?")[1]).get("username");
if (username != null) { if (username != null) {
const userInfo = await client.requestUserInfo(username); const userInfo = await client.requestUserInfo(username);
req.respond( req.respond(
{ {
body: new Card().render(userInfo), body: new Card().render(userInfo),
headers: new Headers({ "Content-Type": "image/svg+xml" }), headers: new Headers(
{
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age: ${cacheMaxAge}`,
},
),
}, },
); );
} else { } else {
@ -24,6 +30,5 @@ for await (const req of s) {
headers: new Headers({ "Content-Type": "text" }), headers: new Headers({ "Content-Type": "text" }),
}, },
); );
} }
} }

View file

@ -2,7 +2,9 @@ import { ServerRequest } from "./deps.ts";
import { GithubAPIClient } from "./src/github_api_client.ts"; import { GithubAPIClient } from "./src/github_api_client.ts";
import { Card } from "./src/card.ts"; import { Card } from "./src/card.ts";
import "https://deno.land/x/dotenv@v0.5.0/load.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts";
const client = new GithubAPIClient(); const client = new GithubAPIClient();
const cacheMaxAge = 7200;
export default async (req: ServerRequest) => { export default async (req: ServerRequest) => {
const username = new URLSearchParams(req.url.split("?")[1]).get("username"); const username = new URLSearchParams(req.url.split("?")[1]).get("username");
@ -12,7 +14,12 @@ export default async (req: ServerRequest) => {
req.respond( req.respond(
{ {
body: new Card().render(userInfo), body: new Card().render(userInfo),
headers: new Headers({ "Content-Type": "image/svg+xml" }), headers: new Headers(
{
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age: ${cacheMaxAge}`,
},
),
}, },
); );
} else { } else {