Skip to content

Commit

Permalink
Merge pull request #858 from gouravmpk/apiUrlFromSwarmHost
Browse files Browse the repository at this point in the history
moving apiFromSwarmHost
  • Loading branch information
Rassl authored Feb 8, 2024
2 parents f83e95b + 60f7296 commit 5e2c2e3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/components/App/Providers/Socket/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SocketProvider.tsx
import { FC, ReactNode } from 'react'
import { io } from 'socket.io-client'
import { API_URL } from '~/constants'
import { API_URL } from '~/utils/apiUrlFromSwarmHost'
import SocketContext from './SocketContext'

interface SocketProviderProps {
Expand Down
31 changes: 2 additions & 29 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable no-console */
import moment from 'moment'
import { API_URL } from '~/utils/apiUrlFromSwarmHost'

const { origin, host } = window.location
const { origin } = window.location

moment.relativeTimeThreshold('h', 24)

Expand All @@ -21,36 +22,8 @@ export const isDevelopment = !!(
origin === 'https://sphinx-jarvis-david.sphinx1.repl.co'
)

const getUrlFormEnv = () => import.meta.env.VITE_APP_API_URL

export const API_URL = getUrlFormEnv() || apiUrlFromSwarmHost() || 'https://knowledge-graph.sphinx.chat'

export const isChileGraph = API_URL.includes('boltwall')

function apiUrlFromSwarmHost(): string | undefined {
// for swarm deployments, always point to "boltwall"
// for now, only if the URL contains "swarm"
const originUrl = window.location.origin

let url = originUrl

if (host.includes('swarm')) {
if (host.startsWith('nav')) {
const hostArray = host.split('.')

hostArray[0] = 'boltwall'

const finalHost = hostArray.join('.')

url = `https://${finalHost}`
}
} else if (originUrl === 'https://second-brain.sphinx.chat' || origin.includes('localhost')) {
url = 'https://knowledge-graph.sphinx.chat'
}

return `${url}/api`
}

export const AWS_IMAGE_BUCKET_URL = 'https://stakwork-uploads.s3.amazonaws.com/'
export const CLOUDFRONT_IMAGE_BUCKET_URL = 'https://d1gd7b7slyku8k.cloudfront.net/'

Expand Down
2 changes: 1 addition & 1 deletion src/network/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { API_URL } from '~/constants'
import { getSignedMessageFromRelay } from '~/utils'
import { API_URL } from '~/utils/apiUrlFromSwarmHost'

const request = async <Res>(url: string, config?: RequestInit): Promise<Res> => {
const admin = localStorage.getItem('admin')
Expand Down
32 changes: 32 additions & 0 deletions src/utils/apiUrlFromSwarmHost/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { apiUrlFromSwarmHost } from '../index.ts'

function mockWindowLocation(url) {
delete global.window.location
global.window = Object.create(window)
global.window.location = new URL(url)
global.window.host = global.window.location.host
global.window.origin = global.window.location.origin
}

describe('apiUrlFromSwarmHost', () => {
afterEach(() => {
jest.restoreAllMocks()
})

it('returns "https://knowledge-graph.sphinx.chat/api" for the URL "https://second-brain.sphinx.chat"', () => {
mockWindowLocation('https://second-brain.sphinx.chat')
expect(apiUrlFromSwarmHost()).toBe('https://knowledge-graph.sphinx.chat/api')
})

it('returns "https://knowledge-graph.sphinx.chat/api" for a URL containing "localhost"', () => {
mockWindowLocation('http://localhost:3000')
expect(apiUrlFromSwarmHost()).toBe('https://knowledge-graph.sphinx.chat/api')
})

it('returns the original URL appended with /api for a URL not containing "swarm" and not hardcoded', () => {
const nonSwarmUrl = 'https://knowledge-graph.sphinx.chat'

mockWindowLocation(nonSwarmUrl)
expect(apiUrlFromSwarmHost()).toBe(`${nonSwarmUrl}/api`)
})
})
29 changes: 29 additions & 0 deletions src/utils/apiUrlFromSwarmHost/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { origin, host } = window.location

const getUrlFormEnv = () => import.meta.env.VITE_APP_API_URL

export const API_URL = getUrlFormEnv() || apiUrlFromSwarmHost() || 'https://knowledge-graph.sphinx.chat'

export function apiUrlFromSwarmHost(): string | undefined {
// for swarm deployments, always point to "boltwall"
// for now, only if the URL contains "swarm"
const originUrl = window.location.origin

let url = originUrl

if (host.includes('swarm')) {
if (host.startsWith('nav')) {
const hostArray = host.split('.')

hostArray[0] = 'boltwall'

const finalHost = hostArray.join('.')

url = `https://${finalHost}`
}
} else if (originUrl === 'https://second-brain.sphinx.chat' || origin.includes('localhost')) {
url = 'https://knowledge-graph.sphinx.chat'
}

return `${url}/api`
}

0 comments on commit 5e2c2e3

Please sign in to comment.