diff --git a/discovery/app.go b/discovery/app.go index 67225503..ba5c0103 100644 --- a/discovery/app.go +++ b/discovery/app.go @@ -1,9 +1,8 @@ package discovery import ( + "github.com/google/uuid" "sync" - - "github.com/go-basic/uuid" ) type app struct { @@ -15,7 +14,7 @@ type app struct { container IAppContainer } -//Reset 重置app的节点列表 +// Reset 重置app的节点列表 func (s *app) Reset(nodes Nodes) { tmp := make(map[string]INode) @@ -33,14 +32,14 @@ func (s *app) Reset(nodes Nodes) { s.locker.Unlock() } -//GetAttrs 获取app的属性集合 +// GetAttrs 获取app的属性集合 func (s *app) GetAttrs() Attrs { s.locker.RLock() defer s.locker.RUnlock() return s.attrs } -//GetAttrByName 通过属性名获取app对应属性 +// GetAttrByName 通过属性名获取app对应属性 func (s *app) GetAttrByName(name string) (string, bool) { s.locker.RLock() defer s.locker.RUnlock() @@ -48,7 +47,7 @@ func (s *app) GetAttrByName(name string) (string, bool) { return attr, ok } -//NewApp 创建服务发现app +// NewApp 创建服务发现app func NewApp(checker IHealthChecker, container IAppContainer, attrs Attrs, nodes Nodes) IApp { if attrs == nil { attrs = make(Attrs) @@ -58,17 +57,17 @@ func NewApp(checker IHealthChecker, container IAppContainer, attrs Attrs, nodes nodes: nodes, locker: sync.RWMutex{}, healthChecker: checker, - id: uuid.New(), + id: uuid.NewString(), container: container, } } -//ID 返回app的id +// ID 返回app的id func (s *app) ID() string { return s.id } -//Nodes 将运行中的节点列表返回 +// Nodes 将运行中的节点列表返回 func (s *app) Nodes() []INode { s.locker.RLock() defer s.locker.RUnlock() @@ -82,7 +81,7 @@ func (s *app) Nodes() []INode { return nodes } -//NodeError 定时检查节点,当节点失败时,则返回错误 +// NodeError 定时检查节点,当节点失败时,则返回错误 func (s *app) NodeError(id string) error { s.locker.Lock() defer s.locker.Unlock() @@ -96,7 +95,7 @@ func (s *app) NodeError(id string) error { return nil } -//Close 关闭服务发现的app +// Close 关闭服务发现的app func (s *app) Close() error { // s.container.Remove(s.id) diff --git a/go.mod b/go.mod index 92f33876..e0454bff 100644 --- a/go.mod +++ b/go.mod @@ -7,14 +7,13 @@ require ( github.com/coocood/freecache v1.2.2 github.com/eolinker/eosc v0.7.1 github.com/fasthttp/websocket v1.5.0 - github.com/go-basic/uuid v1.0.0 github.com/go-redis/redis/v8 v8.11.5 + github.com/google/uuid v1.3.0 github.com/hashicorp/consul/api v1.9.1 github.com/nsqio/go-nsq v1.1.0 github.com/ohler55/ojg v1.12.9 github.com/pkg/sftp v1.13.4 github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f - github.com/satori/go.uuid v1.2.0 github.com/valyala/fasthttp v1.42.0 golang.org/x/crypto v0.1.0 golang.org/x/net v0.1.0 @@ -41,7 +40,6 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/btree v1.0.1 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.1 // indirect diff --git a/health-check-http/http-check.go b/health-check-http/http-check.go index b64cacaa..b08cc7d3 100644 --- a/health-check-http/http-check.go +++ b/health-check-http/http-check.go @@ -11,10 +11,10 @@ import ( "github.com/eolinker/eosc/log" "github.com/eolinker/apinto/discovery" - "github.com/go-basic/uuid" + "github.com/google/uuid" ) -//NewHTTPCheck 创建HTTPCheck +// NewHTTPCheck 创建HTTPCheck func NewHTTPCheck(config Config) *HTTPCheck { ctx, cancel := context.WithCancel(context.Background()) @@ -30,7 +30,7 @@ func NewHTTPCheck(config Config) *HTTPCheck { return checker } -//HTTPCheck HTTP健康检查结构,实现了IHealthChecker接口 +// HTTPCheck HTTP健康检查结构,实现了IHealthChecker接口 type HTTPCheck struct { config *Config ctx context.Context @@ -41,7 +41,7 @@ type HTTPCheck struct { locker sync.RWMutex } -//doCheckLoop 定时检查,维护了一个待检测节点集合 +// doCheckLoop 定时检查,维护了一个待检测节点集合 func (h *HTTPCheck) doCheckLoop() { if h.config.Period < 1 { return @@ -78,18 +78,18 @@ func (h *HTTPCheck) doCheckLoop() { } } -//Agent 生成一个agent +// Agent 生成一个agent func (h *HTTPCheck) Agent() (discovery.IHealthChecker, error) { - return NewAgent(uuid.New(), h), nil + return NewAgent(uuid.NewString(), h), nil } -//Reset 重置HTTPCheck的配置 +// Reset 重置HTTPCheck的配置 func (h *HTTPCheck) Reset(conf Config) error { h.config = &conf return nil } -//AddToCheck 将节点添加进HTTPCheck的检查列表 +// AddToCheck 将节点添加进HTTPCheck的检查列表 func (h *HTTPCheck) AddToCheck(node discovery.INode) error { h.addToCheck(&checkNode{ node: node, @@ -98,24 +98,24 @@ func (h *HTTPCheck) AddToCheck(node discovery.INode) error { return nil } -//addToCheck 将节点传入HTTPCheck的检测Channel +// addToCheck 将节点传入HTTPCheck的检测Channel func (h *HTTPCheck) addToCheck(node *checkNode) error { h.ch <- node return nil } -//Stop 停止HTTPCheck,中止定时检查 +// Stop 停止HTTPCheck,中止定时检查 func (h *HTTPCheck) Stop() error { h.cancel() return nil } -//stop 停止从属该agentID的所有节点的健康检查 +// stop 停止从属该agentID的所有节点的健康检查 func (h *HTTPCheck) stop(agentID string) { h.delCh <- agentID } -//check 对待检查的节点集合进行检测,入参:nodes map[agentID][nodeID]*checkNode +// check 对待检查的节点集合进行检测,入参:nodes map[agentID][nodeID]*checkNode func (h *HTTPCheck) check(nodes map[string]map[string]*checkNode) map[string]map[string]*checkNode { //将待检测节点集合中地址相同的节点整合在一起,结构为:map[node.Addr][]*checkNode newNodes := make(map[string][]*checkNode) @@ -157,7 +157,7 @@ func (h *HTTPCheck) check(nodes map[string]map[string]*checkNode) map[string]map return nodes } -//checkNode 进入检查channel的节点结构 +// checkNode 进入检查channel的节点结构 type checkNode struct { node discovery.INode agentID string diff --git a/node/http-context/context.go b/node/http-context/context.go index 475878ab..1e490c40 100644 --- a/node/http-context/context.go +++ b/node/http-context/context.go @@ -13,7 +13,7 @@ import ( eoscContext "github.com/eolinker/eosc/eocontext" http_service "github.com/eolinker/eosc/eocontext/http-context" - uuid "github.com/satori/go.uuid" + uuid "github.com/google/uuid" "github.com/valyala/fasthttp" ) @@ -169,8 +169,7 @@ func (ctx *HttpContext) Request() http_service.IRequestReader { // NewContext 创建Context func NewContext(ctx *fasthttp.RequestCtx, port int) *HttpContext { - id := uuid.NewV4() - requestID := id.String() + requestID := uuid.New().String() newCtx := &HttpContext{ fastHttpRequestCtx: ctx, requestID: requestID, diff --git a/test/body.go b/test/body.go deleted file mode 100644 index f5188954..00000000 --- a/test/body.go +++ /dev/null @@ -1,393 +0,0 @@ -package test - -import ( - "bytes" - "encoding/json" - "encoding/xml" - - http_service "github.com/eolinker/eosc/eocontext/http-context" - - "io/ioutil" - "net/http" - - "mime" - "mime/multipart" - "net/url" -) - -const defaultMultipartMemory = 32 << 20 // 32 MB - -const ( - MultipartForm = "multipart/form-data" - FormData = "application/x-www-form-urlencoded" - TEXT = "text/plain" - JSON = "application/json" - JavaScript = "application/javascript" - AppLicationXML = "application/xml" - TextXML = "text/xml" - Html = "text/html" -) - -//BodyRequestHandler body请求处理器 -type BodyRequestHandler struct { - form url.Values - contentType string - files map[string]*http_service.FileHeader - - isInit bool - isWriteRaw bool - - object interface{} - - raw []byte -} - -//func NewBodyRequestHandler(contentType string,raw) *BodyRequestHandler { -// return &BodyRequestHandler{ contentType: contentType,raw: raw} -//} - -//GetForm 获取表单参数 -func (b *BodyRequestHandler) GetForm(key string) string { - - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - return "" - } - b.parse() - - if !b.isInit || b.form == nil { - return "" - } - return b.form.Get(key) -} - -//ContentType 获取contentType -func (b *BodyRequestHandler) ContentType() string { - return b.contentType -} - -//BodyForm 获取表单参数 -func (b *BodyRequestHandler) BodyForm() (url.Values, error) { - - err := b.parse() - if err != nil { - return nil, err - } - return b.form, nil -} - -//RawBody 获取raw数据 -func (b *BodyRequestHandler) RawBody() ([]byte, error) { - - err := b.encode() - if err != nil { - return nil, err - } - return b.rawBody(), nil - -} - -//Files 获取文件参数 -func (b *BodyRequestHandler) Files() (map[string]*http_service.FileHeader, error) { - - err := b.parse() - - if err != nil { - return nil, err - } - return b.files, nil -} - -func (b *BodyRequestHandler) GetFile(key string) (file *http_service.FileHeader, has bool) { - err := b.parse() - - if err != nil { - return nil, false - } - file, has = b.files[key] - return file, has -} - -//parse 解析 -func (b *BodyRequestHandler) parse() error { - if b.isInit { - return nil - } - contentType, _, _ := mime.ParseMediaType(b.contentType) - switch contentType { - case JSON: - { - e := json.Unmarshal(b.rawBody(), &b.object) - if e != nil { - return e - } - } - case AppLicationXML, TextXML: - { - e := xml.Unmarshal(b.rawBody(), &b.object) - if e != nil { - return e - } - - } - - case MultipartForm: - { - r, err := multipartReader(b.contentType, false, b.rawBody()) - if err != nil { - return err - } - form, err := r.ReadForm(defaultMultipartMemory) - if err != nil { - return err - } - - if b.form == nil { - b.form = make(url.Values) - } - for k, v := range form.Value { - b.form[k] = append(b.form[k], v...) - } - - b.files = make(map[string]*http_service.FileHeader) - for k, fs := range form.File { - - if len(fs) > 0 { - file, err := fs[0].Open() - if err != nil { - return err - } - fileData, err := ioutil.ReadAll(file) - if err != nil { - return err - } - - b.files[k] = &http_service.FileHeader{ - FileName: fs[0].Filename, - Data: fileData, - Header: fs[0].Header, - } - } - } - - b.object = b.form - } - case FormData: - { - form, err := url.ParseQuery(string(b.rawBody())) - if err != nil { - return err - } - if b.form == nil { - b.form = form - } else { - for k, v := range form { - b.form[k] = append(b.form[k], v...) - } - - } - b.object = b.form - - } - } - b.isInit = true - return nil -} - -//SetToForm 设置表单参数 -func (b *BodyRequestHandler) SetToForm(key, value string) error { - - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - return ErrorNotForm - } - - err := b.parse() - if err != nil { - return err - } - b.isWriteRaw = false - - if b.form == nil { - b.form = make(url.Values) - } - b.form.Set(key, value) - b.isWriteRaw = false - - return nil -} - -//AddForm 新增表单参数 -func (b *BodyRequestHandler) AddForm(key, value string) error { - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - return ErrorNotForm - } - err := b.parse() - if err != nil { - return err - } - b.isWriteRaw = false - - if b.form == nil { - b.form = make(url.Values) - } - b.form.Add(key, value) - return nil -} - -//AddFile 新增文件参数 -func (b *BodyRequestHandler) AddFile(key string, file *http_service.FileHeader) error { - - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - return ErrorNotMultipart - } - err := b.parse() - if err != nil { - return err - } - b.isWriteRaw = false - if file == nil && b.files != nil { - delete(b.files, key) - return nil - } - if b.files == nil { - b.files = make(map[string]*http_service.FileHeader) - } - b.files[key] = file - - return nil -} - -////Clone 克隆body -//func (b *BodyRequestHandler) Clone() *BodyRequestHandler { -// rawBody, _ := b.RawBody() -// return newBodyRequestHandler(b.contentType, rawBody) -// -//} - -//BodyInterface 获取请求体对象 -func (b *BodyRequestHandler) BodyInterface() (interface{}, error) { - err := b.parse() - if err != nil { - return nil, err - } - - return b.object, nil -} - -//encode encode -func (b *BodyRequestHandler) encode() error { - if b.isWriteRaw { - return nil - } - - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - b.isWriteRaw = true - return nil - } - - if len(b.files) > 0 { - body := new(bytes.Buffer) - writer := multipart.NewWriter(body) - - for name, file := range b.files { - part, err := writer.CreateFormFile(name, file.FileName) - if err != nil { - return err - } - _, err = part.Write(file.Data) - if err != nil { - return err - } - } - - for key, values := range b.form { - temp := make(url.Values) - temp[key] = values - value := temp.Encode() - err := writer.WriteField(key, value) - if err != nil { - return err - } - } - err := writer.Close() - if err != nil { - return err - } - b.contentType = writer.FormDataContentType() - b.isWriteRaw = true - } else { - if b.form != nil { - b.raw = []byte(b.form.Encode()) - } else { - b.raw = nil - } - } - return nil -} - -//SetForm 设置表单参数 -func (b *BodyRequestHandler) SetForm(values url.Values) error { - - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - return ErrorNotForm - } - b.parse() - b.form = values - b.isWriteRaw = false - - return nil -} - -//SetFile 设置文件参数 -func (b *BodyRequestHandler) SetFile(files map[string]*http_service.FileHeader) error { - - contentType, _, _ := mime.ParseMediaType(b.contentType) - if contentType != FormData && contentType != MultipartForm { - return ErrorNotForm - } - b.parse() - b.files = files - // b.form = values - b.isWriteRaw = false - - return nil -} - -//SetRaw 设置raw数据 -func (b *BodyRequestHandler) SetRaw(contentType string, body []byte) { - b.contentType, b.isInit, b.isWriteRaw = contentType, false, true - return - -} -func (b *BodyRequestHandler) rawBody() []byte { - return b.raw -} - -////newBodyRequestHandler 创建body请求处理器 -//func newBodyRequestHandler(contentType string, body []byte) *BodyRequestHandler { -// b := new(BodyRequestHandler) -// b.SetRaw(contentType, body) -// return b -//} - -func multipartReader(contentType string, allowMixed bool, raw []byte) (*multipart.Reader, error) { - - if contentType == "" { - return nil, http.ErrNotMultipart - } - d, params, err := mime.ParseMediaType(contentType) - if err != nil || !(d == "multipart/form-data" || allowMixed && d == "multipart/mixed") { - return nil, http.ErrNotMultipart - } - boundary, ok := params["boundary"] - if !ok { - return nil, http.ErrMissingBoundary - } - body := ioutil.NopCloser(bytes.NewBuffer(raw)) - return multipart.NewReader(body, boundary), nil -} diff --git a/test/context.go b/test/context.go deleted file mode 100644 index 11481f0c..00000000 --- a/test/context.go +++ /dev/null @@ -1,75 +0,0 @@ -package test - -import ( - "context" - "errors" - "time" - - http_service "github.com/eolinker/eosc/eocontext/http-context" -) - -var ( - ErrorNotForm = errors.New("contentType is not Form") - ErrorNotMultipart = errors.New("contentType is not Multipart") - ErrorNotAllowRaw = errors.New("contentType is not allow Raw") - ErrorNotSend = errors.New("not send") -) -var _ http_service.IHttpContext = (*Context)(nil) - -type ResponseDO interface { - Response(request *RequestReader) (*Response, error) -} -type Context struct { - proxyRequest *ProxyRequest - requestID string - response *Response - responseError error - requestReader *RequestReader - ctx context.Context - - responseHandler ResponseDO -} - -func (c *Context) RequestId() string { - return c.requestID -} - -func (c *Context) Context() context.Context { - if c.ctx == nil { - c.ctx = context.Background() - } - return c.ctx -} - -func (c *Context) Value(key interface{}) interface{} { - return c.Context().Value(key) -} - -func (c *Context) WithValue(key, val interface{}) { - c.ctx = context.WithValue(c.Context(), key, val) -} - -func (c *Context) Request() http_service.IRequestReader { - return c.requestReader -} - -func (c *Context) Proxy() http_service.IRequest { - return c.proxyRequest -} - -func (c *Context) Response() http_service.IResponse { - return c.response -} - -func (c *Context) ResponseError() error { - return c.responseError -} - -func (c *Context) SendTo(address string, timeout time.Duration) error { - - if c.responseHandler != nil { - c.response, c.responseError = c.responseHandler.Response(c.proxyRequest.RequestReader) - } - - return c.responseError -} diff --git a/test/header.go b/test/header.go deleted file mode 100644 index f0e42db8..00000000 --- a/test/header.go +++ /dev/null @@ -1,79 +0,0 @@ -package test - -import ( - "net/http" -) - -type RequestHeader struct { - host string - method string - header http.Header -} - -func (r *RequestHeader) SetHeader(key, value string) { - - r.header.Set(key, value) -} - -func (r *RequestHeader) AddHeader(key, value string) { - r.header.Add(key, value) -} - -func (r *RequestHeader) DelHeader(key string) { - r.header.Del(key) -} - -func (r *RequestHeader) SetHost(host string) { - r.host = host -} - -func (r *RequestHeader) GetHeader(name string) string { - return r.header.Get(name) -} - -func (r *RequestHeader) Headers() http.Header { - return r.header -} - -func (r *RequestHeader) Host() string { - return r.host -} - -func (r *RequestHeader) Method() string { - return r.method -} -func (r *RequestHeader) SetMethod(method string) { - r.method = method -} - -type ResponseHeader struct { - header http.Header -} - -func NewResponseHeader() *ResponseHeader { - return &ResponseHeader{header: make(http.Header)} -} - -func (r *ResponseHeader) GetHeader(name string) string { - return r.header.Get(name) -} - -func (r *ResponseHeader) Headers() http.Header { - - return r.header -} - -func (r *ResponseHeader) SetHeader(key, value string) { - - r.header.Set(key, value) -} - -func (r *ResponseHeader) AddHeader(key, value string) { - - r.header.Add(key, value) -} - -func (r *ResponseHeader) DelHeader(key string) { - - r.header.Del(key) -} diff --git a/test/proxy.go b/test/proxy.go deleted file mode 100644 index 67048681..00000000 --- a/test/proxy.go +++ /dev/null @@ -1,44 +0,0 @@ -package test - -import ( - http_service "github.com/eolinker/eosc/eocontext/http-context" -) - -var _ http_service.IRequest = (*ProxyRequest)(nil) - -type ProxyRequest struct { - *RequestReader -} - -func (r *ProxyRequest) Finish() error { - return nil -} -func (r *ProxyRequest) Header() http_service.IHeaderWriter { - return r.headers -} - -func (r *ProxyRequest) Body() http_service.IBodyDataWriter { - return r.body -} - -func (r *ProxyRequest) URI() http_service.IURIWriter { - return r.uri -} - -func (r *ProxyRequest) SetPath(s string) { - r.URI().SetPath(s) -} - -func NewProxyRequest(remoteAddr string) *ProxyRequest { - - return &ProxyRequest{ - RequestReader: &RequestReader{ - - remoteAddr: remoteAddr, - }, - } -} - -func (r *ProxyRequest) SetMethod(s string) { - r.headers.SetMethod(s) -} diff --git a/test/request.go b/test/request.go deleted file mode 100644 index cb0ce07f..00000000 --- a/test/request.go +++ /dev/null @@ -1,41 +0,0 @@ -package test - -import ( - http_service "github.com/eolinker/eosc/eocontext/http-context" -) - -type RequestReader struct { - headers *RequestHeader - body *BodyRequestHandler - uri *URIRequest - remoteAddr string - method string -} - -func (r *RequestReader) Header() http_service.IHeaderReader { - return r.headers -} - -func (r *RequestReader) Body() http_service.IBodyDataReader { - return r.body -} - -func (r *RequestReader) RemoteAddr() string { - return r.remoteAddr -} - -func (r *RequestReader) ReadIP() string { - return r.remoteAddr -} - -func (r *RequestReader) ForwardIP() string { - return r.ForwardIP() -} - -func (r *RequestReader) URI() http_service.IURIReader { - return r.uri -} - -func (r *RequestReader) Method() string { - return r.method -} diff --git a/test/response.go b/test/response.go deleted file mode 100644 index 136365a5..00000000 --- a/test/response.go +++ /dev/null @@ -1,46 +0,0 @@ -package test - -import ( - "net/http" - "strconv" - - http_service "github.com/eolinker/eosc/eocontext/http-context" -) - -var _ http_service.IResponse = (*Response)(nil) - -type Response struct { - *ResponseHeader - body []byte - statusCode int -} - -func (r *Response) reset() error { - r.ResponseHeader.header = make(http.Header) - return nil -} - -func NewResponse() *Response { - return &Response{ResponseHeader: NewResponseHeader()} -} - -func (r *Response) GetBody() []byte { - return r.body -} - -func (r *Response) StatusCode() int { - return r.statusCode -} - -func (r *Response) Status() string { - return strconv.Itoa(r.statusCode) - -} - -func (r *Response) SetStatus(code int, status string) { - r.statusCode = code -} - -func (r *Response) SetBody(bytes []byte) { - r.body = bytes -} diff --git a/test/test.go b/test/test.go deleted file mode 100644 index dbee0e04..00000000 --- a/test/test.go +++ /dev/null @@ -1,143 +0,0 @@ -package test - -import ( - "encoding/json" - "net/http" - "net/url" - "strconv" - - http_service "github.com/eolinker/eosc/eocontext/http-context" - uuid "github.com/satori/go.uuid" -) - -type IConfig interface { - Create() http_service.IHttpContext -} -type RequestConfig struct { - ContentType string - Body []byte - Header http.Header -} -type ResponseConfig struct { - Body []byte - ContendType string - Header http.Header - StatusCode int - ResponseError error -} - -func (r *ResponseConfig) Response(request *RequestReader) (*Response, error) { - - header := r.Header - if header == nil { - header = make(http.Header) - } - header.Set("content-type", r.ContendType) - header.Set("content-length", strconv.Itoa(len(r.Body))) - - return &Response{ - ResponseHeader: &ResponseHeader{header: r.Header}, - body: r.Body, - statusCode: r.StatusCode, - }, nil -} - -type Config struct { - Method string - Url string - - Request *RequestConfig - - Response ResponseDO -} - -func (c *Config) Create() http_service.IHttpContext { - requestUri, err := url.Parse(c.Url) - if err != nil { - panic(err) - } - if c.Request == nil { - c.Request = &RequestConfig{ - ContentType: "", - Body: nil, - Header: make(http.Header), - } - } else { - if c.Request.Header == nil { - c.Request.Header = make(http.Header) - } - } - c.Request.Header.Set("contend-type", c.Request.ContentType) - c.Request.Header.Set("content-length", strconv.Itoa(len(c.Request.Body))) - return &Context{ - - proxyRequest: &ProxyRequest{}, - requestID: uuid.NewV4().String(), - response: NewResponse(), - responseHandler: c.Response, - responseError: nil, - requestReader: &RequestReader{ - headers: &RequestHeader{ - header: c.Request.Header, - }, - body: &BodyRequestHandler{ - contentType: c.Request.ContentType, - raw: c.Request.Body, - }, - uri: &URIRequest{ - url: requestUri, - }, - remoteAddr: "127.0.0.1", - method: c.Method, - }, - ctx: nil, - } -} -func NewGet(url string, response ResponseDO) http_service.IHttpContext { - c := &Config{ - Method: http.MethodGet, - Url: url, - Request: nil, - Response: response, - } - return c.Create() -} -func NewPostJson(url string, body interface{}, response ResponseDO) http_service.IHttpContext { - bodyData, _ := json.Marshal(body) - - c := &Config{ - Method: http.MethodPost, - Url: url, - Request: &RequestConfig{ - ContentType: "application/json", - Body: bodyData, - Header: make(http.Header), - }, - Response: response, - } - return c.Create() - -} - -func JsonResponse(body interface{}) *ResponseConfig { - bodyData, _ := json.Marshal(body) - return &ResponseConfig{ - Body: bodyData, - ContendType: "application/json", - Header: make(http.Header), - StatusCode: 200, - ResponseError: nil, - } -} - -type PrintResponse struct { -} - -func (p *PrintResponse) Response(request *RequestReader) (*Response, error) { - body := make(map[string]interface{}) - body["url"] = request.uri.RequestURI() - body["host"] = request.uri.Host() - body["body"], _ = request.Body().RawBody() - - return JsonResponse(body).Response(request) -} diff --git a/test/uri.go b/test/uri.go deleted file mode 100644 index 55df18ed..00000000 --- a/test/uri.go +++ /dev/null @@ -1,75 +0,0 @@ -package test - -import ( - "net/url" - - http_service "github.com/eolinker/eosc/eocontext/http-context" -) - -var _ http_service.IURIWriter = (*URIRequest)(nil) - -type URIRequest struct { - url *url.URL -} - -func (u *URIRequest) RequestURI() string { - return u.url.RequestURI() -} - -func (u *URIRequest) Scheme() string { - return u.url.Scheme -} - -func (u *URIRequest) RawURL() string { - return u.RawURL() -} - -func (u *URIRequest) Host() string { - return u.url.Host -} - -func (u *URIRequest) Path() string { - return u.url.Path -} - -func (u *URIRequest) GetQuery(key string) string { - return u.url.Query().Get(key) -} - -func (u *URIRequest) RawQuery() string { - return u.url.RawQuery -} - -func (u *URIRequest) SetQuery(key, value string) { - query := u.url.Query() - query.Set(key, value) - u.url.RawQuery = query.Encode() -} - -func (u *URIRequest) AddQuery(key, value string) { - query := u.url.Query() - query.Add(key, value) - u.url.RawQuery = query.Encode() -} - -func (u *URIRequest) DelQuery(key string) { - query := u.url.Query() - query.Del(key) - u.url.RawQuery = query.Encode() -} - -func (u *URIRequest) SetRawQuery(raw string) { - u.url.RawQuery = raw -} - -func (u *URIRequest) SetPath(s string) { - u.url.Path = s -} - -func (u *URIRequest) SetScheme(scheme string) { - u.url.Scheme = scheme -} - -func (u *URIRequest) SetHost(host string) { - u.url.Host = host -}