Skip to content

Commit 222ae80

Browse files
authored
Merge pull request #1 from cardanoapi/update/scheduling
Update/scheduling
2 parents 77af558 + f4ca4ab commit 222ae80

File tree

8 files changed

+65
-35
lines changed

8 files changed

+65
-35
lines changed

.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
SERVER_PORT=8081
2-
SCHEDULE_UPDATE=true
1+
SERVER_PORT=8080
2+
SCHEDULE_UPDATE=true
3+
USE_GH_API=true

.github/workflows/docker-image.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ jobs:
1515
- name: Set up Docker Buildx
1616
uses: docker/setup-buildx-action@v1
1717

18-
- name: Get latest Token Registry API Tag
19-
id: get_latest_tag
20-
run: |
21-
latest_tag=$(curl -s https://api.github.com/repos/cardanoapi/token-registry-api/releases/latest | jq -r .tag_name)
22-
echo "::set-output name=tag::$latest_tag"
23-
2418
- name: Log in to GitHub Container Registry
2519
uses: docker/login-action@v1
2620
with:
@@ -34,6 +28,4 @@ jobs:
3428
context: .
3529
file: ./Dockerfile
3630
push: true
37-
tags: ghcr.io/${{ github.repository_owner }}/token-registry-api:${{ steps.get_latest_tag.outputs.tag }}
38-
build-args: |
39-
DBSYNC_TAG=${{ steps.get_latest_tag.outputs.tag }}
31+
tags: ghcr.io/${{ github.repository_owner }}/token-registry-api:latest

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ RUN yarn install --production --frozen-lockfile
4242

4343
# Copy built application files
4444
COPY --from=build /app/dist/ /app/dist/
45+
COPY --from=build /app/offchain-metadata/ /app/offchain-metadata/
4546

4647
# Expose the port the app will run on
47-
EXPOSE 8081
48+
EXPOSE 8080
4849

4950
# Run the application
5051
CMD ["node", "dist/index.js"]

README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
# Token Registry API
1+
# **Token Registry API**
22

3-
- Queries token registry's github to get off-chain metadata of tokens
4-
- https://github.com/cardano-foundation/cardano-token-registry/tree/master/mappings
3+
The **Token Registry API** queries the Cardano Token Registry's GitHub repository to fetch off-chain metadata of tokens.
54

6-
# Steps to run
5+
🔗 **Token Registry Repository:** [Cardano Token Registry - Mappings](https://github.com/cardano-foundation/cardano-token-registry/tree/master/mappings)
76

8-
- specify `SERVER_PORT` in `.env` file (Default is 8081)
9-
- specify `SCHEDULE_UPDATE` in `.env` file to clone data from token registry and use it as cache
10-
- `yarn && yarn start`
7+
---
118

12-
# Run Image
9+
## **Steps to Run Locally**
1310

14-
`docker run -p 8082:8082 -e SCHEDULE_UPDATE=true -e SERVER_PORT=8082 ghcr.io/cardanoapi/token-registry-api:null`
11+
1. **Set Environment Variables**
12+
- Create a `.env` file and specify the following variables:
13+
- `SERVER_PORT`: Port to run the API (Default: `8080`)
14+
- `SCHEDULE_UPDATE`: Set to `true` to enable scheduled (re-clones the repo every 2 days)
15+
- `USE_GH_API`: Set to `true` to directly query github, else uses the cached info from build-time
16+
17+
2. **Install Dependencies & Start Server**
18+
```sh
19+
yarn && yarn start
20+
```
21+
3. **Endpoints**
22+
- *GET* `/health` : Health info
23+
- *GET* `/meetadata/:id` : Offchain metadata info of an asset
24+
- *POST* `/clone` : force clone the mappings folder from the token registry repo
25+
26+
---
27+
28+
## **Run with Docker**
29+
30+
```sh
31+
docker run -p 8080:8080 \
32+
-e SCHEDULE_UPDATE=true \
33+
-e USE_GH_API=false \
34+
-e SERVER_PORT=8080 \
35+
ghcr.io/cardanoapi/token-registry-api:latest
36+
```
37+
---

docker-restart.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
docker stop token-registry-api-container # Stop the existing container
22
docker rm token-registry-api-container # Remove the stopped container
33
docker build -t token-registry-api . # Build the latest image
4-
docker run -p 8081:8081 -d --name token-registry-api-container --env-file .env token-registry-api # Start a new container
4+
docker run -p 8080:8080 -d --name token-registry-api-container --env-file .env token-registry-api # Start a new container
55
docker logs -f token-registry-api-container

src/config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
require("dotenv").config();
2+
13
export const REPO_OWNER = "cardano-foundation";
24
export const REPO_NAME = "cardano-token-registry";
35
export const BRANCH_NAME = "master";
46
export const FOLDER_NAME = "mappings";
57
export const LOCAL_DIR = "offchain-metadata";
68
export const REPO_URL = `https://github.com/${REPO_OWNER}/${REPO_NAME}.git`;
79
export const DAYS = 2;
8-
export const INTERVAL = DAYS * 24 * 60 * 60 * 1000;
10+
export const INTERVAL = DAYS * 24 * 60 * 60 * 1000;
11+
12+
export const port = process.env.SERVER_PORT ? process.env.SERVER_PORT : 8080;
13+
export const scheduleUpdate = process.env.SCHEDULE_UPDATE
14+
? process.env.SCHEDULE_UPDATE === "true"
15+
: false;
16+
export const useGithub = process.env.USE_GH_API
17+
? process.env.USE_GH_API === "true"
18+
: false;

src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ import {
66
BRANCH_NAME,
77
INTERVAL,
88
LOCAL_DIR,
9+
port,
910
REPO_NAME,
1011
REPO_OWNER,
1112
REPO_URL,
13+
scheduleUpdate,
14+
useGithub,
1215
} from "./config";
1316

14-
require("dotenv").config();
1517
const express = require("express");
1618

1719
const app = express();
18-
const port = process.env.SERVER_PORT ? process.env.SERVER_PORT : 8081;
19-
const scheduleUpdate = process.env.SCHEDULE_UPDATE
20-
? process.env.SCHEDULE_UPDATE === "true"
21-
: false;
2220

2321
// Define the /metadata endpoint
2422
app.get("/metadata/:id", async (req: any, res: any) => {
@@ -27,7 +25,7 @@ app.get("/metadata/:id", async (req: any, res: any) => {
2725
if (!id) {
2826
return res.status(400).json({ error: "missing required parameter 'id'" });
2927
}
30-
if (scheduleUpdate) {
28+
if (!useGithub) {
3129
return await queryCache(LOCAL_DIR, id, res);
3230
} else {
3331
const tokenRegistryRequest = `https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/refs/heads/${BRANCH_NAME}/mappings/${id}.json`;
@@ -56,7 +54,7 @@ setInterval(async () => {
5654

5755
app.get("/health", async (req: any, res: any) => {
5856
try {
59-
if (scheduleUpdate) {
57+
if (!useGithub) {
6058
return await cacheHealth(res);
6159
} else {
6260
return await githubHealth(res);

src/queryCache.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "path";
22
import fs from "fs";
3-
import { INTERVAL, LOCAL_DIR } from "./config";
3+
import { INTERVAL, LOCAL_DIR, scheduleUpdate } from "./config";
44
import { differenceInSeconds } from "date-fns";
55

66
type Health = {
@@ -11,8 +11,8 @@ type Health = {
1111
date: string;
1212
}; // last commit details. read from LOCAL_DIR/.commit file
1313
lastCloned: string; // read this from LOCAL_DIR/.timestamp file
14-
nextClone: string; // add INTERVAL milliseconds to the timestamp file and create a date object .toISOstring()
15-
eta: string; // calculate the hours:minutes:seconds between current and next time
14+
nextClone?: string; // add INTERVAL milliseconds to the timestamp file and create a date object .toISOstring()
15+
eta?: string; // calculate the hours:minutes:seconds between current and next time
1616
};
1717

1818
export async function queryCache(localDir: string, id: string, res: any) {
@@ -71,11 +71,16 @@ export async function cacheHealth(res: any) {
7171
2,
7272
"0"
7373
)}m:${String(seconds).padStart(2, "0")}s`;
74+
const nextCloneAndEta = scheduleUpdate
75+
? {
76+
nextClone: nextCloneDate.toISOString(),
77+
eta: eta,
78+
}
79+
: {};
7480
const healthStatus: Health = {
7581
lastCommit: JSON.parse(lastCommitDetails) as any,
7682
lastCloned: lastClonedDate.toISOString(),
77-
nextClone: nextCloneDate.toISOString(),
78-
eta: eta,
83+
...nextCloneAndEta,
7984
};
8085
res.status(200).json(healthStatus);
8186
}

0 commit comments

Comments
 (0)