Skip to content

Commit

Permalink
Merge pull request #21 from DIG-Network/release/v0.0.1-alpha.23
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.23
  • Loading branch information
MichaelTaylor3D authored Sep 16, 2024
2 parents d41ed04 + 32736d3 commit a3cb776
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.23](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.22...v0.0.1-alpha.23) (2024-09-16)


### Features

* add redirect logic to other peers when store not found ([aa57a0a](https://github.com/DIG-Network/dig-content-server/commit/aa57a0a1550ddc1ab47fd0125106285ed95b9e76))

### [0.0.1-alpha.22](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.21...v0.0.1-alpha.22) (2024-09-16)

### [0.0.1-alpha.21](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.20...v0.0.1-alpha.21) (2024-09-16)
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dig-content-server",
"version": "0.0.1-alpha.22",
"version": "0.0.1-alpha.23",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand All @@ -24,7 +24,7 @@
"LICENSE"
],
"dependencies": {
"@dignetwork/dig-sdk": "^0.0.1-alpha.18",
"@dignetwork/dig-sdk": "^0.0.1-alpha.21",
"add": "^2.0.6",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
Expand Down
58 changes: 49 additions & 9 deletions src/controllers/storeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
DataIntegrityTree,
DataIntegrityTreeOptions,
DataStore,
DigChallenge
DigChallenge,
DigNetwork,
} from "@dignetwork/dig-sdk";
import { formatBytes } from "../utils/formatBytes";
import {
renderIndexView,
renderStoreView,
renderKeysIndexView,
renderStoreSyncingView,
renderStoreNotFoundView
} from "../views";
import { extname } from "path";

Expand All @@ -27,7 +29,22 @@ const digFolderPath = getStorageLocation();
export const headStore = async (req: Request, res: Response) => {
// @ts-ignore
let { storeId } = req;
const hasRootHash = req.query.hasRootHash as string;

const dataStore = DataStore.from(storeId);

if (hasRootHash) {
const localRootHistory = await dataStore.getLocalRootHistory();
res.setHeader(
"X-Has-RootHash",
localRootHistory?.some(
(rootHistory) => rootHistory.root_hash === hasRootHash
)
? "true"
: "false"
);
}

const { latestStore: state } = await dataStore.fetchCoinInfo();
res.setHeader("X-Generation-Hash", state.metadata.rootHash.toString("hex"));
res.setHeader("X-Store-Id", storeId);
Expand Down Expand Up @@ -68,6 +85,17 @@ export const getKeysIndex = async (req: Request, res: Response) => {

const showKeys = req.query.showKeys === "true";

const storeList = getStoresList();

if (!storeList.includes(storeId)) {
const peerRedirect = await DigNetwork.findPeerWithStoreKey(
storeId,
rootHash
);

return res.status(400).send(renderStoreNotFoundView(storeId, rootHash, chainName, peerRedirect));
}

const options: DataIntegrityTreeOptions = {
storageMode: "local",
storeDir: `${digFolderPath}/stores`,
Expand Down Expand Up @@ -138,11 +166,15 @@ export const getKeysIndex = async (req: Request, res: Response) => {

// Controller for handling the /:storeId/* route
export const getKey = async (req: Request, res: Response) => {
try {
// @ts-ignore
let { storeId, rootHash } = req;
const catchall = req.params[0];
// @ts-ignore
let { chainName, storeId, rootHash } = req;
const catchall = req.params[0];

const key = Buffer.from(decodeURIComponent(catchall), "utf-8").toString(
"hex"
);

try {
// Extract the challenge from query parameters
const challengeHex = req.query.challenge as string; // Expecting a hex string here

Expand All @@ -153,10 +185,6 @@ export const getKey = async (req: Request, res: Response) => {
rootHash = storeInfo.latestStore.metadata.rootHash.toString("hex");
}

const key = Buffer.from(decodeURIComponent(catchall), "utf-8").toString(
"hex"
);

console.log("Fetching key:", key);

const options: DataIntegrityTreeOptions = {
Expand Down Expand Up @@ -235,6 +263,18 @@ export const getKey = async (req: Request, res: Response) => {
res.status(500).send("Error streaming file.");
});
} catch (error) {
const peerRedirect = await DigNetwork.findPeerWithStoreKey(
storeId,
rootHash,
catchall
);

if (peerRedirect) {
res.redirect(
`http://${peerRedirect}:4161/${chainName}.${storeId}.${rootHash}/${catchall}`
);
}

res.setHeader("X-Key-Exists", "false");
console.error("Error in getKey controller:", error);
res.status(500).send("Error retrieving the requested file.");
Expand Down
3 changes: 2 additions & 1 deletion src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './indexView';
export * from './storeView';
export * from './keysIndexView';
export * from './storeSyncingView';
export * from './unknownChainView';
export * from './unknownChainView';
export * from './peerRedirectView';
109 changes: 109 additions & 0 deletions src/views/peerRedirectView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
export const renderStoreNotFoundView = (
storeId: string,
rootHash: string,
chainName: string,
peerIp?: string | null
) => {
// If no peers are available, show "Store not found on this network"
if (!peerIp) {
return `
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
width: 90%;
max-width: 500px;
border: 1px solid #ddd;
border-radius: 10px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
}
.message {
font-size: 1.2em;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<h2 class="message">Store Not Found on This Network</h2>
</div>
</div>
</body>
</html>
`;
}

// If there are peers available, show the redirect option
return `
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
width: 90%;
max-width: 500px;
border: 1px solid #ddd;
border-radius: 10px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
}
.message {
font-size: 1.2em;
color: #333;
}
.button {
background-color: #007BFF;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 1em;
cursor: pointer;
margin-top: 20px;
text-decoration: none;
display: inline-block;
}
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<h2 class="message">Store Not Found on This Peer</h2>
<p>Click the button below to redirect to another peer.</p>
<a class="button" href="http://${peerIp}:4161/${chainName}.${storeId}.${rootHash}">
Redirect
</a>
</div>
</div>
</body>
</html>
`;
};

0 comments on commit a3cb776

Please sign in to comment.