Commit 1f7390d4 by 盛威豪

支持es cron

parent 302543cb
package _cron
import (
"encoding/json"
"fmt"
"gitlab.aodianyun.com/package/goTools/_curl"
"net/url"
)
var CronService *Cron
type Cron struct {
Domain string `json:"domain"`
}
type CronResult struct {
Code int `json:"code"`
Message string `json:"message"`
Data int `json:"data"`
}
func SetUp(domain string) {
CronService = &Cron{Domain: domain}
}
func (m *Cron) CreateCron(name string, date string, callback string,taskId int) (CronResult, error) {
var cronResult CronResult
if CronService == nil {
return cronResult, fmt.Errorf("服务未实例化")
}
callback = url.QueryEscape(callback)
cronUrl := "http://"+m.Domain+"/api/v1/store"
param := map[string]interface{} {
"name" : name,
"spec" : date,
"command" : callback,
"id" : taskId,
"level" : 1,
"dependency_status" : 1,
"protocol" : 1,
"timeout" : 10,
"multi" : 2,
"notify_status" : 1,
"notify_type" : 2,
"retry_times" : 0,
"retry_interval" : 1,
"http_method" : 1,
}
body, err := _curl.Request(cronUrl, param, map[string]interface{}{
"Content-Type" : _curl.CONTENT_TYPE_FORMDATA,
}, _curl.METHOD_POST, _curl.PARAM_FORM)
json.Unmarshal(body, &cronResult)
return cronResult, err
}
func(m *Cron) DeleteCron(sTaskId int) (CronResult, error){
var cronResult CronResult
if CronService == nil {
return cronResult, fmt.Errorf("服务未实例化")
}
cronUrl := fmt.Sprintf("http://%s/api/v1/remove/%d", m.Domain,sTaskId)
body, _ := _curl.Request(cronUrl, _curl.EmptyMap, _curl.EmptyMap, _curl.METHOD_GET, _curl.PARAM_FORM)
json.Unmarshal(body, &cronResult)
return cronResult, nil
}
package utils
package _curl
import (
"bytes"
......@@ -9,18 +9,35 @@ import (
"strings"
)
const (
METHOD_GET = "GET"
METHOD_POST = "POST"
METHOD_PUT = "PUT"
METHOD_DELETE = "DELETE"
PARAM_DEFAULT = ""
PARAM_JSON = "json"
PARAM_FORM = "form"
CONTENT_TYPE_JSON = "application/json"
CONTENT_TYPE_FORMDATA = "multipart/form-data"
CONTENT_TYPE_URLENCODED = "application/x-www-form-urlencoded"
)
var EmptyMap = map[string]interface{}{}
func Request(url string, data map[string]interface{}, header map[string]interface{}, method string, stype string) (body []byte, err error) {
url = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(url, "\n", ""), " ", ""), "\r", "")
param := []byte("")
if stype == "json" {
param, _ = json.Marshal(data)
header["Content-Type"] = "application/json"
header["Content-Type"] = CONTENT_TYPE_JSON
} else {
s := ""
for k, v := range data {
s += fmt.Sprintf("%s=%v&", k, v)
}
header["Content-Type"] = "application/x-www-form-urlencoded"
header["Content-Type"] = CONTENT_TYPE_URLENCODED
param = []byte(s)
}
......@@ -51,5 +68,5 @@ func Request(url string, data map[string]interface{}, header map[string]interfac
}
func RequestGet(url string) (body []byte, err error) {
return Request(url, map[string]interface{}{}, map[string]interface{}{}, "GET", "")
return Request(url, EmptyMap, EmptyMap, METHOD_GET, PARAM_FORM)
}
package _elasticsearch
import (
"encoding/json"
"fmt"
"gitlab.aodianyun.com/package/goTools/_curl"
)
var EsClient *Client
type Client struct {
Index string `json:"index"`
Type string `json:"type"`
Domain string `json:"domain"`
}
type searchData struct {
Hits struct {
Total int `json:"total"`
MaxScore float64 `json:"max_score"`
Hits []struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Score float64 `json:"_score"`
Source map[string]interface{} `json:"_source"`
} `json:"hits"`
} `json:"hits"`
Status int `json:"status"`
}
func SetUp(host, index, sType string) error {
if host == "" || index == "" || sType == ""{
return fmt.Errorf("参数缺失")
}
EsClient = &Client{
Index: index,
Type: sType,
Domain: host,
}
return nil
}
func (m *Client) Add(typeId, eid string, data map[string]interface{}) error {
if m == nil {
return fmt.Errorf("服务未实例化")
}
_url := m.Domain + "/" + m.Index + "_" + typeId + "/" + m.Type
if eid != "" {
_url += "/" + eid
}
_, err := _curl.Request(_url, data, map[string]interface{}{}, _curl.METHOD_POST, _curl.PARAM_JSON)
if err != nil {
return err
}
return nil
}
func(m *Client) Delete(typeId, eid string) error {
if m == nil {
return fmt.Errorf("服务未实例化")
}
_url := m.Domain + "/" + m.Index + "_" + typeId + "/" + m.Type + "/" + eid
_, err := _curl.Request(_url, map[string]interface{}{}, map[string]interface{}{}, _curl.METHOD_DELETE, _curl.PARAM_JSON)
if err != nil {
return err
}
return nil
}
func (m *Client) DeleteIndex(typeId string) error {
if m == nil {
return fmt.Errorf("服务未实例化")
}
_url := m.Domain + "/" + m.Index + "_" + typeId
_, err := _curl.Request(_url, map[string]interface{}{}, map[string]interface{}{}, _curl.METHOD_DELETE, _curl.PARAM_JSON)
if err != nil {
return err
}
return nil
}
func (m *Client) Search(typeId string, data map[string]interface{}) ([]map[string]interface{}, int, error) {
list := []map[string]interface{}{}
if m == nil {
return list, 0, fmt.Errorf("服务未实例化")
}
_url := m.Domain + "/" + m.Index + "_" + typeId + "/" + m.Type + "/_search"
total := 0
res, err := _curl.Request(_url, data, _curl.EmptyMap, _curl.METHOD_GET, _curl.PARAM_JSON)
if err != nil {
return list, total, err
}
_result := searchData{}
json.Unmarshal(res, &_result)
if _result.Status != 0 {
return list, total, nil
}
total = _result.Hits.Total
for _, hit := range _result.Hits.Hits {
list = append(list, hit.Source)
}
return list, total, err
}
package utils
package _time
import (
"strings"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment