52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"github.com/loveuer/nf"
|
||
|
"github.com/loveuer/nf/nft/resp"
|
||
|
"github.com/loveuer/nfflow/internal/inputs/es7"
|
||
|
"github.com/loveuer/nfflow/internal/util"
|
||
|
)
|
||
|
|
||
|
func NewInput_ES7(c *nf.Ctx) error {
|
||
|
type Req struct {
|
||
|
Endpoints []string `json:"endpoints"`
|
||
|
Username string `json:"username"`
|
||
|
Password string `json:"password"`
|
||
|
CA string `json:"ca"`
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
err error
|
||
|
req = new(Req)
|
||
|
)
|
||
|
|
||
|
if err = c.BodyParser(req); err != nil {
|
||
|
return resp.Resp400(c, err.Error())
|
||
|
}
|
||
|
|
||
|
if len(req.Endpoints) == 0 {
|
||
|
return resp.Resp400(c, req)
|
||
|
}
|
||
|
|
||
|
cli := &es7.Client{
|
||
|
Endpoints: req.Endpoints,
|
||
|
Username: req.Username,
|
||
|
Password: req.Password,
|
||
|
CA: req.CA,
|
||
|
}
|
||
|
|
||
|
if err = cli.InitClient(util.Timeout(5)); err != nil {
|
||
|
return resp.Resp400(c, err.Error(), "连接失败, 请检查配置")
|
||
|
}
|
||
|
|
||
|
if err = cli.Ping(util.Timeout(3)); err != nil {
|
||
|
return resp.Resp400(c, err.Error(), "尝试连接失败, 请检查配置")
|
||
|
}
|
||
|
|
||
|
if cli.Save(util.Timeout(5)); err != nil {
|
||
|
return resp.Resp500(c, err.Error())
|
||
|
}
|
||
|
|
||
|
return resp.Resp200(c, nil, "保存 输入流 成功")
|
||
|
}
|