context修改

This commit is contained in:
Liujian
2021-11-11 20:23:52 +08:00
parent b296406448
commit c223628ef9
17 changed files with 625 additions and 373 deletions

View File

@@ -3,206 +3,105 @@ package http_context
import (
"context"
"encoding/json"
"net/http"
"github.com/valyala/fasthttp"
http_service "github.com/eolinker/eosc/http-service"
access_field "github.com/eolinker/goku/node/common/access-field"
uuid "github.com/satori/go.uuid"
)
//Context context
var _ http_service.IHttpContext = (*Context)(nil)
//Context requestCtx
type Context struct {
context *fasthttp.RequestCtx
requestOrg *fasthttp.Request
proxyRequest *fasthttp.Request
requestCtx *fasthttp.RequestCtx
requestOrg *fasthttp.Request
proxyRequest *ProxyRequest
proxyResponse *fasthttp.Response
Body []byte
body []byte
requestID string
RestfulParam map[string]string
LogFields *access_field.Fields
request IRequest
labels map[string]string
bodyHandler *BodyRequestHandler
}
func (ctx *Context) SetStatus(code int, status string) {
panic("implement me")
}
func (ctx *Context) Request() http_service.RequestReader {
panic("implement me")
}
func (ctx *Context) ProxyResponse() http_service.ResponseReader {
panic("implement me")
code int
status string
response *Response
requestReader *RequestReader
ctx context.Context
}
func (ctx *Context) Context() context.Context {
panic("implement me")
if ctx.ctx == nil {
ctx.ctx = context.Background()
}
return ctx.ctx
}
func (ctx *Context) Value(key interface{}) interface{} {
panic("implement me")
return ctx.Context().Value(key)
}
func (ctx *Context) WithValue(key, val interface{}) {
panic("implement me")
ctx.ctx = context.WithValue(ctx.Context(), key, val)
}
func (ctx *Context) GetHeader(name string) string {
panic("implement me")
func (ctx *Context) Response() http_service.IResponse {
if ctx.response == nil {
ctx.response = NewResponse(ctx.proxyResponse)
}
return ctx.response
}
func (ctx *Context) Headers() http.Header {
panic("implement me")
func (ctx *Context) Proxy() http_service.IRequest {
return ctx.proxyRequest
}
func (ctx *Context) SetHeader(key, value string) {
panic("implement me")
func (ctx *Context) SetStatus(code int, status string) {
ctx.code, ctx.status = code, status
}
func (ctx *Context) AddHeader(key, value string) {
panic("implement me")
}
func (ctx *Context) DelHeader(key string) {
panic("implement me")
}
func (ctx *Context) Set() http_service.Header {
panic("implement me")
}
func (ctx *Context) Append() http_service.Header {
panic("implement me")
}
func (ctx *Context) Cookie(name string) (*http.Cookie, error) {
panic("implement me")
}
func (ctx *Context) Cookies() []*http.Cookie {
panic("implement me")
}
func (ctx *Context) AddCookie(c *http.Cookie) {
panic("implement me")
}
func (ctx *Context) StatusCode() int {
panic("implement me")
}
func (ctx *Context) Status() string {
panic("implement me")
}
func (ctx *Context) GetBody() []byte {
panic("implement me")
}
func (ctx *Context) Proxy() http_service.Request {
panic("implement me")
}
func (ctx *Context) SetStoreValue(key string, value interface{}) error {
panic("implement me")
}
func (ctx *Context) GetStoreValue(key string) (interface{}, bool) {
panic("implement me")
func (ctx *Context) Request() http_service.IRequestReader {
if ctx.requestReader == nil {
ctx.requestReader = NewRequestReader(ctx.requestOrg, ctx.requestCtx.RemoteAddr().String())
}
return ctx.requestReader
}
//NewContext 创建Context
func NewContext(ctx *fasthttp.RequestCtx) http_service.IHttpContext {
func NewContext(ctx *fasthttp.RequestCtx) *Context {
id := uuid.NewV4()
requestID := id.String()
newRequest := &ctx.Request
newCtx := &Context{
context: ctx,
requestOrg: fasthttp.AcquireRequest(),
proxyRequest: fasthttp.AcquireRequest(),
requestID: requestID,
LogFields: access_field.NewFields(),
requestCtx: ctx,
requestOrg: fasthttp.AcquireRequest(),
requestID: requestID,
}
proxyRequest := fasthttp.AcquireRequest()
newRequest.CopyTo(newCtx.requestOrg)
newRequest.CopyTo(newCtx.proxyRequest)
newRequest.CopyTo(proxyRequest)
newCtx.LogFields.RequestHeader = newCtx.requestOrg.Header.String()
newCtx.LogFields.RequestMsg = string(newCtx.Body)
newCtx.LogFields.RequestMsgSize = len(newCtx.Body)
newCtx.LogFields.RequestUri = string(newCtx.requestOrg.RequestURI())
newCtx.LogFields.RequestID = requestID
newCtx.proxyRequest = NewProxyRequest(NewRequestReader(proxyRequest, ""))
return newCtx
}
func (ctx *Context) Labels() map[string]string {
if ctx.labels == nil {
ctx.labels = map[string]string{}
}
return ctx.labels
}
func (ctx *Context) SetLabels(labels map[string]string) {
if ctx.labels == nil {
ctx.labels = make(map[string]string)
}
if labels != nil {
for k, v := range labels {
ctx.labels[k] = v
}
}
}
//RequestId 请求ID
func (ctx *Context) RequestId() string {
return ctx.requestID
}
//func (ctx *Context) Request() IRequest {
// if ctx.request == nil {
// ctx.request = newRequest(ctx.requestOrg)
// }
// return ctx.request
//}
func (ctx *Context) RequestOrg() *fasthttp.Request {
return ctx.requestOrg
}
func (ctx *Context) ProxyRequest() *fasthttp.Request {
return ctx.proxyRequest
}
//func (ctx *Context) ProxyResponse() *fasthttp.Response {
// return ctx.proxyResponse
//}
func (ctx *Context) BodyHandler() *BodyRequestHandler {
if ctx.bodyHandler == nil {
r := ctx.Request()
body, _ := r.RawBody()
ctx.bodyHandler = newBodyRequestHandler(r.ContentType(), body)
}
return ctx.bodyHandler
}
func (ctx *Context) SetBody(body []byte) {
ctx.context.SetBody(body)
ctx.requestCtx.SetBody(body)
}
func (ctx *Context) SetResponse(response *fasthttp.Response) {
ctx.Body = response.Body()
ctx.body = response.Body()
ctx.proxyResponse = response
}
//Finish finish
func (ctx *Context) Finish() {
ctx.LogFields.ResponseMsg = string(ctx.Body)
ctx.LogFields.ResponseMsgSize = len(ctx.Body)
ctx.proxyResponse.CopyTo(&ctx.context.Response)
ctx.proxyResponse.CopyTo(&ctx.requestCtx.Response)
return
}
@@ -212,10 +111,10 @@ func (ctx *Context) SetError(err error) {
"msg": err.Error(),
}
errByte, _ := json.Marshal(result)
ctx.Body = errByte
ctx.body = errByte
}
func NotFound(ctx *Context) {
ctx.context.SetStatusCode(404)
ctx.context.SetBody([]byte("404 Not Found"))
ctx.requestCtx.SetStatusCode(404)
ctx.requestCtx.SetBody([]byte("404 Not Found"))
}