forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
254 lines (208 loc) · 7.08 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package web
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
log "github.com/sirupsen/logrus"
"github.com/GenesisKernel/go-genesis/packages/consts"
"github.com/GenesisKernel/go-genesis/tools/update_server/config"
"github.com/GenesisKernel/go-genesis/tools/update_server/crypto"
"github.com/GenesisKernel/go-genesis/tools/update_server/model"
"github.com/GenesisKernel/go-genesis/tools/update_server/storage"
"github.com/GenesisKernel/go-genesis/tools/update_server/web/middleware"
)
// Server is storing web dependencies
type Server struct {
Db storage.Engine
Conf *config.Config
Signer crypto.Signer
PublicKey []byte
}
// Run is running web server
func (s *Server) Run() error {
log.WithFields(log.Fields{"ip": s.Conf.Host, "port": s.Conf.Port}).Info("Starting server")
return http.ListenAndServe(s.Conf.Host+":"+s.Conf.Port, s.GetRoutes())
}
// GetRoutes returning all web server routes
func (s *Server) GetRoutes() *chi.Mux {
r := chi.NewRouter()
r.Route("/api/v1", func(r chi.Router) {
r.Route("/private", func(r chi.Router) {
r.Use(middleware.Auth(s.Conf.Login, s.Conf.Pass))
r.Route("/binary", func(r chi.Router) {
r.Post("/", s.addBinary)
r.Delete("/{os}/{arch}/{version}", s.removeBinary)
})
})
r.Route("/{os}/{arch}", func(r chi.Router) {
r.Get("/last", s.getLastBuildInfo)
r.Get("/versions", s.getVersions)
r.Route("/{version}", func(r chi.Router) {
r.Get("/", s.getBuildInfo)
r.Get("/binary", s.getBuild)
})
})
})
return r
}
func (s *Server) getLastBuildInfo(w http.ResponseWriter, r *http.Request) {
versions, err := s.Db.GetVersionsList()
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err}).Error("Retrieving versions list")
w.WriteHeader(http.StatusInternalServerError)
return
}
os := chi.URLParam(r, "os")
a := chi.URLParam(r, "arch")
lv, err := model.GetLastVersion(versions, os, a)
if err != nil {
s.HTTPError(w, r, http.StatusBadRequest, "Wrong os/arch params")
return
}
var ev model.Version
if lv == ev {
s.HTTPError(w, r, http.StatusNotFound, "Nothing here yet")
return
}
binary, err := s.Db.Get(model.Build{Version: lv})
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": lv.String()}).Error("Retrieving build")
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
binary.Body = []byte{}
s.JSON(w, r, BuildInfoResponse{Build: binary})
}
func (s *Server) getVersions(w http.ResponseWriter, r *http.Request) {
os := chi.URLParam(r, "os")
a := chi.URLParam(r, "arch")
versions, err := s.Db.GetVersionsList()
if err != nil {
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
var resp []BuildInfoResponse
vrs := model.VersionFilter(versions, os, a)
for _, vr := range vrs {
cb, err := s.Db.Get(model.Build{Version: vr})
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": vr.String()}).Error("Retrieving build")
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
cb.Body = []byte{}
resp = append(resp, BuildInfoResponse{Build: cb})
}
s.JSON(w, r, resp)
}
func (s *Server) getBuild(w http.ResponseWriter, r *http.Request) {
v := chi.URLParam(r, "version")
os := chi.URLParam(r, "os")
a := chi.URLParam(r, "arch")
rb := model.Build{Version: model.Version{Number: v, OS: os, Arch: a}}
b, err := s.Db.Get(rb)
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": rb.String()}).Error("Retrieving build")
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
if bytes.Equal(b.Body, []byte{}) {
s.HTTPError(w, r, http.StatusNotFound, "No such version")
return
}
var ev model.Version
if b.Version == ev {
s.HTTPError(w, r, http.StatusNotFound, "Nothing here yet")
return
}
w.Write(b.Body)
}
// BuildInfoResponse is same to model.Build but without encoding body to json (for build body see getBuild action)
type BuildInfoResponse struct {
model.Build
Body []byte `json:"body,omitempty"`
}
func (s *Server) getBuildInfo(w http.ResponseWriter, r *http.Request) {
v := chi.URLParam(r, "version")
os := chi.URLParam(r, "os")
a := chi.URLParam(r, "arch")
rb := model.Build{Version: model.Version{Number: v, OS: os, Arch: a}}
b, err := s.Db.Get(rb)
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": rb.String()}).Error("Retrieving build")
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
var ev model.Version
if b.Version == ev {
s.HTTPError(w, r, http.StatusNotFound, "Nothing here yet")
return
}
b.Body = []byte{}
s.JSON(w, r, BuildInfoResponse{Build: b})
}
func (s *Server) addBinary(w http.ResponseWriter, r *http.Request) {
var b model.Build
err := render.DecodeJSON(r.Body, &b)
if err != nil {
log.WithFields(log.Fields{"errType": consts.JSONUnmarshallError, "err": err, "body": r.Body}).Error("Unmarshalling body")
s.HTTPError(w, r, http.StatusBadRequest, "Problem with decoding json")
return
}
verified, err := s.Signer.CheckSign(b, s.PublicKey)
if err != nil || !verified {
log.Warn("Someone trying to upload wrong signed binary")
s.HTTPError(w, r, http.StatusBadRequest, "Wrong binary sign")
return
}
if !b.Validate() {
s.HTTPError(w, r, http.StatusBadRequest, fmt.Sprintf("Wrong os+arch, available systems list: %s", model.GetAvailableVersions()))
return
}
aeb, err := s.Db.Get(b)
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": b.String()}).Error("Retrieving build")
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
if aeb.Version == b.Version {
s.HTTPError(w, r, http.StatusBadRequest, "Version already exists")
return
}
err = s.Db.Add(b)
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": b.String()}).Error("Adding build")
s.HTTPError(w, r, http.StatusInternalServerError, "Database problems")
return
}
s.JSON(w, r, struct{}{})
}
func (s *Server) removeBinary(w http.ResponseWriter, r *http.Request) {
v := chi.URLParam(r, "version")
os := chi.URLParam(r, "os")
a := chi.URLParam(r, "arch")
rb := model.Build{Version: model.Version{Number: v, OS: os, Arch: a}}
err := s.Db.Delete(rb)
if err != nil {
log.WithFields(log.Fields{"errType": consts.DBError, "err": err, "version": rb.String()}).Error("Deleting build")
w.WriteHeader(http.StatusInternalServerError)
return
}
s.JSON(w, r, struct{}{})
}
func (s *Server) HTTPError(w http.ResponseWriter, r *http.Request, status int, error string) {
w.WriteHeader(status)
s.JSON(w, r, error)
}
func (s *Server) JSON(w http.ResponseWriter, r *http.Request, data interface{}) {
b, err := json.Marshal(data)
if err != nil {
log.WithFields(log.Fields{"errType": consts.JSONMarshallError, "err": err}).Error("Marshalling response data to JSON")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}