Skip to content

Commit

Permalink
Cache currency data in a file for better reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffany352 committed Feb 15, 2021
1 parent d2dbd1d commit 20cc99c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@
**/__sapper__
**/.rpt2_cache
**/build
/web/data
5 changes: 5 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web/package.json
Expand Up @@ -42,7 +42,8 @@
"elementtree": "^0.1.7",
"express": "^4.17.1",
"node-fetch": "^2.6.1",
"sirv": "^1.0.0"
"sirv": "^1.0.0",
"atomically": "1.7.0"
},
"browserslist": [
"last 3 chrome versions",
Expand Down
18 changes: 13 additions & 5 deletions web/src/routes/data/currency.json.ts → web/src/currency.ts
@@ -1,7 +1,8 @@
import cached from "./_cached";
import { Def } from "util/defs";
import fetch from "node-fetch";
import { parse } from "elementtree";
import { writeFile } from "atomically";
import { mkdir } from "fs/promises";

async function btc() {
interface Format {
Expand Down Expand Up @@ -104,10 +105,17 @@ async function ecb() {
return defs;
}

export const get = cached(3600 * 1000, async () => {
const [btcDefs, ecbDefs] = await Promise.all([btc(), ecb()]);
const DATA_DIR = process.env.DATA_DIR || "data";
export const currencyPath = `${DATA_DIR}/currency.json`;

export async function updateCurrency() {
const [btcDefs, ecbDefs] = await Promise.all([btc(), ecb()]);
const defs: Def[] = [...btcDefs, ...ecbDefs];

return defs;
});
await mkdir(DATA_DIR, { recursive: true });

await writeFile(currencyPath, JSON.stringify(defs, undefined, "\t"), {
encoding: "utf8",
});
console.log("Successfully updated file.");
}
49 changes: 0 additions & 49 deletions web/src/routes/data/_cached.ts

This file was deleted.

46 changes: 33 additions & 13 deletions web/src/server.js
Expand Up @@ -4,23 +4,43 @@ import sirv from "sirv";
import express from "express";
import compression from "compression";
import * as sapper from "@sapper/server";
import { updateCurrency, currencyPath } from "./currency.ts";
import { existsSync } from "fs";

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === "development";

express()
.use(
compression({ threshold: 0 }),
sirv("static", { dev }),
sapper.middleware()
)
.listen(PORT, (err) => {
if (err) console.log("error", err);
});
async function batchJob() {
await updateCurrency();
process.exit(0);
}

async function main() {
if (!existsSync(currencyPath)) {
await updateCurrency();
}

express()
.use("data", sirv(".", { dev }))
.use(
compression({ threshold: 0 }),
sirv("static", { dev }),
sapper.middleware()
)
.listen(PORT, (err) => {
if (err) console.log("error", err);
});

async function loadRink() {
let rink = await wasm();
Rink.setRink(rink);
async function loadRink() {
let rink = await wasm();
Rink.setRink(rink);
}

loadRink();
}

loadRink();
if (process.argv[2] == "batch-job") {
batchJob();
} else {
main();
}

0 comments on commit 20cc99c

Please sign in to comment.