mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Add server package
This commit is contained in:
+15
-30
@@ -13,23 +13,24 @@ import (
|
||||
"github.com/mxpv/podsync/pkg/db"
|
||||
"github.com/mxpv/podsync/pkg/feed"
|
||||
"github.com/mxpv/podsync/pkg/model"
|
||||
"github.com/mxpv/podsync/pkg/server"
|
||||
"github.com/mxpv/podsync/pkg/ytdl"
|
||||
)
|
||||
|
||||
type ServerConfig struct {
|
||||
// Hostname to use for download links
|
||||
Hostname string `toml:"hostname"`
|
||||
// Port is a server port to listen to
|
||||
Port int `toml:"port"`
|
||||
// Bind a specific IP addresses for server
|
||||
// "*": bind all IP addresses which is default option
|
||||
// localhost or 127.0.0.1 bind a single IPv4 address
|
||||
BindAddress string `toml:"bind_address"`
|
||||
// Specify path for reverse proxy and only [A-Za-z0-9]
|
||||
Path string `toml:"path"`
|
||||
// DataDir is a path to a directory to keep XML feeds and downloaded episodes,
|
||||
// that will be available to user via web server for download.
|
||||
DataDir string `toml:"data_dir"`
|
||||
type Config struct {
|
||||
// Server is the web server configuration
|
||||
Server server.Config `toml:"server"`
|
||||
// Log is the optional logging configuration
|
||||
Log Log `toml:"log"`
|
||||
// Database configuration
|
||||
Database db.Config `toml:"database"`
|
||||
// Feeds is a list of feeds to host by this app.
|
||||
// ID will be used as feed ID in http://podsync.net/{FEED_ID}.xml
|
||||
Feeds map[string]*feed.Config
|
||||
// Tokens is API keys to use to access YouTube/Vimeo APIs.
|
||||
Tokens map[model.Provider][]string `toml:"tokens"`
|
||||
// Downloader (youtube-dl) configuration
|
||||
Downloader ytdl.Config `toml:"downloader"`
|
||||
}
|
||||
|
||||
type Log struct {
|
||||
@@ -45,22 +46,6 @@ type Log struct {
|
||||
Compress bool `toml:"compress"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// Server is the web server configuration
|
||||
Server ServerConfig `toml:"server"`
|
||||
// Log is the optional logging configuration
|
||||
Log Log `toml:"log"`
|
||||
// Database configuration
|
||||
Database db.Config `toml:"database"`
|
||||
// Feeds is a list of feeds to host by this app.
|
||||
// ID will be used as feed ID in http://podsync.net/{FEED_ID}.xml
|
||||
Feeds map[string]*feed.Config
|
||||
// Tokens is API keys to use to access YouTube/Vimeo APIs.
|
||||
Tokens map[model.Provider][]string `toml:"tokens"`
|
||||
// Downloader (youtube-dl) configuration
|
||||
Downloader ytdl.Config `toml:"downloader"`
|
||||
}
|
||||
|
||||
// LoadConfig loads TOML configuration from a file path
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mxpv/podsync/pkg/model"
|
||||
"github.com/mxpv/podsync/pkg/server"
|
||||
)
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
@@ -173,7 +174,7 @@ data_dir = "/data"
|
||||
|
||||
func TestDefaultHostname(t *testing.T) {
|
||||
cfg := Config{
|
||||
Server: ServerConfig{},
|
||||
Server: server.Config{},
|
||||
}
|
||||
|
||||
t.Run("empty hostname", func(t *testing.T) {
|
||||
|
||||
+2
-1
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/mxpv/podsync/pkg/feed"
|
||||
"github.com/mxpv/podsync/pkg/server"
|
||||
"github.com/robfig/cron/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -179,7 +180,7 @@ func main() {
|
||||
})
|
||||
|
||||
// Run web server
|
||||
srv := NewServer(cfg, storage)
|
||||
srv := server.New(cfg.Server, storage)
|
||||
|
||||
group.Go(func() error {
|
||||
log.Infof("running listener at %s", srv.Addr)
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
http.Server
|
||||
}
|
||||
|
||||
func NewServer(cfg *Config, storage http.FileSystem) *Server {
|
||||
port := cfg.Server.Port
|
||||
if port == 0 {
|
||||
port = 8080
|
||||
}
|
||||
|
||||
bindAddress := cfg.Server.BindAddress
|
||||
if bindAddress == "*" {
|
||||
bindAddress = ""
|
||||
}
|
||||
|
||||
srv := Server{}
|
||||
|
||||
srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port)
|
||||
log.Debugf("using address: %s:%s", bindAddress, srv.Addr)
|
||||
|
||||
fileServer := http.FileServer(storage)
|
||||
|
||||
log.Debugf("handle path: /%s", cfg.Server.Path)
|
||||
http.Handle(fmt.Sprintf("/%s", cfg.Server.Path), fileServer)
|
||||
|
||||
return &srv
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
http.Server
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// Hostname to use for download links
|
||||
Hostname string `toml:"hostname"`
|
||||
// Port is a server port to listen to
|
||||
Port int `toml:"port"`
|
||||
// Bind a specific IP addresses for server
|
||||
// "*": bind all IP addresses which is default option
|
||||
// localhost or 127.0.0.1 bind a single IPv4 address
|
||||
BindAddress string `toml:"bind_address"`
|
||||
// Specify path for reverse proxy and only [A-Za-z0-9]
|
||||
Path string `toml:"path"`
|
||||
// DataDir is a path to a directory to keep XML feeds and downloaded episodes,
|
||||
// that will be available to user via web server for download.
|
||||
DataDir string `toml:"data_dir"`
|
||||
}
|
||||
|
||||
func New(cfg Config, storage http.FileSystem) *Server {
|
||||
port := cfg.Port
|
||||
if port == 0 {
|
||||
port = 8080
|
||||
}
|
||||
|
||||
bindAddress := cfg.BindAddress
|
||||
if bindAddress == "*" {
|
||||
bindAddress = ""
|
||||
}
|
||||
|
||||
srv := Server{}
|
||||
|
||||
srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port)
|
||||
log.Debugf("using address: %s:%s", bindAddress, srv.Addr)
|
||||
|
||||
fileServer := http.FileServer(storage)
|
||||
|
||||
log.Debugf("handle path: /%s", cfg.Path)
|
||||
http.Handle(fmt.Sprintf("/%s", cfg.Path), fileServer)
|
||||
|
||||
return &srv
|
||||
}
|
||||
Reference in New Issue
Block a user