-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
update.go
48 lines (39 loc) · 997 Bytes
/
update.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
package http
import (
"context"
"github.com/SyncYomi/SyncYomi/pkg/version"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"net/http"
)
type updateService interface {
CheckUpdates(ctx context.Context)
GetLatestRelease(ctx context.Context) *version.Release
}
type updateHandler struct {
encoder encoder
service updateService
}
func newUpdateHandler(encoder encoder, service updateService) *updateHandler {
return &updateHandler{
encoder: encoder,
service: service,
}
}
func (h updateHandler) Routes(r chi.Router) {
r.Get("/latest", h.getLatest)
r.Get("/check", h.checkUpdates)
}
func (h updateHandler) getLatest(w http.ResponseWriter, r *http.Request) {
latest := h.service.GetLatestRelease(r.Context())
if latest != nil {
render.Status(r, http.StatusOK)
render.JSON(w, r, latest)
return
}
render.NoContent(w, r)
}
func (h updateHandler) checkUpdates(w http.ResponseWriter, r *http.Request) {
h.service.CheckUpdates(r.Context())
render.NoContent(w, r)
}