Big refacto

Signed-off-by: Pierre-Emmanuel Jacquier <pierre-emmanuel.jacquier@epitech.eu>
This commit is contained in:
Pierre-Emmanuel Jacquier
2019-02-26 20:24:26 +01:00
parent a7baba6289
commit 2ed7c2f0e6
4 changed files with 95 additions and 54 deletions

View File

@@ -6,6 +6,8 @@ import (
"os"
"strings"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/routes"
"github.com/jamesnetherton/m3u"
@@ -26,12 +28,15 @@ var rootCmd = &cobra.Command{
log.Fatal(err)
}
conf := routes.Configuration{
Hostname: viper.GetString("hostname"),
Port: viper.GetInt64("port"),
conf := &config.ProxyConfig{
Playlist: &playlist,
HostConfig: &config.HostConfiguration{
Hostname: viper.GetString("hostname"),
Port: viper.GetInt64("port"),
},
}
if e := routes.Serve(&playlist, conf); e != nil {
if e := routes.Serve(conf); e != nil {
log.Fatal(e)
}
},

16
pkg/config/config.go Normal file
View File

@@ -0,0 +1,16 @@
package config
import "github.com/jamesnetherton/m3u"
// HostConfiguration containt host infos
type HostConfiguration struct {
Hostname string
Port int64
}
// ProxyConfig Contain original m3u playlist and HostConfiguration,
// if track is not nil current track selected in playlist
type ProxyConfig struct {
Playlist *m3u.Playlist
HostConfig *HostConfiguration
}

39
pkg/m3u/m3u.go Normal file
View File

@@ -0,0 +1,39 @@
package m3u
import (
"fmt"
"net/url"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
)
// Marshall m3u.playlist struct to m3u file
func Marshall(p *m3u.Playlist, config *config.HostConfiguration) (string, error) {
result := "#EXTM3U\n"
for _, track := range p.Tracks {
result += "#EXTINF:"
result += fmt.Sprintf("%d ", track.Length)
for i := range track.Tags {
if i == len(track.Tags)-1 {
result += fmt.Sprintf("%s=%q,", track.Tags[i].Name, track.Tags[i].Value)
continue
}
result += fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value)
}
result += fmt.Sprintf("%s\n", track.Name)
oriURL, err := url.Parse(track.URI)
if err != nil {
return "", err
}
destURL, err := url.Parse(fmt.Sprintf("http://%s:%d%s", config.Hostname, config.Port, oriURL.RequestURI()))
if err != nil {
return "", err
}
result += fmt.Sprintf("%s\n", destURL.String())
}
return result, nil
}

View File

@@ -7,49 +7,54 @@ import (
"net/http"
"net/url"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
proxyM3U "github.com/pierre-emmanuelJ/iptv-proxy/pkg/m3u"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jamesnetherton/m3u"
)
type Configuration struct {
Hostname string
Port int64
}
type ProxyPlaylist struct {
playlist *m3u.Playlist
track *m3u.Track
conf *Configuration
type proxy struct {
*config.ProxyConfig
*m3u.Track
}
// Serve the pfinder api
func Serve(playlist *m3u.Playlist, conf Configuration) error {
func Serve(proxyConfig *config.ProxyConfig) error {
router := gin.Default()
router.Use(cors.Default())
Routes(playlist, conf, router.Group("/"))
Routes(proxyConfig, router.Group("/"))
return router.Run(fmt.Sprintf(":%d", conf.Port))
return router.Run(fmt.Sprintf(":%d", proxyConfig.HostConfig.Port))
}
// Routes adds the routes for the app to the RouterGroup r
func Routes(playlist *m3u.Playlist, conf Configuration, r *gin.RouterGroup) {
p := ProxyPlaylist{playlist, nil, &conf}
func Routes(proxyConfig *config.ProxyConfig, r *gin.RouterGroup) {
r.GET("/m3u", p.getM3U)
p := &proxy{
proxyConfig,
nil,
}
for i, track := range playlist.Tracks {
r.GET("/iptv.m3u", p.getM3U)
for i, track := range proxyConfig.Playlist.Tracks {
oriURL, err := url.Parse(track.URI)
if err != nil {
return
}
tmp := &ProxyPlaylist{playlist, &playlist.Tracks[i], &conf}
r.GET(oriURL.RequestURI(), tmp.reversProxy)
tmp := &proxy{
nil,
&proxyConfig.Playlist.Tracks[i],
}
r.GET(oriURL.RequestURI(), tmp.reverseProxy)
}
}
func (p *ProxyPlaylist) reversProxy(c *gin.Context) {
rpURL, err := url.Parse(p.track.URI)
func (p *proxy) reverseProxy(c *gin.Context) {
rpURL, err := url.Parse(p.Track.URI)
if err != nil {
log.Fatal(err)
}
@@ -66,35 +71,11 @@ func (p *ProxyPlaylist) reversProxy(c *gin.Context) {
})
}
func (p *ProxyPlaylist) getM3U(c *gin.Context) {
result := "#EXTM3U\n"
for _, track := range p.playlist.Tracks {
result += "#EXTINF:"
result += fmt.Sprintf("%d ", track.Length)
for i := range track.Tags {
if i == len(track.Tags)-1 {
result += fmt.Sprintf("%s=%q,", track.Tags[i].Name, track.Tags[i].Value)
continue
}
result += fmt.Sprintf("%s=%q ", track.Tags[i].Name, track.Tags[i].Value)
}
result += fmt.Sprintf("%s\n", track.Name)
oriURL, err := url.Parse(track.URI)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
destURL, err := url.Parse(fmt.Sprintf("http://%s:%d%s", p.conf.Hostname, p.conf.Port, oriURL.RequestURI()))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
result += fmt.Sprintf("%s\n", destURL.String())
func (p *proxy) getM3U(c *gin.Context) {
result, err := proxyM3U.Marshall(p.Playlist, p.HostConfig)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Data(http.StatusOK, "text/plain", []byte(result))
}