Skip to content

Commit b279e47

Browse files
committed
init engine
1 parent 47ec964 commit b279e47

File tree

424 files changed

+227276
-45
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

424 files changed

+227276
-45
lines changed

command/build.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var CmdList = []string{
1717
}
1818

1919
func buildCmd() (h handler.Handler, e error) {
20-
exec := handler.Handler(handler.HandlerFunc(func(ctx *handler.Context) {}))
20+
exec := handler.Handler(handler.HandleFunc(func(ctx *handler.Context) {}))
2121
for i := len(CmdList) - 1; i >= 0; i-- {
2222
name := CmdList[i]
2323
if setup := Get(name); setup != nil {
@@ -32,13 +32,16 @@ func buildCmd() (h handler.Handler, e error) {
3232
return exec, nil
3333
}
3434

35-
func GetCmdExecutor() handler.Handler {
35+
func Build() (err error) {
3636
once.Do(func() {
37-
bc, err := buildCmd()
37+
cmdExecutor, err = buildCmd()
3838
if err != nil {
39-
panic("init() buildCmd err:" + err.Error())
39+
return
4040
}
41-
cmdExecutor = bc
4241
})
42+
return
43+
}
44+
45+
func GetCmdExecutor() handler.Handler {
4346
return cmdExecutor
4447
}

command/combine/combine.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package combine
22

33
import (
4-
"log"
54
"net/http"
65
"xhttp/command"
6+
"xhttp/command/combine/process"
77
"xhttp/handler"
88
)
99

@@ -23,12 +23,16 @@ type Combine struct {
2323
}
2424

2525
func (c *Combine) ServerHTTP(ctx *handler.Context) {
26-
if c.NextHandler != nil {
27-
defer c.NextHandler.ServerHTTP(ctx)
26+
if c.Next() != nil {
27+
defer c.Next().ServerHTTP(ctx)
2828
}
29-
log.Println("Combine:", ctx.Request.RequestURI)
30-
ctx.Response.WriteHeader(http.StatusOK)
31-
ctx.Response.Write([]byte("ok:" + ctx.Request.RequestURI))
29+
p, ok := process.DefaultProcess[ctx.API.ExecType]
30+
if !ok {
31+
ctx.Response.WriteHeader(http.StatusInternalServerError)
32+
ctx.Response.Write([]byte("no support exec method"))
33+
return
34+
}
35+
p.Exec(ctx)
3236
}
3337

3438
func (c *Combine) Next() handler.Handler {

command/combine/process/http.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package process
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
"strings"
10+
"time"
11+
"xhttp/storage"
12+
)
13+
14+
var HttpRequestFactory = map[string]IHttp{}
15+
16+
func init() {
17+
register(http.MethodPost, &PostJson{})
18+
register(http.MethodGet, &Get{})
19+
}
20+
21+
type IHttp interface {
22+
DoRequest(api *storage.APIChildren, requestParams map[string]interface{}) (v string, err error)
23+
}
24+
25+
func register(method string, o IHttp) {
26+
if _, ok := HttpRequestFactory[method]; !ok {
27+
HttpRequestFactory[method] = o
28+
}
29+
}
30+
31+
type PostJson struct {
32+
}
33+
34+
func (p *PostJson) DoRequest(api *storage.APIChildren, requestParams map[string]interface{}) (v string, err error) {
35+
url := api.Url
36+
var bs []byte
37+
bs, err = json.Marshal(requestParams)
38+
if err != nil {
39+
return
40+
}
41+
var req *http.Request
42+
req, err = http.NewRequest(strings.ToUpper(api.Method), url, strings.NewReader(string(bs)))
43+
c := http.Client{
44+
Timeout: time.Duration(api.Timeout) * time.Second,
45+
}
46+
var resp *http.Response
47+
resp, err = c.Do(req)
48+
if err != nil {
49+
return
50+
}
51+
defer resp.Body.Close()
52+
var bodyBytes []byte
53+
bodyBytes, err = ioutil.ReadAll(resp.Body)
54+
if err != nil {
55+
return
56+
}
57+
return string(bodyBytes), nil
58+
}
59+
60+
type Get struct {
61+
}
62+
63+
func (g *Get) DoRequest(api *storage.APIChildren, requestParams map[string]interface{}) (v string, err error) {
64+
url := api.Url
65+
var req *http.Request
66+
req, err = http.NewRequest(strings.ToUpper(api.Method), url, nil)
67+
q := req.URL.Query()
68+
for k, val := range requestParams {
69+
q.Add(k, val.(string))
70+
}
71+
req.URL.RawQuery = q.Encode()
72+
c := http.Client{
73+
Timeout: time.Duration(api.Timeout) * time.Second,
74+
}
75+
var resp *http.Response
76+
log.Println("req.URL:::::::", req.URL, req.Method)
77+
resp, err = c.Do(req)
78+
if err != nil {
79+
return
80+
}
81+
defer resp.Body.Close()
82+
var bodyBytes []byte
83+
bodyBytes, err = ioutil.ReadAll(resp.Body)
84+
if err != nil {
85+
return
86+
}
87+
return string(bodyBytes), nil
88+
}
89+
90+
func execRequest(api *storage.APIChildren, requestParams map[string]interface{}, data *Response) (err error) {
91+
method := strings.ToUpper(api.Method)
92+
if exec, ok := HttpRequestFactory[method]; ok {
93+
var val string
94+
val, err = exec.DoRequest(api, requestParams)
95+
if err != nil {
96+
return
97+
}
98+
data.Set(api.Name, val)
99+
return
100+
}
101+
return errors.New("no support method:" + method)
102+
}

command/combine/process/iprocess.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package process
2+
3+
import (
4+
"encoding/json"
5+
"sync"
6+
"xhttp/handler"
7+
)
8+
9+
type IProcess interface {
10+
Name() string
11+
Exec(ctx *handler.Context)
12+
}
13+
14+
var DefaultProcess = map[string]IProcess{}
15+
16+
func Register(p IProcess) {
17+
if _, ok := DefaultProcess[p.Name()]; !ok {
18+
DefaultProcess[p.Name()] = p
19+
}
20+
}
21+
22+
type Response struct {
23+
Data map[string]interface{}
24+
mu sync.Mutex
25+
}
26+
27+
func NewResponse() *Response {
28+
return &Response{
29+
Data: make(map[string]interface{}),
30+
}
31+
}
32+
33+
func (r *Response) Set(k string, v interface{}) {
34+
r.mu.Lock()
35+
r.Data[k] = v
36+
r.mu.Unlock()
37+
}
38+
39+
func (r *Response) Get(k string) (v interface{}, ok bool) {
40+
v, ok = r.Data[k]
41+
return
42+
}
43+
44+
func (r *Response) ToBytes() (bs []byte) {
45+
bs, _ = json.MarshalIndent(r.Data, "", " ")
46+
return
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package process
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"sync"
8+
"xhttp/command/combine/process/params"
9+
"xhttp/handler"
10+
"xhttp/storage"
11+
)
12+
13+
func init() {
14+
Register(NewParallelProcess())
15+
}
16+
17+
type ParallelProcess struct {
18+
}
19+
20+
func NewParallelProcess() *ParallelProcess {
21+
return &ParallelProcess{}
22+
}
23+
24+
func (p *ParallelProcess) Name() string {
25+
return "parallel"
26+
}
27+
28+
func (p *ParallelProcess) Exec(ctx *handler.Context) {
29+
data := NewResponse()
30+
wg := sync.WaitGroup{}
31+
for _, apiChild := range ctx.GetCurAPIChildren() {
32+
log.Println("start exec api:", apiChild.Name)
33+
requestParams := make(map[string]interface{})
34+
for _, param := range apiChild.Params {
35+
v, _ := params.GetParamsValue(ctx, param)
36+
requestParams[param.Name] = v
37+
}
38+
wg.Add(1)
39+
go func(api *storage.APIChildren) {
40+
if err := execRequest(api, requestParams, data); err != nil {
41+
log.Println(fmt.Sprintf("url:%s,child:%s,request err:%s", ctx.API.Url, apiChild.Name, err.Error()))
42+
}
43+
wg.Done()
44+
}(apiChild)
45+
}
46+
wg.Wait()
47+
48+
ctx.Response.Header().Add("Content-Type", "application/json")
49+
ctx.Response.WriteHeader(http.StatusOK)
50+
ctx.Response.Write(data.ToBytes())
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package params
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
"xhttp/handler"
7+
)
8+
9+
func init() {
10+
Register("$.body", &Body{})
11+
}
12+
13+
type Body struct {
14+
}
15+
16+
func (b *Body) Get(ctx *handler.Context, name string) (v string, has bool) {
17+
ct := ctx.Request.Header.Get("Content-Type")
18+
if strings.HasPrefix(strings.ToLower(ct), "application/x-www-form-urlencoded") {
19+
return ctx.Request.Form.Get(name), ctx.Request.Form.Has(name)
20+
}
21+
if strings.HasPrefix(strings.ToLower(ct), "application/json") {
22+
decoder := json.NewDecoder(ctx.Request.Body)
23+
var params map[string]string
24+
if err := decoder.Decode(&params); err == nil {
25+
v, has = params[name]
26+
return
27+
}
28+
}
29+
return
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package params
2+
3+
import "xhttp/handler"
4+
5+
func init() {
6+
Register("$.cookie", &Cookie{})
7+
}
8+
9+
type Cookie struct {
10+
}
11+
12+
func (c *Cookie) Get(ctx *handler.Context, name string) (v string, has bool) {
13+
cookies := ctx.Request.Cookies()
14+
for _, cookie := range cookies {
15+
if name == cookie.Name {
16+
return cookie.Value, true
17+
}
18+
}
19+
return v, false
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package params
2+
3+
import "xhttp/handler"
4+
5+
func init() {
6+
Register("$.header", &Header{})
7+
}
8+
9+
type Header struct {
10+
}
11+
12+
func (h *Header) Get(ctx *handler.Context, name string) (v string, has bool) {
13+
if len(ctx.Request.Header.Values(name)) > 0 {
14+
has = true
15+
}
16+
return ctx.Request.Header.Get(name), has
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package params
2+
3+
import (
4+
"xhttp/handler"
5+
"xhttp/storage"
6+
)
7+
8+
var ParamsFactory = map[string]IParams{}
9+
10+
type IParams interface {
11+
Get(ctx *handler.Context, name string) (v string, has bool)
12+
}
13+
14+
func Register(source string, p IParams) {
15+
if _, ok := ParamsFactory[source]; !ok {
16+
ParamsFactory[source] = p
17+
}
18+
}
19+
20+
func GetParamsValue(ctx *handler.Context, param *storage.Param) (v string, hit bool) {
21+
if factory, ok := ParamsFactory[param.Source]; ok {
22+
val, has := factory.Get(ctx, param.Name)
23+
if !has {
24+
val = param.DefaultValue
25+
}
26+
return val, true
27+
}
28+
return param.DefaultValue, false
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package params
2+
3+
import "xhttp/handler"
4+
5+
func init() {
6+
Register("$.query", &Query{})
7+
}
8+
9+
type Query struct {
10+
}
11+
12+
func (q Query) Get(ctx *handler.Context, name string) (v string, has bool) {
13+
v = ctx.Request.URL.Query().Get(name)
14+
return v, ctx.Request.URL.Query().Has(name)
15+
}

0 commit comments

Comments
 (0)