-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (95 loc) · 2.8 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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/julienschmidt/httprouter"
)
func main() {
customResolver := func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{
URL: "http://localstack:4566",
HostnameImmutable: true,
SigningRegion: region,
}, nil
}
s := server{
router: httprouter.New(),
s3: s3.NewFromConfig(aws.Config{
Region: "us-east-1",
EndpointResolver: aws.EndpointResolverFunc(customResolver),
}),
}
s.routes()
log.Fatal(http.ListenAndServe(":8080", s.router))
}
type server struct {
router *httprouter.Router // route the requests
s3 *s3.Client // talk to s3
}
func (s *server) routes() {
s.router.GET("/greeting/:name", s.handleGetGreeting())
s.router.POST("/greeting", s.handleSetGreeting())
}
func (s *server) handleGetGreeting() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Request object with key :name under the bucket greetings in s3
input := &s3.GetObjectInput{
Bucket: aws.String("greetings"),
Key: aws.String(ps.ByName("name")),
}
obj, err := s.s3.GetObject(r.Context(), input)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Something went wrong: %s", err.Error())))
return
}
// Read the file
b, err := io.ReadAll(obj.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Something went wrong: %s", err.Error())))
return
}
// Write the file contents
w.Write(b)
}
}
func (s *server) handleSetGreeting() httprouter.Handle {
// Request is what we expect from the /greeting endpoint
type Request struct {
Name string `json:"name"`
}
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Read the body to bytes
b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Something went wrong: %s", err.Error())))
return
}
// Unmarshal the body into the Request struct
var req Request
if err := json.Unmarshal(b, &req); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Something went wrong: %s", err.Error())))
return
}
// Store the greeting under a key of name in the greetings bucket
input := &s3.PutObjectInput{
Bucket: aws.String("greetings"),
Key: aws.String(req.Name),
Body: strings.NewReader(fmt.Sprintf("Hello, %s!", req.Name)),
}
if _, err := s.s3.PutObject(r.Context(), input); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Something went wrong: %s", err.Error())))
return
}
}
}