Skip to content

Commit 9d8d09e

Browse files
committed
improve storage
1 parent 00e2987 commit 9d8d09e

File tree

7 files changed

+31
-42
lines changed

7 files changed

+31
-42
lines changed

.env.test

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
export PROXY_HTTP_PORT=8080
2-
export PROXY_IMGPROXY_ENDPOINT=http://127.0.0.1:8081
3-
export IMGPROXY_BIND=127.0.0.1:8081
4-
51
export AWS_ACCESS_KEY_ID=test
62
export AWS_SECRET_ACCESS_KEY=test
7-
export PROXY_S3_ENDPOINT=http://localstack:4566
8-
export PROXY_S3_BUCKET=test-bucket
9-
export PROXY_S3_REGION=us-east-1
10-
3+
export S3_ENDPOINT=http://localstack:4566
4+
export S3_BUCKET=test-bucket
5+
IMGPROXY_KEY='f6cc9ef7106fb8d0cf9038e678fb9e95a7e39791654e1290ef19ff33a5d8faae13d0f0e701988f6127d8d72b8826612b59f91038d343469db83f67cf6f2ad7bc'
6+
IMGPROXY_SALT='9a2c23412cd54bdedf131f9bd125ccb33f65c9ce3a670043a5857684e12cec1282ad34e7af3260aacc892b30b13fbd5c1b2f993382e3e213b7991d40455796b0'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env.dev

Dockerfile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,12 @@ COPY . .
1717
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o proxy .
1818

1919
# Final stage using official imgproxy
20-
FROM ghcr.io/imgproxy/imgproxy:latest
20+
FROM ghcr.io/imgproxy/imgproxy:v3.27.2
2121

2222
# Copy our proxy binary
2323
COPY --from=builder /app/proxy /usr/local/bin/
2424
COPY start_processes.sh /usr/local/bin/
2525

26-
# Environment variables
27-
ENV PROXY_S3_ENDPOINT=""
28-
ENV PROXY_S3_BUCKET=""
29-
ENV PROXY_S3_REGION=""
30-
ENV PROXY_HTTP_PORT=8080
31-
3226
EXPOSE 8080
3327

3428
# Start proxy (which will start tigris-proxy & imgproxy)

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
services:
42
localstack:
53
image: localstack/localstack:2.3

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
</head>
2828
<body>
2929
<div class="image-container">
30-
<img
30+
<img
3131
src="http://localhost:4566/test-bucket/f245f88252712c73d1d62b78a427869f"
32-
onerror="this.src = 'http://localhost:8080/_/rs:fill:1160:532:1/g:ce/wm:0.5:soea:10:10:0.2/aHR0cHM6Ly93d3cuem9vcGx1cy5jby51ay9tYWdhemluZS93cC1jb250ZW50L3VwbG9hZHMvMjAyMS8wMS9zdHJpcGVkLWdyZXkta2l0dGVuLTc2OHg1MTIuanBn';"
32+
onerror="this.src ='http://localhost:8080/_/rs:fill:1160:532:1/g:ce/wm:0.5:soea:10:10:0.2/aHR0cHM6Ly93d3cuem9vcGx1cy5jby51ay9tYWdhemluZS93cC1jb250ZW50L3VwbG9hZHMvMjAyMS8wMS9zdHJpcGVkLWdyZXkta2l0dGVuLTc2OHg1MTIuanBn';"
3333
/>
3434
</div>
3535
</body>

main.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,40 @@ import (
2020
)
2121

2222
type Config struct {
23-
S3Endpoint string
24-
S3Bucket string
25-
S3Region string
26-
ImgproxyEndpoint string
23+
S3Endpoint string
24+
S3Bucket string
25+
S3Folder string
26+
TigrisProxyBind string
2727
}
2828

