From 2ed7c2f0e6aa8d3d5cb043e684c31a858b69d84f Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier Date: Tue, 26 Feb 2019 20:24:26 +0100 Subject: [PATCH] Big refacto Signed-off-by: Pierre-Emmanuel Jacquier --- cmd/root.go | 13 ++++--- pkg/config/config.go | 16 +++++++++ pkg/m3u/m3u.go | 39 +++++++++++++++++++++ pkg/routes/routes.go | 81 +++++++++++++++++--------------------------- 4 files changed, 95 insertions(+), 54 deletions(-) create mode 100644 pkg/config/config.go create mode 100644 pkg/m3u/m3u.go diff --git a/cmd/root.go b/cmd/root.go index aa376c4..375f1e7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) } }, diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..7b2fbe7 --- /dev/null +++ b/pkg/config/config.go @@ -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 +} diff --git a/pkg/m3u/m3u.go b/pkg/m3u/m3u.go new file mode 100644 index 0000000..425ac62 --- /dev/null +++ b/pkg/m3u/m3u.go @@ -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 +} diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 4725337..fef4639 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -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)) }