-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
httpserver.go
147 lines (140 loc) · 3.51 KB
/
httpserver.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
package httpserver
import (
"errors"
"fmt"
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/httpserver"
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"github.com/bitmagnet-io/bitmagnet/internal/torznab"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"strconv"
"strings"
)
type Params struct {
fx.In
Client lazy.Lazy[torznab.Client]
}
type Result struct {
fx.Out
Option httpserver.Option `group:"http_server_options"`
}
func New(p Params) Result {
return Result{
Option: builder{
client: p.Client,
},
}
}
type builder struct {
client lazy.Lazy[torznab.Client]
}
func (builder) Key() string {
return "torznab"
}
func (b builder) Apply(e *gin.Engine) error {
client, err := b.client.Get()
if err != nil {
return err
}
e.GET("/torznab/*any", func(c *gin.Context) {
writeInternalError := func(err error) {
_ = c.AbortWithError(500, err)
_, _ = c.Writer.WriteString(err.Error() + "\n")
}
writeXml := func(obj torznab.Xmler) {
body, err := obj.Xml()
if err != nil {
writeInternalError(fmt.Errorf("failed to encode xml: %w", err))
return
}
c.Status(200)
c.Header("Content-Type", "application/xml; charset=utf-8")
_, _ = c.Writer.Write(body)
}
writeErr := func(err error) {
torznabErr := &torznab.Error{}
if ok := errors.As(err, torznabErr); ok {
writeXml(torznabErr)
} else {
writeInternalError(err)
}
}
tp := c.Query(torznab.ParamType)
if tp == "" {
writeErr(torznab.Error{
Code: 200,
Description: fmt.Sprintf("missing parameter (%s)", torznab.ParamType),
})
return
}
if tp == torznab.FunctionCaps {
caps, capsErr := client.Caps(c)
if capsErr != nil {
writeErr(fmt.Errorf("failed to execute caps: %w", capsErr))
return
}
writeXml(caps)
return
}
var cats []int
for _, csvCat := range c.QueryArray(torznab.ParamCat) {
for _, cat := range strings.Split(csvCat, ",") {
if intCat, err := strconv.Atoi(cat); err == nil {
cats = append(cats, intCat)
}
}
}
imdbId := model.NullString{}
if qImdbId := c.Query(torznab.ParamImdbId); qImdbId != "" {
imdbId.Valid = true
imdbId.String = qImdbId
}
tmdbId := model.NullString{}
if qTmdbId := c.Query(torznab.ParamTmdbId); qTmdbId != "" {
tmdbId.Valid = true
tmdbId.String = qTmdbId
}
season := model.NullInt{}
episode := model.NullInt{}
if qSeason := c.Query(torznab.ParamSeason); qSeason != "" {
if intSeason, err := strconv.Atoi(qSeason); err == nil {
season.Valid = true
season.Int = intSeason
if qEpisode := c.Query(torznab.ParamEpisode); qEpisode != "" {
if intEpisode, err := strconv.Atoi(qEpisode); err == nil {
episode.Valid = true
episode.Int = intEpisode
}
}
}
}
limit := model.NullUint{}
if intLimit, limitErr := strconv.Atoi(c.Query(torznab.ParamLimit)); limitErr == nil && intLimit > 0 {
limit.Valid = true
limit.Uint = uint(intLimit)
}
offset := model.NullUint{}
if intOffset, offsetErr := strconv.Atoi(c.Query(torznab.ParamOffset)); offsetErr == nil {
offset.Valid = true
offset.Uint = uint(intOffset)
}
result, searchErr := client.Search(c, torznab.SearchRequest{
Query: c.Query(torznab.ParamQuery),
Type: tp,
Cats: cats,
ImdbId: imdbId,
TmdbId: tmdbId,
Season: season,
Episode: episode,
Limit: limit,
Offset: offset,
})
if searchErr != nil {
writeErr(fmt.Errorf("failed to search: %w", searchErr))
return
}
writeXml(result)
})
return nil
}