-
Notifications
You must be signed in to change notification settings - Fork 114
text/event-stream
not supported
#1375
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
Comments
I'm personally interested in this functionality so that I could generate boilerplate handler code for https://data-star.dev with ogen. What I do currently is that I have two OpenAPI specifications for an application - one API that returns HTML for the GUI and one that returns JSON for M2M communication. With support for |
As a workaround you can use middleware to pass and retrieve the raw request and response objects - #1252. I've tested this approach with https://github.com/starfederation/datastar/tree/main/sdk/go and it works fine. The only issue I found was that Ogen writes an error log saying that there's an superfluous header write once the stream is finished - it doesn't seem to break anything though. |
I made another discovery today, which makes the current workaround totally usable as it seems - the superfluous header error is gone 😄 You'll need at least one dummy response in the OpenAPI specification:
Then you can use a dummy no-op error and watch for it in Ogen's error handler, i.e.: var Noop = errors.New("noop-error")
func ErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
if errors.Is(err, Noop) {
return
} In the Ogen HTTP handlers where manually writing your responses you can then return the no-op error at the end of the SSE "session" like: r := util.RequestFromContext(ctx)
w := util.ResponseFromContext(ctx)
// ...
sse := datastar.NewSSE(w, r)
err = sse.MergeFragments(buff.String())
if err != nil {
return nil, err
}
return nil, herrors.Noop
} |
Hey @rokf I also use Datastar and also have the exact same need in that I need a HTML API and a M2M API. If you have a repo for this, I would love to help out. |
@joeblew999 the repo in which we're using patterns described above is private. It's for a project at work. I don't have a public repo with this code. Regarding Datastar and its use together with ogen - you should be able to find all the information you'll need to make it work in this thread as long as you're familiar with the basics of ogen. For making a HTTP server that combines two or more HTTP servers generated by ogen you can use |
What version of ogen are you using?
Can this issue be reproduced with the latest version?
Yes
What did you do?
Consider the following schema. Save it as
openapi.yaml
.Run the following terminal commands
Create a
main.go
file with the following contents.Click to view server implementation
Finally, start the server.
What did you expect to see?
Client can stream the response. e.g.
What did you see instead?
The generated code in
oas_response_encoders_gen.go
looks like the following.Click to view generated code
The call to
io.Copy
prevents streaming of response. This results in clients not receiving data.The text was updated successfully, but these errors were encountered: