Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk在fc3.0的http请求处理函数中出错。 #39

Open
slayercat opened this issue Nov 11, 2024 · 1 comment
Open

sdk在fc3.0的http请求处理函数中出错。 #39

slayercat opened this issue Nov 11, 2024 · 1 comment

Comments

@slayercat
Copy link

关键片段:


fc.StartHttp(func(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
		fctx, good := fccontext.FromContext(ctx)
		if good {
            log.Println(fctx)
		}
		
		router.ServeHTTP(w, req)
		return nil
	})
}

FC Invoke Start RequestId: 1-67318f55-1310da55-db2afabf2b30
2024/11/11 05:00:05.934155  1-67318f55-1310da55-db2afabf2b30 [ERROR] {
 "errorMessage": "no httpParams found in request",
 "errorType": "errorString"
}
FC Invoke End RequestId: 1-67318f55-1310da55-db2afabf2b30, Error: Unhandled function error

@slayercat
Copy link
Author

slayercat commented Nov 11, 2024

提供如下片段信息供参考,这个片段是我测试了没问题的。


// GinFcAdapter 适配 gin 框架到函数计算
func GinFcAdapter(handler *gin.Engine) interface{} {
	return func(ctx context.Context, httpEvent events.HTTPTriggerEvent) (*events.HTTPTriggerResponse, error) {
		
		// 创建 http.Request
		var reqBody io.Reader
		if httpEvent.Body != nil {
			if httpEvent.IsBase64Encoded != nil && *httpEvent.IsBase64Encoded {
				decoded, err := base64.StdEncoding.DecodeString(*httpEvent.Body)
				if err != nil {
					return nil, err
				}
				reqBody = bytes.NewReader(decoded)
			} else {
				reqBody = strings.NewReader(*httpEvent.Body)
			}
		}

		// 构造请求
		req, err := http.NewRequest(
			*httpEvent.TriggerContext.Http.Method,
			*httpEvent.RawPath,
			reqBody,
		)
		if err != nil {
			return nil, err
		}

		// 设置 headers
		for k, v := range *httpEvent.Headers {
			req.Header.Set(k, v)
		}

		// 设置 query parameters
		q := req.URL.Query()
		for k, v := range *httpEvent.QueryParameters {
			q.Add(k, v)
		}
		req.URL.RawQuery = q.Encode()

		// 创建 response writer
		w := newFcResponseWriter()

		// 处理请求
		handler.ServeHTTP(w, req)

		// 返回响应
		return w.ToFcResponse(), nil
	}
}

// fcResponseWriter 实现 http.ResponseWriter 接口
type fcResponseWriter struct {
	headers    http.Header
	body       bytes.Buffer
	statusCode int
}

func newFcResponseWriter() *fcResponseWriter {
	return &fcResponseWriter{
		headers:    make(http.Header),
		statusCode: http.StatusOK,
	}
}

func (w *fcResponseWriter) Header() http.Header {
	return w.headers
}

func (w *fcResponseWriter) Write(data []byte) (int, error) {
	return w.body.Write(data)
}

func (w *fcResponseWriter) WriteHeader(statusCode int) {
	w.statusCode = statusCode
}

func (w *fcResponseWriter) ToFcResponse() *events.HTTPTriggerResponse {
	headers := make(map[string]string)
	for k, v := range w.headers {
		if len(v) > 0 {
			headers[k] = v[0]
		}
	}

	return &events.HTTPTriggerResponse{
		StatusCode: w.statusCode,
		Headers:    headers,
		Body:       w.body.String(),
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant