Skip to content

Commit

Permalink
refactor settings to be config
Browse files Browse the repository at this point in the history
  • Loading branch information
rraymondgh committed Jan 22, 2025
1 parent 40cb78a commit bbb1977
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 100 deletions.
81 changes: 79 additions & 2 deletions internal/torznab/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,87 @@
package torznab

import (
"encoding/json"
"strings"

"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/database/search"
"go.uber.org/fx"
"go.uber.org/zap"
)

type Profile struct {
OrderBy search.TorrentContentOrderBy
OrderDirection search.OrderDirection
Tags []string
}

type Config struct {
Hostname *string
Profiles map[string]Profile
}

type UntypedConfig struct {
Hostname *string
Profiles map[string]map[string]string
}

func NewDefaultConfig() Config {
return Config{}
func NewDefaultUntypedConfig() UntypedConfig {
return UntypedConfig{}
}

type Params struct {
fx.In
UntypedConfig UntypedConfig
Log *zap.SugaredLogger
}

type Result struct {
fx.Out
Profiles lazy.Lazy[*Config]
}

// lazy validation of strongly typed config
func New(p Params) Result {
return Result{
Profiles: lazy.New[*Config](func() (*Config, error) {
// make sure default profile has been defined.
_, ok := p.UntypedConfig.Profiles[ProfileDefault]
if !ok {
p.UntypedConfig.Profiles[ProfileDefault] = make(map[string]string, 0)
}
config := Config{
Hostname: p.UntypedConfig.Hostname,
Profiles: make(map[string]Profile, len(p.UntypedConfig.Profiles)),
}
for name, profile := range p.UntypedConfig.Profiles {
orderbyRaw, ok := profile[ProfileItemOrderBy]
if !ok {
orderbyRaw = string(search.TorrentContentOrderByRelevance)
}
orderby, err := search.ParseTorrentContentOrderBy(orderbyRaw)
if err != nil {
return nil, err
}
dirRaw, ok := profile[ProfileItemOrderDirection]
if !ok {
dirRaw = string(search.OrderDirectionDescending)
}
dir, err := search.ParseOrderDirection(dirRaw)
if err != nil {
return nil, err
}
tags := make([]string, 0)
csvTags, ok := profile[ProfileItemTags]
if ok && len(csvTags) > 0 {
tags = strings.Split(csvTags, ",")
}
config.Profiles[name] = Profile{OrderBy: orderby, OrderDirection: dir, Tags: tags}

}
log, _ := json.MarshalIndent(config, "", " ")
p.Log.Infof("torznab profiles:\n%s\n", log)
return &config, nil
}),
}
}
30 changes: 14 additions & 16 deletions internal/torznab/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"github.com/bitmagnet-io/bitmagnet/internal/torznab"
"github.com/bitmagnet-io/bitmagnet/internal/torznab/settings"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
)

type Params struct {
fx.In
Client lazy.Lazy[torznab.Client]
Settings lazy.Lazy[*settings.Settings]
Client lazy.Lazy[torznab.Client]
Config lazy.Lazy[*torznab.Config]
}

type Result struct {
Expand All @@ -29,25 +28,24 @@ type Result struct {
func New(p Params) Result {
return Result{
Option: builder{
client: p.Client,
settings: p.Settings,
client: p.Client,
config: p.Config,
},
}
}

type builder struct {
client lazy.Lazy[torznab.Client]
settings lazy.Lazy[*settings.Settings]
client lazy.Lazy[torznab.Client]
config lazy.Lazy[*torznab.Config]
}

func (builder) Key() string {
return "torznab"
}

type torznabworker struct {
client torznab.Client
// settings *settings.Settings
profile settings.Profile
client torznab.Client
profile torznab.Profile
hostname *string
}

Expand Down Expand Up @@ -170,12 +168,12 @@ func (w torznabworker) get(c *gin.Context) {

}

func (w torznabworker) getDefault(profile settings.Profile) gin.HandlerFunc {
func (w torznabworker) getDefault(profile torznab.Profile) gin.HandlerFunc {
w.profile = profile
return gin.HandlerFunc(w.get)
}

func (w torznabworker) getWithProfile(profiles map[string]settings.Profile) gin.HandlerFunc {
func (w torznabworker) getWithProfile(profiles map[string]torznab.Profile) gin.HandlerFunc {
handler := func(c *gin.Context) {
profileName := c.Param("profile")
profile, ok := profiles[profileName]
Expand All @@ -198,16 +196,16 @@ func (b builder) Apply(e *gin.Engine) error {
if err != nil {
return err
}
settings, err := b.settings.Get()
config, err := b.config.Get()
if err != nil {
return err
}
worker := torznabworker{
client: client,
hostname: settings.Hostname,
hostname: config.Hostname,
}

e.GET("/torznab/api/*any", worker.getDefault(settings.Profiles[torznab.ProfileDefault]))
e.GET("/torznab/:profile/*any", worker.getWithProfile(settings.Profiles))
e.GET("/torznab/api/*any", worker.getDefault(config.Profiles[torznab.ProfileDefault]))
e.GET("/torznab/:profile/*any", worker.getWithProfile(config.Profiles))
return nil
}
79 changes: 0 additions & 79 deletions internal/torznab/settings/settings.go

This file was deleted.

5 changes: 2 additions & 3 deletions internal/torznab/torznabfx/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (
"github.com/bitmagnet-io/bitmagnet/internal/torznab"
"github.com/bitmagnet-io/bitmagnet/internal/torznab/adapter"
"github.com/bitmagnet-io/bitmagnet/internal/torznab/httpserver"
"github.com/bitmagnet-io/bitmagnet/internal/torznab/settings"
"go.uber.org/fx"
)

func New() fx.Option {
return fx.Module(
"torznab",
configfx.NewConfigModule[torznab.Config]("torznab", torznab.NewDefaultConfig()),
configfx.NewConfigModule[torznab.UntypedConfig]("torznab", torznab.NewDefaultUntypedConfig()),
fx.Provide(
adapter.New,
settings.New,
torznab.New,
httpserver.New,
),
)
Expand Down

0 comments on commit bbb1977

Please sign in to comment.