feat: can healthcheck a provider

This commit is contained in:
Haishan 2019-12-26 18:16:40 +08:00
parent 62b7f7f7ba
commit b033b4825d
4 changed files with 43 additions and 6 deletions

View file

@ -53,9 +53,15 @@ export async function fetchProviderProxies(config) {
export async function updateProviderByName(config, name) { export async function updateProviderByName(config, name) {
const { url, init } = getURLAndInit(config); const { url, init } = getURLAndInit(config);
const options = { const options = { ...init, method: 'PUT' };
...init,
method: 'PUT'
};
return await fetch(url + '/providers/proxies/' + name, options); return await fetch(url + '/providers/proxies/' + name, options);
} }
export async function healthcheckProviderByName(config, name) {
const { url, init } = getURLAndInit(config);
const options = { ...init, method: 'GET' };
return await fetch(
url + '/providers/proxies/' + name + '/healthcheck',
options
);
}

View file

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { ChevronDown, RotateCw } from 'react-feather'; import { ChevronDown, RotateCw, Zap } from 'react-feather';
import { formatDistance } from 'date-fns'; import { formatDistance } from 'date-fns';
import ResizeObserver from 'resize-observer-polyfill'; import ResizeObserver from 'resize-observer-polyfill';
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
@ -12,7 +12,10 @@ import { SectionNameType } from './shared/Basic';
import { ProxyList, ProxyListSummaryView } from './ProxyGroup'; import { ProxyList, ProxyListSummaryView } from './ProxyGroup';
import { ButtonWithIcon, ButtonPlain } from './Button'; import { ButtonWithIcon, ButtonPlain } from './Button';
import { updateProviderByName } from '../store/proxies'; import {
updateProviderByName,
healthcheckProviderByName
} from '../store/proxies';
import s from './ProxyProvider.module.css'; import s from './ProxyProvider.module.css';
@ -42,6 +45,10 @@ function ProxyProvider({ item, dispatch }: Props) {
() => dispatch(updateProviderByName(apiConfig, item.name)), () => dispatch(updateProviderByName(apiConfig, item.name)),
[apiConfig, dispatch, item.name] [apiConfig, dispatch, item.name]
); );
const healthcheckProvider = useCallback(
() => dispatch(healthcheckProviderByName(apiConfig, item.name)),
[apiConfig, dispatch, item.name]
);
const [isCollapsibleOpen, setCollapsibleOpen] = useState(false); const [isCollapsibleOpen, setCollapsibleOpen] = useState(false);
const toggle = useCallback(() => setCollapsibleOpen(x => !x), []); const toggle = useCallback(() => setCollapsibleOpen(x => !x), []);
@ -67,6 +74,11 @@ function ProxyProvider({ item, dispatch }: Props) {
icon={<Refresh />} icon={<Refresh />}
onClick={updateProvider} onClick={updateProvider}
/> />
<ButtonWithIcon
text="Health Check"
icon={<Zap size={16} />}
onClick={healthcheckProvider}
/>
</div> </div>
</Collapsible2> </Collapsible2>
<Collapsible2 isOpen={!isCollapsibleOpen}> <Collapsible2 isOpen={!isCollapsibleOpen}>

View file

@ -33,6 +33,12 @@
.actionFooter { .actionFooter {
display: flex; display: flex;
button {
margin: 0 5px;
&:first-child {
margin-left: 0;
}
}
} }
.refresh { .refresh {

View file

@ -66,6 +66,19 @@ export function updateProviderByName(apiConfig, name) {
}; };
} }
export function healthcheckProviderByName(apiConfig, name) {
return async dispatch => {
try {
await proxiesAPI.healthcheckProviderByName(apiConfig, name);
} catch (x) {
// ignore
}
// should be optimized
// but ¯\_(ツ)_/¯
dispatch(fetchProxies(apiConfig));
};
}
export function switchProxy(apiConfig, name1, name2) { export function switchProxy(apiConfig, name1, name2) {
return async dispatch => { return async dispatch => {
proxiesAPI proxiesAPI