-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (72 loc) · 2.16 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
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func main() {
http.HandleFunc("GET /_/health", healthHandler)
http.HandleFunc("POST /", uploadHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("OK"))
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
if r.Header.Get("Content-Type") != "application/json" {
slog.Error("Invalid content type", slog.String("content-type", r.Header.Get("Content-Type")))
http.Error(w, "Invalid content type", http.StatusUnsupportedMediaType)
return
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(os.Getenv("AWS_REGION")),
})
if err != nil {
slog.Error("Failed to create AWS session", slog.Any("error", err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
uploader := s3manager.NewUploader(sess)
bucket := os.Getenv("S3_BUCKET")
prefix := "/"
if p := os.Getenv("S3_KEY_PREFIX"); p != "" {
prefix = p
}
ts := time.Now().Format("2006-01-02-15:04:05.000")
id, err := generateRandomString(8)
if err != nil {
slog.Error("Failed generating random ID", slog.Any("error", err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
key := fmt.Sprintf("%s%s.%s.json", prefix, ts, id)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: io.NopCloser(r.Body),
})
if err != nil {
slog.Error("Failed to upload to S3", slog.Any("error", err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
slog.Info("Successfully uploaded", slog.String("bucket", bucket), slog.String("key", key))
}
func generateRandomString(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}