org.go 1.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package application

import (
	"fmt"
Y
Your Name 已提交
5
	goku_plugin "github.com/eolinker/goku-plugin"
E
eoLinker API Management 已提交
6 7 8
	"net/http"
	"net/url"
	"time"
Y
Your Name 已提交
9 10

	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
11 12
)

Y
Your Name 已提交
13
//Org org
E
eoLinker API Management 已提交
14 15 16 17
type Org struct {
	server string
}

Y
Your Name 已提交
18
//Send 请求发送,忽略重试
Y
Your Name 已提交
19
func (app *Org) Send(ctx goku_plugin.ContextAccess,proto string, method string, path string, querys url.Values, header http.Header, body []byte, timeout time.Duration, retry int) (*http.Response, string, []string, error) {
E
eoLinker API Management 已提交
20

Y
Your Name 已提交
21 22
	var response *http.Response
	var err error
E
eoLinker API Management 已提交
23 24

	FinalTargetServer := ""
Y
Your Name 已提交
25
	RetryTargetServers := make([]string, 0, retry+1)
E
eoLinker API Management 已提交
26

Y
Your Name 已提交
27
	path = utils.TrimPrefixAll(path, "/")
E
eoLinker API Management 已提交
28

Y
Your Name 已提交
29
	for doTrice := retry + 1; doTrice > 0; doTrice-- {
E
eoLinker API Management 已提交
30

Y
Your Name 已提交
31
		u := fmt.Sprintf("%s://%s/%s", proto, app.server, path)
E
eoLinker API Management 已提交
32
		FinalTargetServer = app.server
Y
Your Name 已提交
33
		RetryTargetServers = append(RetryTargetServers, FinalTargetServer)
Y
Your Name 已提交
34
		response, err = request(ctx,method, u, querys, header, body, timeout)
Y
Your Name 已提交
35 36 37 38
		if err != nil {
			continue
		} else {
			return response, FinalTargetServer, RetryTargetServers, err
E
eoLinker API Management 已提交
39 40 41
		}
	}

Y
Your Name 已提交
42
	return response, FinalTargetServer, RetryTargetServers, err
E
eoLinker API Management 已提交
43 44
}

Y
Your Name 已提交
45
//NewOrg 创建新的IHttpApplication
Y
Your Name 已提交
46
func NewOrg(server string) IHttpApplication {
E
eoLinker API Management 已提交
47
	return &Org{
Y
Your Name 已提交
48
		server: server,
E
eoLinker API Management 已提交
49
	}
Y
Your Name 已提交
50
}