danielgtaylor / danielgtaylor/huma

Support streaming content

Open
#801 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
4.4k
Forks
285
Avg merge
40m
Merged PRs (30d)
1

Description

I got the task to add a video endpoint to our server. We're using huma for automated documentation of the api.

I was not able to find examples of how to add streaming content using huma. So I added a request using a raw mux router. This works fine, but because I had to use a raw mux router, the video endpoint does not show up on the generated documentation.

It would be cool if huma would support these kind of requests, to be able to generate documentation for it.

Our endpoint implementation looks like this:

```
var introductionVideo []byte

func IntroductionVideo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "video/mp4")

rangeHeader := r.Header.Get("Range")
if rangeHeader == "" {
w.WriteHeader(http.StatusOK)
w.Write(introductionVideo)

return
}

var start, end int64

_, err := fmt.Sscanf(rangeHeader, "bytes=%d-%d", &start, &end)
if err != nil {
_, err = fmt.Sscanf(rangeHeader, "bytes=%d-", &start)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

end = int64(len(introductionVideo)) - 1
}

if start < 0 || start >= int64(len(introductionVideo)) || end >= int64(len(introductionVideo)) || start > end {
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
return
}

contentLength := end - start + 1

w.Header().Set("Content-Length", fmt.Sprintf("%d", contentLength))
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, len(introductionVideo)))
w.WriteHeader(http.StatusPartialContent)
w.Write(introductionVideo[start : end+1])
}

```

It would be neat if huma would support requests like this, particularly adding the request header `Range` and the response header `Content-Range` to the docs and API validation, with the following formats:

|Location|Header|Format|Docs|
|---|---|---|---|
|Request|Range|bytes=start-end|Start and end specify the requested range.|
|Response|Content-Range|bytes start-end/length|Start and end specify the requested range. Length specifies the total length of the object.|

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.