Feature: Save releases (#36)

* chore: tidy deps

* refactor: database migration

* refactor: store release

* refactor: save release

* chore: add packages

* feat(web): show stats and recent releases

* refactor: simply filter struct

* feat: add eventbus

* chore: cleanup logging

* chore: update packages
This commit is contained in:
Ludvig Lundgren 2021-11-24 23:18:12 +01:00 committed by GitHub
parent d22dd2fe84
commit 7177e48c02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 5859 additions and 3328 deletions

90
internal/http/release.go Normal file
View file

@ -0,0 +1,90 @@
package http
import (
"context"
"net/http"
"strconv"
"github.com/autobrr/autobrr/internal/domain"
"github.com/go-chi/chi"
)
type releaseService interface {
Find(ctx context.Context, query domain.QueryParams) (res []domain.Release, nextCursor int64, err error)
Stats(ctx context.Context) (*domain.ReleaseStats, error)
}
type releaseHandler struct {
encoder encoder
service releaseService
}
func newReleaseHandler(encoder encoder, service releaseService) *releaseHandler {
return &releaseHandler{
encoder: encoder,
service: service,
}
}
func (h releaseHandler) Routes(r chi.Router) {
r.Get("/", h.findReleases)
r.Get("/stats", h.getStats)
}
func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
limitP := r.URL.Query().Get("limit")
limit, err := strconv.Atoi(limitP)
if err != nil && limitP != "" {
h.encoder.StatusResponse(r.Context(), w, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "limit parameter is invalid",
}, http.StatusBadRequest)
}
if limit == 0 {
limit = 20
}
cursorP := r.URL.Query().Get("cursor")
cursor, err := strconv.Atoi(cursorP)
if err != nil && cursorP != "" {
h.encoder.StatusResponse(r.Context(), w, map[string]interface{}{
"code": "BAD_REQUEST_PARAMS",
"message": "cursor parameter is invalid",
}, http.StatusBadRequest)
}
query := domain.QueryParams{
Limit: uint64(limit),
Cursor: uint64(cursor),
Sort: nil,
//Filter: "",
}
releases, nextCursor, err := h.service.Find(r.Context(), query)
if err != nil {
h.encoder.StatusNotFound(r.Context(), w)
return
}
ret := struct {
Data []domain.Release `json:"data"`
NextCursor int64 `json:"next_cursor"`
}{
Data: releases,
NextCursor: nextCursor,
}
h.encoder.StatusResponse(r.Context(), w, ret, http.StatusOK)
}
func (h releaseHandler) getStats(w http.ResponseWriter, r *http.Request) {
stats, err := h.service.Stats(r.Context())
if err != nil {
h.encoder.StatusNotFound(r.Context(), w)
return
}
h.encoder.StatusResponse(r.Context(), w, stats, http.StatusOK)
}

View file

@ -25,9 +25,10 @@ type Server struct {
filterService filterService
indexerService indexerService
ircService ircService
releaseService releaseService
}
func NewServer(sse *sse.Server, address string, baseUrl string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService) Server {
func NewServer(sse *sse.Server, address string, baseUrl string, actionService actionService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService, releaseSvc releaseService) Server {
return Server{
sse: sse,
address: address,
@ -39,6 +40,7 @@ func NewServer(sse *sse.Server, address string, baseUrl string, actionService ac
filterService: filterSvc,
indexerService: indexerSvc,
ircService: ircSvc,
releaseService: releaseSvc,
}
}
@ -94,6 +96,7 @@ func (s Server) Handler() http.Handler {
r.Route("/filters", newFilterHandler(encoder, s.filterService).Routes)
r.Route("/irc", newIrcHandler(encoder, s.ircService).Routes)
r.Route("/indexer", newIndexerHandler(encoder, s.indexerService, s.ircService).Routes)
r.Route("/release", newReleaseHandler(encoder, s.releaseService).Routes)
r.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
@ -105,7 +108,7 @@ func (s Server) Handler() http.Handler {
"X-Accel-Buffering": "no",
}
s.sse.HTTPHandler(w, r)
s.sse.ServeHTTP(w, r)
})
})
})