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

feat(plugin): allowing limit-req to dynamically adapt to the BE rules #1995

Merged
merged 2 commits into from
Jul 31, 2021
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
3 changes: 2 additions & 1 deletion web/src/components/Plugin/PluginDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const PluginDetail: React.FC<Props> = ({
const [UIForm] = Form.useForm();
const data = initialData[name] || {};
const pluginType = pluginList.find((item) => item.name === name)?.originType;
const pluginSchema = pluginList.find((item) => item.name === name)?.schema;
const [content, setContent] = useState<string>(JSON.stringify(data, null, 2));
const [monacoMode, setMonacoMode] = useState<PluginComponent.MonacoLanguage>(monacoModeList.JSON);
const modeOptions: { label: string; value: string }[] = [
Expand Down Expand Up @@ -411,7 +412,7 @@ const PluginDetail: React.FC<Props> = ({
</Button>
]}
/>
{Boolean(monacoMode === monacoModeList.UIForm) && <PluginForm name={name} form={UIForm} renderForm={!(pluginType === 'auth' && schemaType !== 'consumer')} />}
{Boolean(monacoMode === monacoModeList.UIForm) && <PluginForm name={name} schema={pluginSchema} form={UIForm} renderForm={!(pluginType === 'auth' && schemaType !== 'consumer')} />}
<div style={{ display: monacoMode === monacoModeList.UIForm ? 'none' : 'unset' }}>
<Editor
value={content}
Expand Down
14 changes: 8 additions & 6 deletions web/src/components/Plugin/UI/limit-req.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useIntl } from 'umi';

type Props = {
form: FormInstance;
schema: Record<string, any> | undefined;
ref?: any;
};

Expand All @@ -33,8 +34,9 @@ export const FORM_ITEM_LAYOUT = {
},
};

const LimitReq: React.FC<Props> = ({ form }) => {
const LimitReq: React.FC<Props> = ({ form, schema }) => {
const { formatMessage } = useIntl();
const propertires = schema?.properties;
return (
<Form
form={form}
Expand All @@ -50,7 +52,7 @@ const LimitReq: React.FC<Props> = ({ form }) => {
tooltip={formatMessage({ id: 'component.pluginForm.limit-req.rate.tooltip' })}
validateTrigger={['onChange', 'onBlur', 'onClick']}
>
<InputNumber min={1} required />
<InputNumber min={propertires.rate.exclusiveMinimum} required />
</Form.Item>
<Form.Item
label="burst"
Expand All @@ -62,7 +64,7 @@ const LimitReq: React.FC<Props> = ({ form }) => {
tooltip={formatMessage({ id: 'component.pluginForm.limit-req.burst.tooltip' })}
validateTrigger={['onChange', 'onBlur', 'onClick']}
>
<InputNumber min={0} required />
<InputNumber min={propertires.burst.minimum} required />
</Form.Item>
<Form.Item
label="key"
Expand All @@ -75,18 +77,18 @@ const LimitReq: React.FC<Props> = ({ form }) => {
validateTrigger={['onChange', 'onBlur', 'onClick']}
>
<Select>
{["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"].map(item => {
{propertires.key.enum.map((item: string) => {
return <Select.Option value={item} key={item}>{item}</Select.Option>
})}
</Select>
</Form.Item>
<Form.Item
label="rejected_code"
name="rejected_code"
initialValue={503}
initialValue={propertires.rejected_code.default}
tooltip={formatMessage({ id: 'component.pluginForm.limit-req.rejected_code.tooltip' })}
>
<InputNumber min={200} max={599} />
<InputNumber min={propertires.rejected_code.minimum} max={propertires.rejected_code.maximum} />
</Form.Item>
</Form>
);
Expand Down
5 changes: 3 additions & 2 deletions web/src/components/Plugin/UI/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import Cors from './cors';

type Props = {
name: string,
schema: Record<string, any> | undefined,
form: FormInstance,
renderForm: boolean
}

export const PLUGIN_UI_LIST = ['api-breaker', 'basic-auth', 'cors', 'limit-req', 'limit-conn', 'proxy-mirror', 'referer-restriction', 'limit-count'];

export const PluginForm: React.FC<Props> = ({ name, renderForm, form }) => {
export const PluginForm: React.FC<Props> = ({ name, schema, renderForm, form }) => {

const { formatMessage } = useIntl();

Expand All @@ -52,7 +53,7 @@ export const PluginForm: React.FC<Props> = ({ name, renderForm, form }) => {
case 'cors':
return <Cors form={form} />
case 'limit-req':
return <LimitReq form={form} />
return <LimitReq form={form} schema={schema} />
case 'proxy-mirror':
return <ProxyMirror form={form} />
case 'limit-conn':
Expand Down