forked from tellytv/telly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
162 lines (130 loc) · 3.76 KB
/
routes.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
package main
import (
"encoding/base64"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
ssdp "github.com/koron/go-ssdp"
"github.com/sirupsen/logrus"
ginprometheus "github.com/zsais/go-gin-prometheus"
)
func serve(opts config) {
discoveryData := opts.DiscoveryData()
log.Debugln("creating device xml")
upnp := discoveryData.UPNP()
log.Debugln("creating webserver routes")
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
if opts.LogRequests {
router.Use(ginrus())
}
p := ginprometheus.NewPrometheus("http")
p.Use(router)
router.GET("/", deviceXML(upnp))
router.GET("/discover.json", discovery(discoveryData))
router.GET("/lineup_status.json", lineupStatus(LineupStatus{
ScanInProgress: 0,
ScanPossible: 1,
Source: "Cable",
SourceList: []string{"Cable"},
}))
router.GET("/lineup.post", func(c *gin.Context) {
c.AbortWithStatus(http.StatusNotImplemented)
})
router.GET("/device.xml", deviceXML(upnp))
router.GET("/lineup.json", lineup(opts.lineup))
router.GET("/stream/:channelID", stream)
if opts.SSDP {
log.Debugln("advertising telly service on network via UPNP/SSDP")
if ssdpErr := setupSSDP(opts.BaseAddress.String(), opts.FriendlyName, opts.DeviceUUID); ssdpErr != nil {
log.WithError(ssdpErr).Errorln("telly cannot advertise over ssdp")
}
}
log.Infof("Listening and serving HTTP on %s", opts.ListenAddress)
if err := router.Run(opts.ListenAddress.String()); err != nil {
log.WithError(err).Panicln("Error starting up web server")
}
}
func deviceXML(deviceXML UPNP) gin.HandlerFunc {
return func(c *gin.Context) {
c.XML(http.StatusOK, deviceXML)
}
}
func discovery(data DiscoveryData) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, data)
}
}
func lineupStatus(status LineupStatus) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, status)
}
}
func lineup(lineup []LineupItem) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, lineup)
}
}
func stream(c *gin.Context) {
channelID := c.Param("channelID")
log.Debugf("Parsing URI %s to %s", c.Request.RequestURI, channelID)
decodedStreamURI, decodeErr := base64.StdEncoding.DecodeString(channelID)
if decodeErr != nil {
log.WithError(decodeErr).Errorf("Invalid base64: %s", channelID)
c.AbortWithError(http.StatusBadRequest, decodeErr) // nolint: errcheck
return
}
log.Debugln("Redirecting to:", string(decodedStreamURI))
c.Redirect(http.StatusMovedPermanently, string(decodedStreamURI))
}
func ginrus() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
// some evil middlewares modify this values
path := c.Request.URL.Path
c.Next()
end := time.Now()
latency := end.Sub(start)
end = end.UTC()
logFields := logrus.Fields{
"status": c.Writer.Status(),
"method": c.Request.Method,
"path": path,
"ipAddress": c.ClientIP(),
"latency": latency,
"userAgent": c.Request.UserAgent(),
"time": end.Format(time.RFC3339),
}
entry := log.WithFields(logFields)
if len(c.Errors) > 0 {
// Append error field if this is an erroneous request.
entry.Error(c.Errors.String())
} else {
entry.Info()
}
}
}
func setupSSDP(baseAddress, deviceName, deviceUUID string) error {
log.Debugf("Advertising telly as %s (%s)", deviceName, deviceUUID)
adv, err := ssdp.Advertise(
"upnp:rootdevice",
fmt.Sprintf("uuid:%s::upnp:rootdevice", deviceUUID),
fmt.Sprintf("http://%s/device.xml", baseAddress),
deviceName,
1800)
if err != nil {
return err
}
go func(advertiser *ssdp.Advertiser) {
aliveTick := time.Tick(15 * time.Second)
for {
<-aliveTick
if err := advertiser.Alive(); err != nil {
log.WithError(err).Panicln("error when sending ssdp heartbeat")
}
}
}(adv)
return nil
}