Skip to content

Commit

Permalink
Add server package
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Jan 2, 2022
1 parent 16315c0 commit 142b0e0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 68 deletions.
45 changes: 15 additions & 30 deletions cmd/podsync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,13 @@ 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 Log struct {
// Filename to write the log to (instead of stdout)
Filename string `toml:"filename"`
// MaxSize is the maximum size of the log file in MB
MaxSize int `toml:"max_size"`
// MaxBackups is the maximum number of log file backups to keep after rotation
MaxBackups int `toml:"max_backups"`
// MaxAge is the maximum number of days to keep the logs for
MaxAge int `toml:"max_age"`
// Compress old backups
Compress bool `toml:"compress"`
}

type Config struct {
// Server is the web server configuration
Server ServerConfig `toml:"server"`
Server server.Config `toml:"server"`
// Log is the optional logging configuration
Log Log `toml:"log"`
// Database configuration
Expand All @@ -61,6 +33,19 @@ type Config struct {
Downloader ytdl.Config `toml:"downloader"`
}

type Log struct {
// Filename to write the log to (instead of stdout)
Filename string `toml:"filename"`
// MaxSize is the maximum size of the log file in MB
MaxSize int `toml:"max_size"`
// MaxBackups is the maximum number of log file backups to keep after rotation
MaxBackups int `toml:"max_backups"`
// MaxAge is the maximum number of days to keep the logs for
MaxAge int `toml:"max_age"`
// Compress old backups
Compress bool `toml:"compress"`
}

// LoadConfig loads TOML configuration from a file path
func LoadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
Expand Down
3 changes: 2 additions & 1 deletion cmd/podsync/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/podsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 0 additions & 36 deletions cmd/podsync/server.go

This file was deleted.

52 changes: 52 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 142b0e0

Please sign in to comment.