From e2cc011e38af675f034a3870be6bc5251480fbef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Mon, 3 May 2021 04:57:54 +0200 Subject: [PATCH] pageviews: Track text page --- index.ts | 3 ++- pageviews/index.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 006c281..b6b8065 100644 --- a/index.ts +++ b/index.ts @@ -4,7 +4,7 @@ import config from "./config/config.ts"; import router from "./api/index.ts"; import { homeTXT } from "./txt/index.ts"; import { bootstrapBlocks } from "./blocks/index.ts"; -import pageviews from "./pageviews/index.ts"; +import { pageviews, pageviewsTxt } from "./pageviews/index.ts"; bootstrapBlocks(); @@ -26,6 +26,7 @@ app.use(async (context) => { accepts.includes("text/plain") || context.request.url.pathname === "/index.txt") ) { + await pageviewsTxt(); context.response.body = homeTXT(); } else { await pageviews(); diff --git a/pageviews/index.ts b/pageviews/index.ts index 27f30b3..6406a3c 100644 --- a/pageviews/index.ts +++ b/pageviews/index.ts @@ -2,12 +2,14 @@ import { ensureFile } from "https://deno.land/std/fs/mod.ts"; import format from "https://deno.land/x/date_fns@v2.15.0/format/index.js"; await ensureFile("./pageviews.json"); +await ensureFile("./pageviews_txt.json"); interface IPageViews { [key: string]: number; } const counter: IPageViews = JSON.parse((await Deno.readTextFile("./pageviews.json")) || "{}"); +const counterTxt: IPageViews = JSON.parse((await Deno.readTextFile("./pageviews_txt.json")) || "{}"); -const pageviews = async function () { +export const pageviews = async function () { try { const date = format(new Date(), "yyyy-MM-dd", {}); counter[date] = (counter[date] ?? 0) + 1; @@ -17,4 +19,12 @@ const pageviews = async function () { } }; -export default pageviews; +export const pageviewsTxt = async function () { + try { + const date = format(new Date(), "yyyy-MM-dd", {}); + counterTxt[date] = (counterTxt[date] ?? 0) + 1; + await Deno.writeTextFile("./pageviews_txt.json", JSON.stringify(counterTxt, null, 2)); + } catch (e) { + console.log(e.message); + } +};