Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
cc7dca1e
Commit
cc7dca1e
authored
Feb 08, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:添加rpc目录,markdown
parent
cf725231
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
0 deletions
+150
-0
README.md
README.md
+21
-0
const.go
rpc/const.go
+13
-0
http.go
rpc/http.go
+116
-0
No files found.
README.md
0 → 100644
View file @
cc7dca1e
# hilo公共代码
# 项目架构
+
目录说明
+
mycontext: 上下文
+
mylogrus: 日志包
+
resource: 资源层
+
config: 配置相关
+
consul: 注册中心
+
mysql: 数据库
+
redisCli: 缓存
+
domain: 领域层
+
ctx.go: 定义ctxAndDb
+
model.go: 通用model
+
event.go: 抽象定义event
+
rpc: rpc请求
+
const.go: 常量
+
http.go: 封装http相关
+
utils: 工具包
+
script: 临时脚本
rpc/const.go
0 → 100644
View file @
cc7dca1e
package
rpc
import
"time"
const
(
defaultMaxIdleConnsPerHost
=
100
defaultMaxIdleConns
=
100
defaultDialTimeout
=
10
*
time
.
Second
defaultKeepAliveTimeout
=
30
*
time
.
Second
defaultIdleConnTimeout
=
90
*
time
.
Second
defaultTLSHandshakeTimeout
=
10
*
time
.
Second
defaultExpectContinueTimeout
=
1
*
time
.
Second
)
rpc/http.go
0 → 100644
View file @
cc7dca1e
package
rpc
import
(
"git.hilo.cn/hilo-common/domain"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
)
var
httpTransport
=
&
http
.
Transport
{
MaxIdleConnsPerHost
:
defaultMaxIdleConnsPerHost
,
MaxIdleConns
:
defaultMaxIdleConns
,
Proxy
:
http
.
ProxyFromEnvironment
,
DialContext
:
(
&
net
.
Dialer
{
Timeout
:
defaultDialTimeout
,
KeepAlive
:
defaultKeepAliveTimeout
,
})
.
DialContext
,
IdleConnTimeout
:
defaultIdleConnTimeout
,
TLSHandshakeTimeout
:
defaultTLSHandshakeTimeout
,
ExpectContinueTimeout
:
defaultExpectContinueTimeout
,
}
// 执行http get请求
// param: header + postForm
// return: resp body,err
func
HttpGet
(
model
*
domain
.
Model
,
_url
string
,
header
map
[
string
]
string
,
query
map
[
string
][]
string
)
([]
byte
,
error
)
{
var
err
error
defer
func
()
{
if
err
!=
nil
{
model
.
Log
.
Errorf
(
"HttpPut err:%v"
,
err
)
}
}()
if
len
(
query
)
>
0
{
q
:=
url
.
Values
{}
for
k
,
v
:=
range
query
{
for
_
,
v2
:=
range
v
{
q
.
Add
(
k
,
v2
)
}
}
_url
+=
"?"
+
q
.
Encode
()
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
_url
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
for
k
,
v
:=
range
header
{
req
.
Header
.
Add
(
k
,
v
)
}
resp
,
err
:=
httpTransport
.
RoundTrip
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
if
resp
!=
nil
{
err
=
resp
.
Body
.
Close
()
}
return
body
,
err
}
// 执行http post请求
// 默认: Content-Type":"application/x-www-form-urlencoded"
// param: header + postForm
// return: resp body,err
func
HttpPostForm
(
model
*
domain
.
Model
,
_url
string
,
header
,
form
map
[
string
]
string
)
([]
byte
,
error
)
{
return
httpForm
(
model
,
"POST"
,
_url
,
header
,
form
)
}
// 执行http put请求
// 默认: Content-Type":"application/x-www-form-urlencoded"
// param: header + postForm
// return: resp body,err
func
HttpPutForm
(
model
*
domain
.
Model
,
_url
string
,
header
map
[
string
]
string
,
form
map
[
string
]
string
)
([]
byte
,
error
)
{
return
httpForm
(
model
,
"PUT"
,
_url
,
header
,
form
)
}
// 默认: Content-Type":"application/x-www-form-urlencoded"
// param: header + postForm
// return: resp body,err
func
httpForm
(
model
*
domain
.
Model
,
method
,
_url
string
,
header
map
[
string
]
string
,
form
map
[
string
]
string
)
([]
byte
,
error
)
{
var
err
error
defer
func
()
{
if
err
!=
nil
{
model
.
Log
.
Errorf
(
"HttpPut err:%v"
,
err
)
}
}()
postForm
:=
url
.
Values
{}
for
k
,
v
:=
range
form
{
postForm
.
Set
(
k
,
v
)
}
payload
:=
strings
.
NewReader
(
postForm
.
Encode
())
req
,
err
:=
http
.
NewRequest
(
method
,
_url
,
payload
)
req
.
Header
.
Add
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
if
err
!=
nil
{
return
nil
,
err
}
for
k
,
v
:=
range
header
{
req
.
Header
.
Add
(
k
,
v
)
}
resp
,
err
:=
httpTransport
.
RoundTrip
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
if
resp
!=
nil
{
err
=
resp
.
Body
.
Close
()
}
return
body
,
err
}
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