2929
func main() {
3030
cfg := Config{
31-
S3Endpoint: os.Getenv("PROXY_S3_ENDPOINT"),
32-
S3Bucket: os.Getenv("PROXY_S3_BUCKET"),
33-
S3Region: os.Getenv("PROXY_S3_REGION"),
34-
ImgproxyEndpoint: os.Getenv("PROXY_IMGPROXY_ENDPOINT"),
31+
S3Endpoint: os.Getenv("S3_ENDPOINT"),
32+
S3Bucket: os.Getenv("S3_BUCKET"),
33+
S3Folder: os.Getenv("S3_FOLDER"),
34+
TigrisProxyBind: os.Getenv("IMGPROXY_BIND"),
3535
}
36-
if cfg.S3Endpoint == "" || cfg.S3Bucket == "" || cfg.S3Region == "" {
36+
if cfg.S3Endpoint == "" || cfg.S3Bucket == "" {
3737
slog.Error("Missing required environment variable(s)", "config", cfg)
3838
os.Exit(1)
3939
}
40-
if cfg.ImgproxyEndpoint == "" {
41-
cfg.ImgproxyEndpoint = "http://localhost:8080"
40+
if cfg.TigrisProxyBind == "" {
41+
cfg.TigrisProxyBind = ":8080"
4242
}
4343

44-
uploader := manager.NewUploader(initS3Client(cfg), func(u *manager.Uploader) {
45-
u.PartSize = 5 * 1024 * 1024
46-
u.BufferProvider = manager.NewBufferedReadSeekerWriteToPool(10 * 1024 * 1024)
47-
})
48-
4944
// Initialize the proxy
50-
target, err := url.Parse(cfg.ImgproxyEndpoint)
45+
target, err := url.Parse("http://127.0.0.1:8081")
5146
if err != nil {
52-
slog.Error("Failed to parse imgproxy endpoint", "error", err)
47+
slog.Error("Failed to parse imgproxy local endpoint", "error", err)
5348
os.Exit(1)
5449
}
5550
proxy := httputil.NewSingleHostReverseProxy(target)
5651

52+
uploader := manager.NewUploader(initS3Client(cfg), func(u *manager.Uploader) {
53+
u.PartSize = 5 * 1024 * 1024
54+
u.BufferProvider = manager.NewBufferedReadSeekerWriteToPool(10 * 1024 * 1024)
55+
})
56+
5757
proxy.ModifyResponse = func(resp *http.Response) error {
5858
if resp.StatusCode == http.StatusOK {
5959
var buf bytes.Buffer
@@ -74,7 +74,7 @@ func main() {
7474
proxy.ServeHTTP(w, r)
7575
})
7676

77-
if err := http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PROXY_HTTP_PORT")), nil); err != nil {
77+
if err := http.ListenAndServe(fmt.Sprintf("%s", cfg.TigrisProxyBind), nil); err != nil {
7878
slog.Error("Server failed", "error", err)
7979
}
8080
}
@@ -84,7 +84,7 @@ func uploadToS3(ctx context.Context, uploader *manager.Uploader, cfg Config, r i
8484

8585
_, err := uploader.Upload(ctx, &s3.PutObjectInput{
8686
Bucket: aws.String(cfg.S3Bucket),
87-
Key: aws.String(key),
87+
Key: aws.String(fmt.Sprintf("%s%s", cfg.S3Folder, key)),
8888
Body: r,
8989
})
9090

@@ -112,7 +112,7 @@ func initS3Client(cfg Config) *s3.Client {
112112

113113
svc := s3.NewFromConfig(sdkConfig, func(o *s3.Options) {
114114
o.BaseEndpoint = aws.String(cfg.S3Endpoint)
115-
o.Region = cfg.S3Region
115+
o.Region = "auto"
116116
o.UsePathStyle = true
117117
})
118118

start_processes.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Start the first process
4-
imgproxy &
4+
IMGPROXY_BIND=127.0.0.1:8081 imgproxy &
55

66
# Start the second process
77
proxy &
@@ -10,4 +10,4 @@ proxy &
1010
wait -n
1111

1212
# Exit with status of process that exited first
13-
exit $?
13+
exit $?

0 commit comments

Comments
 (0)