-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
130 lines (113 loc) · 2.89 KB
/
server.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
package toy
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/gofrs/uuid"
"github.com/sbogacz/going-serverless/02_separate_binaries/internal/httperrs"
)
// Server represents all of the config and clients
// needed to run our app
type Server struct {
store Store
cfg *Config
router *chi.Mux // HL
cancel chan struct{} // OMIT
}
// New tries to cerate a new instance of Service
func New(c *Config, s Store) *Server {
return &Server{
cfg: c,
store: s,
router: chi.NewRouter(),
cancel: make(chan struct{}),
}
}
// Start starts the server
func (s *Server) Start() {
s.router.Route("/blobs", func(r chi.Router) { // HL
r.Post("/", s.storeBlob)
r.Route("/{key}", func(r chi.Router) {
r.Get("/", s.getBlob)
r.Delete("/", s.deleteBlob)
})
})
h := &http.Server{ // OMIT
Addr: fmt.Sprintf(":%d", s.cfg.Port), // OMIT
ReadTimeout: 5 * time.Second, // OMIT
WriteTimeout: 5 * time.Second, // OMIT
Handler: s.router, // OMIT
} // OMIT
// OMIT
go func() { // OMIT
<-s.cancel // OMIT
_ = h.Shutdown(context.Background()) // OMIT
}() // OMIT
// OMIT
if err := h.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}
// Stop stops the server gracefully
func (s *Server) Stop() {
s.cancel <- struct{}{}
}
// Router exposes our chi Route externally
func (s *Server) Router() *chi.Mux {
return s.router
}
func (s *Server) storeBlob(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
b, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "couldn't parse the request body", http.StatusBadRequest)
return
}
// create key
u, err := uuid.NewV4()
if err != nil {
http.Error(w, "failed to generate key", http.StatusInternalServerError)
return
}
key := u.String()
if err := s.store.Set(context.TODO(), key, string(b)); err != nil {
http.Error(w, "failed to store", httperrs.StatusCode(err))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(key))
}
func (s *Server) getBlob(w http.ResponseWriter, req *http.Request) {
key := chi.URLParam(req, "key")
_, err := uuid.FromString(key)
if err != nil {
http.Error(w, "invalid key", http.StatusBadRequest)
return
}
data, err := s.store.Get(context.TODO(), key)
if err != nil {
http.Error(w, "failed to retrieve object", httperrs.StatusCode(err))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(data))
}
func (s *Server) deleteBlob(w http.ResponseWriter, req *http.Request) {
key := chi.URLParam(req, "key")
_, err := uuid.FromString(key)
if err != nil {
http.Error(w, "invalid key", http.StatusBadRequest)
return
}
if err := s.store.Del(context.TODO(), key); err != nil {
http.Error(w, "failed to delete object", httperrs.StatusCode(err))
return
}
w.WriteHeader(http.StatusNoContent)
}