Commit cc7dca1e authored by hujiebin's avatar hujiebin

feat:添加rpc目录,markdown

parent cf725231
# 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: 临时脚本
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
)
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
}
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 to comment