Add interactive form/report
This commit is contained in:
parent
4be2328faa
commit
bda66e47eb
1 changed files with 66 additions and 0 deletions
66
checker/interactive.go
Normal file
66
checker/interactive.go
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
package checker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RenderForm implements sdk.CheckerInteractive.
|
||||||
|
func (p *xmppProvider) RenderForm() []sdk.CheckerOptionField {
|
||||||
|
return []sdk.CheckerOptionField{
|
||||||
|
{
|
||||||
|
Id: "domain",
|
||||||
|
Type: "string",
|
||||||
|
Label: "Domain",
|
||||||
|
Placeholder: "example.com",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "mode",
|
||||||
|
Type: "string",
|
||||||
|
Label: "Mode",
|
||||||
|
Default: "both",
|
||||||
|
Choices: []string{"c2s", "s2s", "both"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "timeout",
|
||||||
|
Type: "number",
|
||||||
|
Label: "Per-endpoint timeout (seconds)",
|
||||||
|
Default: 10,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseForm implements sdk.CheckerInteractive.
|
||||||
|
func (p *xmppProvider) ParseForm(r *http.Request) (sdk.CheckerOptions, error) {
|
||||||
|
domain := strings.TrimSpace(r.FormValue("domain"))
|
||||||
|
domain = strings.TrimSuffix(domain, ".")
|
||||||
|
if domain == "" {
|
||||||
|
return nil, errors.New("domain is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := sdk.CheckerOptions{"domain": domain}
|
||||||
|
|
||||||
|
if mode := strings.TrimSpace(r.FormValue("mode")); mode != "" {
|
||||||
|
switch mode {
|
||||||
|
case "c2s", "s2s", "both":
|
||||||
|
opts["mode"] = mode
|
||||||
|
default:
|
||||||
|
return nil, errors.New("mode must be one of: c2s, s2s, both")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if to := strings.TrimSpace(r.FormValue("timeout")); to != "" {
|
||||||
|
v, err := strconv.ParseFloat(to, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("timeout must be a number")
|
||||||
|
}
|
||||||
|
opts["timeout"] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts, nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue