代码拉取完成,页面将自动刷新
package leego
import (
"fmt"
"io"
"net/http"
"strings"
)
var (
GowayTracer = http.CanonicalHeaderKey("Goway-Tracer")
GowayServer = http.CanonicalHeaderKey("Goway-Server")
)
type OptionGoway struct {
// 环境 (必选)
Env string
// 应用 ID (必选)
AppId string
// 应用版本 (可选)
AppVer string
// 实例 ID (可选)
InstanceId string
}
// RequestByGoway 尝试通过 Goway 网关发起请求
func (own *Context) RequestByGoway(method, url string, headers map[string]string, optGoway OptionGoway, body io.Reader) (*http.Response, error) {
if optGoway.Env == "" || optGoway.AppId == "" {
return nil, fmt.Errorf("nil goway gateway")
}
var server string
if optGoway.AppVer == "" {
if optGoway.InstanceId != "" {
server = fmt.Sprintf("%s, %s, %s, %s", optGoway.Env, optGoway.AppId, optGoway.AppVer, optGoway.InstanceId)
} else {
server = fmt.Sprintf("%s, %s, %s", optGoway.Env, optGoway.AppId, optGoway.AppVer)
}
} else {
server = fmt.Sprintf("%s, %s", optGoway.Env, optGoway.AppId)
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
for key, val := range headers {
req.Header.Set(key, val)
}
req.Header.Set(GowayServer, server)
req.Header.Set(GowayTracer, own.Headers.Get(GowayTracer))
return http.DefaultClient.Do(req)
}
// GetGowayTracer 尝试获取 Goway 网关中的的链路 ID:trace id 和 span id
func (own *Context) GetGowayTracer() (tid, sid string) {
info := strings.Split(own.Headers.Get(GowayTracer), ", ")
if len(info) == 1 {
return info[0], ""
}
if len(info) > 1 {
return info[0], info[1]
}
return
}
// GetGowayServer 尝试获取 Goway 网关的代理数据:env, app id, app ver, instance id
func (own *Context) GetGowayServer() (env, appId, appVer, instanceId string) {
info := strings.Split(own.Headers.Get(GowayServer), ", ")
switch len(info) {
case 0: // 未指定任何信息
return
case 1: // 只指定环境
env = info[0]
case 2: // 指定应用环境和ID
env = info[0]
appId = info[1]
case 3: // 指定应用环境、ID和版本
env = info[0]
appId = info[1]
appVer = info[2]
default: // 指定应用环境、ID、版本和实例ID
env = info[0]
appId = info[1]
appVer = info[2]
instanceId = info[3]
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。