~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.


64dabe3a Peter John

tag: v1.3.1

v1.3.1

3 years ago
Fix file download
Files changed (2) hide show
  1. db.go +70 -0
  2. http.go +6 -5
db.go ADDED
@@ -0,0 +1,70 @@
1
+ package gromer
2
+
3
+ import (
4
+ "context"
5
+ "database/sql"
6
+ )
7
+
8
+ type SQLInterface[T any] interface {
9
+ SqlOne(ctx context.Context, query string, args ...any) (*T, error)
10
+ SqlMany(ctx context.Context, query string, args ...any) ([]*T, error)
11
+ SqlExecute(ctx context.Context, query string, args ...any) error
12
+ }
13
+
14
+ type DBSQLInterface[T any] struct {
15
+ db *sql.DB
16
+ t T
17
+ }
18
+
19
+ func (p *DBSQLInterface[T]) SqlOne(ctx context.Context, query string, args ...any) (*T, error) {
20
+ conn, err := p.db.Conn(ctx)
21
+ if err != nil {
22
+ return nil, err
23
+ }
24
+ rows, err := conn.QueryContext(ctx, query, args)
25
+ if err != nil {
26
+ return nil, err
27
+ }
28
+ defer rows.Close()
29
+ // cols, _ := rows.Columns()
30
+ for rows.Next() {
31
+ // rows.Scan()
32
+ // for _, c := range cols {
33
+ // if rows.Err()
34
+ // }
35
+ }
36
+
37
+ // row.Scan()
38
+ // switch {
39
+ // case err == sql.ErrNoRows:
40
+ // log.Fatalf("no user with id %d", id)
41
+ // case err != nil:
42
+ // log.Fatal(err)
43
+ // default:
44
+ // log.Printf("username is %s\n", username)
45
+ // }
46
+ return nil, nil
47
+ }
48
+
49
+ func (p *DBSQLInterface[T]) SqlMany(ctx context.Context, query string, args ...any) ([]*T, error) {
50
+ return nil, nil
51
+ }
52
+
53
+ func (p *DBSQLInterface[T]) SqlExecute(ctx context.Context, query string, args ...any) error {
54
+ return nil
55
+ }
56
+
57
+ // type User struct {
58
+ // }
59
+
60
+ // func UsersTable(ctx context.Context) SQLInterface[User] {
61
+ // return &DBSQLInterface[User]{t: User{}}
62
+ // }
63
+
64
+ // func GetNote(ctx context.Context, id string) (*User, error) {
65
+ // return UsersTable(ctx).SqlOne(ctx, "select * from users where id = :id", id)
66
+ // }
67
+
68
+ // func GetNotes2(ctx context.Context) ([]*User, error) {
69
+ // return UsersTable(ctx).SqlMany(ctx, "select * from users")
70
+ // }
http.go CHANGED
@@ -45,8 +45,9 @@ var (
45
45
  type StatusComponent func(c *gsx.Context, status int, err error) []*gsx.Tag
46
46
 
47
47
  type File struct {
48
- Name string
48
+ Name string
49
+ ContentType string
49
- Data *bytes.Buffer
50
+ Data *bytes.Buffer
50
51
  }
51
52
 
52
53
  func init() {
@@ -211,9 +212,9 @@ func PerformRequest(route string, h interface{}, c interface{}, w http.ResponseW
211
212
  return
212
213
  }
213
214
  if file, ok := response.(*File); ok {
214
- w.Header().Set("Content-Type", "application/octet-stream")
215
+ w.Header().Set("Content-Type", file.ContentType)
216
+ w.Header().Set("Content-Length", fmt.Sprintf("%d", file.Data.Len()))
215
- w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename=%s`, file.Name))
217
+ w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, file.Name))
216
- w.WriteHeader(responseStatus)
217
218
  w.Write(file.Data.Bytes())
218
219
  return
219
220
  }