How to use with http.ServeFile? #187
-
I have an endpoint that returns large files of varying Content-Types. The regular |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, the easiest way to access those would be to embed (or use directly if you don't need extra parameters/headers) Please check an example. package usecase
import (
"context"
"net/http"
"path"
"github.com/swaggest/rest/request"
"github.com/swaggest/rest/response"
"github.com/swaggest/usecase"
)
func ServeSiteFile(deps interface{}) usecase.Interactor {
type fileReq struct {
File string `path:"file"`
request.EmbeddedSetter
}
u := usecase.NewInteractor(func(ctx context.Context, in fileReq, out *response.EmbeddedSetter) error {
rw := out.ResponseWriter()
rw.Header().Set("Cache-Control", "max-age=31536000")
filePath := path.Join("site", in.File)
http.ServeFile(rw, in.Request(), filePath)
return nil
})
u.SetTags("Site")
return u
} |
Beta Was this translation helpful? Give feedback.
Hi, the easiest way to access those would be to embed (or use directly if you don't need extra parameters/headers)
request.EmbeddedSetter
andresponse.EmbeddedSetter
in your in/out.Please check an example.
https://github.com/vearutop/photo-blog/blob/v0.0.4/internal/usecase/serve_site_file.go