-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
150 lines (120 loc) · 4.44 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package request
import (
"errors"
"net/http"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/code-payments/code-server/pkg/code/common"
)
const (
v1PathPrefix = "/v1"
v1CreateIntentPath = v1PathPrefix + "/createIntent"
v1GetStatusPath = v1PathPrefix + "/getStatus"
v1GetUserIdPath = v1PathPrefix + "/getUserId"
contentTypeHeaderName = "content-type"
jsonContentTypeHeaderValue = "application/json"
)
type Server struct {
log *logrus.Entry
cc *grpc.ClientConn
}
func NewRequestServer(cc *grpc.ClientConn) *Server {
return &Server{
log: logrus.StandardLogger().WithField("type", "request/server"),
cc: cc,
}
}
func (s *Server) createIntentHandler(path string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := s.log.WithField("path", path)
statusCode, body := func() (int, GenericApiResponseBody) {
ctx := r.Context()
if r.Method != http.MethodPost {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(errors.New("http post expected"))
}
model, err := newTrustlessRequestFromHttpContext(r)
if err != nil {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(err)
}
err = s.createTrustlessRequest(ctx, model)
if err != nil {
log.WithError(err).Warn("failure creating request")
statusCode, err := HandleGrpcErrorInWebContext(w, err)
return statusCode, NewGenericApiFailureResponseBody(err)
}
return http.StatusOK, NewGenericApiSuccessResponseBody()
}()
w.Header().Set(contentTypeHeaderName, jsonContentTypeHeaderValue)
w.WriteHeader(statusCode)
w.Write([]byte(body.ToString()))
}
}
// todo: migrate to a POST variant
func (s *Server) getStatusHandler(path string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := s.log.WithField("path", path)
statusCode, body := func() (int, GenericApiResponseBody) {
ctx := r.Context()
if r.Method != http.MethodGet {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(errors.New("http get expected"))
}
intentIdQueryParam := r.URL.Query()["intent"]
if len(intentIdQueryParam) < 1 {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(errors.New("intent query parameter missing"))
}
intentId, err := common.NewAccountFromPublicKeyString(intentIdQueryParam[0])
if err != nil {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(errors.New("intent id is not a public key"))
}
log = log.WithField("intent", intentId.PublicKey().ToBase58())
res, err := s.getIntentStatus(ctx, intentId)
if err != nil {
log.WithError(err).Warn("failure getting intent status")
statusCode, err := HandleGrpcErrorInWebContext(w, err)
return statusCode, NewGenericApiFailureResponseBody(err)
}
respBody := NewGenericApiSuccessResponseBody()
respBody["status"] = res.Status
return http.StatusOK, respBody
}()
w.Header().Set(contentTypeHeaderName, jsonContentTypeHeaderValue)
w.WriteHeader(statusCode)
w.Write([]byte(body.ToString()))
}
}
func (s *Server) getUserIdHandler(path string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := s.log.WithField("path", path)
statusCode, body := func() (int, GenericApiResponseBody) {
ctx := r.Context()
if r.Method != http.MethodPost {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(errors.New("http post expected"))
}
protoReq, err := newGetLoggedInUserIdRequestFromHttpContext(r)
if err != nil {
return http.StatusBadRequest, NewGenericApiFailureResponseBody(err)
}
res, err := s.getUserId(ctx, protoReq)
if err != nil {
log.WithError(err).Warn("failure getting user")
statusCode, err := HandleGrpcErrorInWebContext(w, err)
return statusCode, NewGenericApiFailureResponseBody(err)
}
respBody := NewGenericApiSuccessResponseBody()
if len(res.User) > 0 {
respBody["user"] = res.User
}
return http.StatusOK, respBody
}()
w.Header().Set(contentTypeHeaderName, jsonContentTypeHeaderValue)
w.WriteHeader(statusCode)
w.Write([]byte(body.ToString()))
}
}
func (s *Server) GetHandlers() map[string]http.HandlerFunc {
return map[string]http.HandlerFunc{
v1CreateIntentPath: s.createIntentHandler(v1CreateIntentPath),
v1GetStatusPath: s.getStatusHandler(v1GetStatusPath),
v1GetUserIdPath: s.getUserIdHandler(v1GetUserIdPath),
}
}