forked from brave/go-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
95 lines (84 loc) · 2.71 KB
/
controller.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
package controller
import (
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"github.com/brave-intl/bat-go/middleware"
"github.com/brave-intl/bat-go/utils/closers"
"github.com/go-chi/chi"
"github.com/odedlaz/go-sync/auth"
"github.com/odedlaz/go-sync/cache"
"github.com/odedlaz/go-sync/command"
"github.com/odedlaz/go-sync/datastore"
"github.com/odedlaz/go-sync/schema/protobuf/sync_pb"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
)
const (
payloadLimit10MB = 1024 * 1024 * 10
)
// SyncRouter add routers for command and auth endpoint requests.
func SyncRouter(cache *cache.Cache, datastore datastore.Datastore) chi.Router {
r := chi.NewRouter()
r.Method("POST", "/command/", middleware.InstrumentHandler("Command", Command(cache, datastore)))
return r
}
// Command handles GetUpdates and Commit requests from sync clients.
func Command(cache *cache.Cache, db datastore.Datastore) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Authorize
clientID, err := auth.Authorize(r)
if clientID == "" {
if err != nil {
log.Error().Err(err).Msg("Authorization failed")
}
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
reader := r.Body
// Create a gzip reader if needed.
if r.Header.Get("Content-Encoding") == "gzip" {
gr, err := gzip.NewReader(r.Body)
if err != nil {
log.Error().Err(err).Msg("Create gzip reader failed")
http.Error(w, "Create gzip reader failed", http.StatusInternalServerError)
return
}
defer closers.Panic(gr)
reader = gr
}
msg, err := ioutil.ReadAll(io.LimitReader(reader, payloadLimit10MB))
if err != nil {
log.Error().Err(err).Msg("Read request body failed")
http.Error(w, "Read request body error", http.StatusInternalServerError)
return
}
// Unmarshal into ClientToServerMessage
pb := &sync_pb.ClientToServerMessage{}
err = proto.Unmarshal(msg, pb)
if err != nil {
log.Error().Err(err).Msg("Unmarshall ClientToServerMessage failed")
http.Error(w, "Unmarshal error", http.StatusInternalServerError)
return
}
pbRsp := &sync_pb.ClientToServerResponse{}
err = command.HandleClientToServerMessage(cache, pb, pbRsp, db, clientID)
if err != nil {
log.Error().Err(err).Msg("Handle command message failed")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
out, err := proto.Marshal(pbRsp)
if err != nil {
log.Error().Err(err).Msg("Marshall ClientToServerResponse failed")
http.Error(w, "Marshal Error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(out)
if err != nil {
log.Error().Err(err).Msg("Write HTTP response body failed")
return
}
})
}