Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
package
/
goTools
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1f7390d4
authored
Jun 12, 2023
by
盛威豪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持es cron
parent
302543cb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
192 additions
and
5 deletions
+192
-5
_cron/cron.go
+66
-0
_curl/curl.go
+21
-4
_elasticsearch/elasticsearch.go
+104
-0
_time/time.go
+1
-1
_utils/utils.go
+0
-0
No files found.
_cron/cron.go
0 → 100644
View file @
1f7390d4
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
}
utils
/curl.go
→
_curl
/curl.go
View file @
1f7390d4
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
)
}
_elasticsearch/elasticsearch.go
0 → 100644
View file @
1f7390d4
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
}
utils
/time.go
→
_time
/time.go
View file @
1f7390d4
package
utils
package
_time
import
(
"strings"
...
...
utils/utils.go
→
_
utils/utils.go
View file @
1f7390d4
File moved
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment