fix ping status

This commit is contained in:
Matthias Kretschmann 2021-09-14 15:44:12 +02:00
parent 0a8f384e21
commit baec08c0a9
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 8 additions and 5 deletions

View File

@ -11,9 +11,9 @@ export default function Status({ type }: { type: string }): ReactElement {
const url = const url =
type === 'gateway' type === 'gateway'
? `${ipfsGateway}/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme` ? `${ipfsGateway}/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme`
: `${ipfsNodeUri}/api/v0/id` : `${ipfsNodeUri}/api/v0/version`
const method = type === 'gateway' ? 'get' : 'post'
const ping = await pingUrl(url) const ping = await pingUrl(url, method)
setIsLoading(false) setIsLoading(false)
setIsOnline(ping) setIsOnline(ping)
}, [type]) }, [type])

View File

@ -9,9 +9,12 @@ export function formatBytes(a: number, b: number): string {
return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f] return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
} }
export async function pingUrl(url: string): Promise<boolean> { export async function pingUrl(
url: string,
method?: 'get' | 'post'
): Promise<boolean> {
try { try {
const response = await axios(url, { timeout: 5000 }) const response = await axios[method || 'get'](url, { timeout: 5000 })
if (!response || response.status !== 200) return false if (!response || response.status !== 200) return false
return true return true
} catch (error) { } catch (error) {