Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPFS api #37

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
REACT_APP_IPFS_URL="http://109.235.70.27:5001"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are currently connecting to the IPFS node on our own server.

I have created a follow-up to this issue that should connect to IPFS running locally first: #38

REACT_APP_TENSORFLOW_URL="http://109.235.70.27:8501"
3 changes: 2 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
REACT_APP_TENSORFLOW_URL=""
REACT_APP_IPFS_URL="http://localhost:3000/IPFS_API"
REACT_APP_TENSORFLOW_URL="http://localhost:3000/TF_API"
706 changes: 699 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"dayjs": "^1.11.5",
"framer-motion": "^6.2.9",
"history": "^5.3.0",
"ipfs-http-client": "^58.0.0",
"lodash": "^4.17.21",
"node-polyfill-webpack-plugin": "^2.0.0",
"notistack": "^2.0.5",
Expand Down Expand Up @@ -127,9 +128,9 @@
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "4.3.0",
"gh-pages": "^3.2.3",
"http-proxy-middleware": "^2.0.6",
"prettier": "^2.5.1",
"react-app-rewired": "^2.2.1",
"typescript": "^4.5.5"
},
"proxy": "http://109.235.70.27:8501"
}
}
}
12 changes: 12 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import LoadingScreen from './components/LoadingScreen';
// universaldot imports
import { useProfile, useUser, useLoader } from './hooks/universaldot';
import { useSubstrateState } from './substrate-lib';
import { useIpfsAPI } from './api';
// ----------------------------------------------------------------------

export default function App() {
Expand Down Expand Up @@ -41,6 +42,17 @@ export default function App() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [keyring]);

const ipfsAPI = useIpfsAPI();

useEffect(() => {
const fetchIpfsID = async () => {
const result = await ipfsAPI.id();
console.log('IPFS id: ', result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns and object with:

addresses:  (4) [Multiaddr, Multiaddr, Multiaddr, Multiaddr]
agentVersion: "go-ipfs/0.5.1/8431e2e"
id: RSAPeerIdImpl {type: 'RSA', multihash: Digest, privateKey: undefined, publicKey: undefined, string: undefined}
protocolVersion: "ipfs/0.1.0"
publicKey:"CAASpAIwggEgMA0GCSqGSIb3DQEBAQUAA4IBDQAwggEIAoIBAQCoRibE0zQnIU4GA/m9G1D4ZNjeXg1zsQysnvnkm0uTpj8SAT4bMx1RazaJ4Soo5dYF6scjCdLOr0b1RsxtZzeLWhtartmGbaLmR/OqFv7sb4YfMDapBigLSHG9zm2ezdSu29eYSDBjT+AXGC2E43EeBIMvUvJN/VQfmtO55Lm774YipZbp5XMFKm3pcT44t1jVr7Z634/3cOD8mc37bYqtzuNBVxvEcLOPPhsc4nDgGVg2Qr51BDziOaAD6YQGLKnCn7MdTQG5EnL850IBm8sEaoquxnydPl7BGh3Os4PFLk39xEQQFgUEHrPhdZiZV/73goXZctJq+8iKLNYHylmPAgED"
[[Prototype]]: Object

};

fetchIpfsID();
}, [ipfsAPI]);

return (
<MotionLazyContainer>
<ThemeProvider>
Expand Down
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ipfs';
export * from './tensorFlow';
12 changes: 12 additions & 0 deletions src/api/ipfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import windowInstance from '../window';
import { create } from 'ipfs-http-client';
import { useState } from 'react';

const { IPFS_URL } = windowInstance.env;
const VERSION_PATH = '/api/v0';

export const useIpfsAPI = () => {
const [ipfsAPI] = useState(() => create({ url: `${IPFS_URL}${VERSION_PATH}` }));

return ipfsAPI;
};
20 changes: 20 additions & 0 deletions src/setupProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {
app.use(
'/IPFS_API',
createProxyMiddleware({
target: 'http://109.235.70.27:5001',
pathRewrite: { '^/IPFS_API': '' },
changeOrigin: true,
})
);
app.use(
'/TF_API',
createProxyMiddleware({
target: 'http://109.235.70.27:8501',
pathRewrite: { '^/TF_API': '' },
changeOrigin: true,
})
);
};
3 changes: 3 additions & 0 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {};
declare global {
interface Window {
env: {
IPFS_URL: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are putting these configurations in the global Window object?

TENSORFLOW_URL: string;
};
}
Expand All @@ -12,10 +13,12 @@ const windowInstance: Window = window;

if (!window.env) {
window.env = {
IPFS_URL: process.env.REACT_APP_IPFS_URL!,
TENSORFLOW_URL: process.env.REACT_APP_TENSORFLOW_URL!,
};
} else {
window.env = {
IPFS_URL: window.env.IPFS_URL,
TENSORFLOW_URL: window.env.TENSORFLOW_URL,
};
}
Expand Down