~repos /gromer

#golang#htmx#ssr

git clone https://pyrossh.dev/repos/gromer.git

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


ade61a29 Peter John

tag: v1.3.0

v1.3.0

3 years ago
Support File downloads
Files changed (1) hide show
  1. http.go +13 -0
http.go CHANGED
@@ -1,6 +1,7 @@
1
1
  package gromer
2
2
 
3
3
  import (
4
+ "bytes"
4
5
  "context"
5
6
  "crypto/md5"
6
7
  "embed"
@@ -43,6 +44,11 @@ var (
43
44
 
44
45
  type StatusComponent func(c *gsx.Context, status int, err error) []*gsx.Tag
45
46
 
47
+ type File struct {
48
+ Name string
49
+ Data *bytes.Buffer
50
+ }
51
+
46
52
  func init() {
47
53
  IsCloundRun = os.Getenv("K_REVISION") != ""
48
54
  info, _ = debug.ReadBuildInfo()
@@ -204,6 +210,13 @@ func PerformRequest(route string, h interface{}, c interface{}, w http.ResponseW
204
210
  RespondError(w, r, responseStatus, eris.Wrap(responseError.(error), "Render failed"))
205
211
  return
206
212
  }
213
+ if file, ok := response.(*File); ok {
214
+ w.Header().Set("Content-Type", "application/octet-stream")
215
+ w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename=%s`, file.Name))
216
+ w.WriteHeader(responseStatus)
217
+ w.Write(file.Data.Bytes())
218
+ return
219
+ }
207
220
  if isJson {
208
221
  w.Header().Set("Content-Type", "application/json")
209
222
  w.WriteHeader(responseStatus)