Skip to content

Commit

Permalink
Merge pull request #1310 from research-software-directory/1288-fix-ec…
Browse files Browse the repository at this point in the history
…onrefused

chore: add plugin hostname to plugin configuration
  • Loading branch information
cmeessen authored Oct 4, 2024
2 parents 9580d69 + 2dfa718 commit 5ec608d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
3 changes: 1 addition & 2 deletions frontend/config/RsdPluginContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ export const RsdPluginContext = createContext<{settings:PluginConfig[]}>({settin

export default function PluginSettingsProvider(props: any) {
const [settings, setSettings] = useState(props?.settings ?? [])
const value = useMemo(() => ({settings, setSettings}), [settings, setSettings])

return <RsdPluginContext.Provider
value={value}
value={{settings, setSettings}}
{...props}
/>
}
Expand Down
18 changes: 9 additions & 9 deletions frontend/config/getPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import logger from '~/utils/logger'
import {createJsonHeaders} from '~/utils/fetchHelpers'
import {PluginConfig} from '~/config/RsdPluginContext'
import {RsdPluginSettings} from './rsdSettingsReducer'

async function getPlugin({pluginName,token}:{pluginName: string, token?: string}){
const url = `http://localhost/plugin/${pluginName}/api/config`
async function getPlugin({pluginSettings, token}:{pluginSettings: RsdPluginSettings, token?: string}){
try {
const response = await fetch(url, {
const response = await fetch(pluginSettings.config_url, {
headers: {
...createJsonHeaders(token)
},
Expand All @@ -25,34 +25,34 @@ async function getPlugin({pluginName,token}:{pluginName: string, token?: string}
return {
...item,
// add plugin name
name: pluginName
name: pluginSettings.name
}
})
return config
}
logger(`Failed to load plugin config from ${url}: ${response.status} ${response.statusText}`,'warn')
logger(`Failed to load plugin config from ${pluginSettings.config_url}: ${response.status} ${response.statusText}`,'warn')
return []

} catch (error) {
if (error instanceof TypeError) {
const message = error.message
const cause = error.cause
logger(`Error loading plugin config from ${url}. Message: ${message}. Cause: ${cause}.`,'warn')
logger(`Error loading plugin config from ${pluginSettings.config_url}. Message: ${message}. Cause: ${cause}.`,'warn')
} else {
logger(`Error loading plugin config from ${url}: ${error}`,'warn')
logger(`Error loading plugin config from ${pluginSettings.config_url}: ${error}`,'warn')
}
return []
}
}

export default async function getPlugins(
{plugins,token}: {plugins?: string[], token?: string}
{plugins, token}: {plugins?: RsdPluginSettings[], token?: string}
) {
if (!plugins) {
return []
}
// create all requests
const promises = plugins?.map(pluginName=>getPlugin({pluginName,token}))
const promises = plugins?.map(pluginSettings=>getPlugin({pluginSettings, token}))
// execute all requests
const pluginSlots = await Promise.all(promises)
// flatten definitions
Expand Down
7 changes: 6 additions & 1 deletion frontend/config/rsdSettingsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export type RsdSettingsState = {

export type RsdModule= 'software'| 'projects' | 'organisations' | 'communities' | 'news' | 'user'

export type RsdPluginSettings = {
name: string,
config_url: string
}

export type RsdHost = {
name: string,
email: string,
Expand All @@ -41,7 +46,7 @@ export type RsdHost = {
description?: string | null
},
modules?: RsdModule[],
plugins?: string[]
plugins?: RsdPluginSettings[]
}

export type CustomLink = {
Expand Down

0 comments on commit 5ec608d

Please sign in to comment.