-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
200 lines (170 loc) · 5.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"flag"
"log"
"net/http"
"time"
"github.com/a-h/templ"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/facebookgo/flagenv"
"github.com/google/uuid"
_ "github.com/joho/godotenv/autoload"
)
var (
bind = flag.String("bind", ":9090", "The address to bind the server to")
bucket = flag.String("bucket-name", "", "The bucket where images are stored")
)
//go:generate go run github.com/a-h/templ/cmd/templ@latest generate
func main() {
flagenv.Parse()
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sdkConfig, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Printf("Couldn't load default configuration. Here's why: %v\n", err)
return
}
svc := s3.NewFromConfig(sdkConfig, func(o *s3.Options) {
o.BaseEndpoint = aws.String("https://fly.storage.tigris.dev")
o.Region = "auto"
o.UsePathStyle = false
})
presigner := &Client{PresignClient: s3.NewPresignClient(svc)}
mux := http.NewServeMux()
mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
var objects []string
resp, err := svc.ListObjectsV2(r.Context(), &s3.ListObjectsV2Input{
Bucket: bucket,
})
if err != nil {
log.Printf("can't list objects: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
for _, obj := range resp.Contents {
objects = append(objects, *obj.Key)
}
templ.Handler(
Simple("Simple upload", index(objects)),
).ServeHTTP(w, r)
})
mux.HandleFunc("GET /upload", func(w http.ResponseWriter, r *http.Request) {
templ.Handler(
Simple("Upload", upload()),
).ServeHTTP(w, r)
})
mux.HandleFunc("POST /upload", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(10 << 20); err != nil {
log.Printf("can't parse form: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
file, _, err := r.FormFile("image")
if err != nil {
log.Printf("can't get file: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
defer file.Close()
keyName := uuid.NewString()
_, err = svc.PutObject(ctx, &s3.PutObjectInput{
Bucket: bucket,
Key: &keyName,
Body: file,
})
if err != nil {
log.Printf("can't put object: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/img/"+keyName, http.StatusSeeOther)
})
mux.HandleFunc("GET /img/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if _, err := svc.HeadObject(ctx, &s3.HeadObjectInput{
Bucket: bucket,
Key: &id,
}); err != nil {
log.Printf("can't head object: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
psg, err := presigner.GetObject(ctx, *bucket, id, 6*time.Hour)
if err != nil {
log.Printf("can't get presigned request: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
psd, err := presigner.DeleteObject(ctx, *bucket, id, 6*time.Hour)
if err != nil {
log.Printf("can't get presigned request: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
templ.Handler(
Simple("Image "+id, viewImage(id, psg.URL, psd.URL)),
).ServeHTTP(w, r)
})
log.Printf("Listening on %v", *bind)
log.Fatal(http.ListenAndServe(*bind, mux))
}
// Client encapsulates the S3 SDK presign client and provides methods to presign requests.
type Client struct {
PresignClient *s3.PresignClient
}
// GetObject makes a presigned request that can be used to get an object from a bucket.
func (p *Client) GetObject(
ctx context.Context,
bucket string,
key string,
expiry time.Duration,
) (*v4.PresignedHTTPRequest, error) {
request, err := p.PresignClient.PresignGetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}, func(opts *s3.PresignOptions) {
opts.Expires = expiry
})
if err != nil {
log.Printf("Couldn't get a presigned request to get %v:%v. Here's why: %v\n",
bucket, key, err)
}
return request, err
}
// PutObject makes a presigned request that can be used to put an object in a bucket.
func (p *Client) PutObject(
ctx context.Context,
bucket string,
object string,
expiry time.Duration,
) (*v4.PresignedHTTPRequest, error) {
request, err := p.PresignClient.PresignPutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
}, func(opts *s3.PresignOptions) {
opts.Expires = expiry
})
if err != nil {
log.Printf("Couldn't get a presigned request to put %v:%v. Here's why: %v\n",
bucket, object, err)
}
return request, err
}
// DeleteObject makes a presigned request that can be used to delete an object from a bucket.
func (p *Client) DeleteObject(ctx context.Context, bucket string, object string, expiry time.Duration) (*v4.PresignedHTTPRequest, error) {
request, err := p.PresignClient.PresignDeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
}, func(opts *s3.PresignOptions) {
opts.Expires = expiry
})
if err != nil {
log.Printf("Couldn't get a presigned request to delete object %v. Here's why: %v\n", object, err)
}
return request, err
}