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

Context issue when using protocol's incoming channel with gin framework #1061

Open
tank-500m opened this issue May 23, 2024 · 0 comments
Open

Comments

@tank-500m
Copy link

When creating an HTTP receiver using the gin framework, there is an issue with the context when using the incoming channel from the CloudEvents SDK protocol. The context of the message received through the protocol's incoming channel is different from the gin context created with the HTTP request.

The message context is passed through the channel provided by the CloudEvents SDK protocol, while the gin context is created along with the HTTP request, resulting in two different contexts.

This can cause problems if you need to access the gin context during message processing. For example, you may not be able to access request headers, query parameters, or other information provided by the gin context.

To resolve this issue, you should consider either including the gin context in the message context or passing the gin context separately during message processing. However, even if you include the gin context in the message context, there is no guarantee that a proper response will be sent to the client if an error occurs in the Respond function.

this sample code

package main

import (
	cloudevents "github.com/cloudevents/sdk-go/v2"
	cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.New()

	var p *cehttp.Protocol
	p, _ = cloudevents.NewHTTP()

	/*
		input CloudEvent example:
		{
			"specversion": "1.0",
			"type": "com.example.test",
			"source": "http://example.com",
			"id": UUID value,
			"topic": topicID,  // #extension for test, path param's topic id == this
			"data": {
				"message": "Hello, World!"
			}
		}
	*/

	r.POST("/topics/:topicId/", func(c *gin.Context) {
		ceh, _ := cloudevents.NewHTTPReceiveHandler(c, p, func(e cloudevents.Event) {
			if c.Param("topicId") != e.Extensions()["topicid"] {
				panic("context issue.....!")
			}
		})

		ceh.ServeHTTP(c.Writer, c.Request)
	})

	r.Run(":9192")
}

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