-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
75 lines (66 loc) · 1.81 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Status } from "https://deno.land/std@0.193.0/http/http_status.ts";
import axiod from "https://deno.land/x/axiod@0.26.2/mod.ts";
import { cron } from "https://deno.land/x/deno_cron@v1.0.0/cron.ts";
import {
Application,
Context,
helpers,
Router,
} from "https://deno.land/x/oak@v12.6.0/mod.ts";
export const app = new Application();
export const router = new Router();
export const db = new Map();
type Entries<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T][];
type IndodaxResponse = {
[key: string]: {
last: number;
};
};
cron("* * * * *", async () => {
const response = await axiod.get("https://indodax.com/api/ticker_all");
const tickers: Entries<IndodaxResponse> = Object.entries(
response.data.tickers
);
for (const [symbol, { last: value }] of tickers) {
const [key, pair] = symbol.split("_");
if (pair === "idr") {
db.set(key, value);
}
}
});
db.set("test", "OK");
router.get("/", (ctx: Context) => {
const samples = ["btc", "eth", "usdt", "test"];
ctx.response.status = Status.OK;
ctx.response.type = "json";
ctx.response.body = {
message: "For each coin price you can check like on url below",
erro: null,
data: samples.map((sample) => `${ctx.request.url}${sample}`),
};
});
router.get("/:coin", (ctx: Context) => {
const { coin } = helpers.getQuery(ctx, { mergeParams: true });
const result = db.get(coin);
if (!result) {
ctx.response.status = Status.NotFound;
ctx.response.type = "json";
ctx.response.body = {
message: "Coin not found",
error: "Not Found",
data: null,
};
return;
}
ctx.response.status = Status.OK;
ctx.response.type = "json";
ctx.response.body = {
symbol: coin,
price: result,
};
});
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8000 });