Add cmd option for quick start w/o config file

This commit is contained in:
Lawrence, Rendall
2022-11-18 18:28:19 +03:00
parent db671d3891
commit ef6d820c5b
7 changed files with 97 additions and 49 deletions
+28 -11
View File
@@ -20,14 +20,16 @@ import (
"github.com/sot-tech/mochi/pkg/metrics"
)
// Name - registered name of the frontend
const Name = "http"
var (
logger = log.NewLogger("frontend/http")
errTLSNotProvided = errors.New("tls certificate/key not provided")
errRoutesNotProvided = errors.New("routes not provided")
logger = log.NewLogger("frontend/http")
errTLSNotProvided = errors.New("tls certificate/key not provided")
)
func init() {
frontend.RegisterBuilder("http", NewFrontend)
frontend.RegisterBuilder(Name, NewFrontend)
}
// Config represents all configurable options for an HTTP BitTorrent Frontend
@@ -44,15 +46,17 @@ type Config struct {
ParseOptions
}
const defaultIdleTimeout = 30 * time.Second
const (
defaultIdleTimeout = 30 * time.Second
defaultAnnounceRoute = "/announce"
defaultScrapeRoute = "/scrape"
)
// Validate sanity checks values set in a config and returns a new config with
// default values replacing anything that is invalid.
func (cfg Config) Validate() (validCfg Config, err error) {
validCfg = cfg
if validCfg.ListenOptions, err = cfg.ListenOptions.Validate(); err != nil {
return
}
validCfg.ListenOptions = cfg.ListenOptions.Validate(false)
if cfg.UseTLS && (len(cfg.TLSCertPath) == 0 || len(cfg.TLSKeyPath) == 0) {
err = errTLSNotProvided
return
@@ -68,6 +72,22 @@ func (cfg Config) Validate() (validCfg Config, err error) {
Msg("falling back to default configuration")
}
}
if len(cfg.AnnounceRoutes) == 0 {
validCfg.AnnounceRoutes = []string{defaultAnnounceRoute}
logger.Warn().
Str("name", "AnnounceRoutes").
Strs("provided", cfg.AnnounceRoutes).
Strs("default", validCfg.AnnounceRoutes).
Msg("falling back to default configuration")
}
if len(cfg.ScrapeRoutes) == 0 {
validCfg.ScrapeRoutes = []string{defaultScrapeRoute}
logger.Warn().
Str("name", "ScrapeRoutes").
Strs("provided", cfg.ScrapeRoutes).
Strs("default", validCfg.ScrapeRoutes).
Msg("falling back to default configuration")
}
validCfg.ParseOptions.ParseOptions = cfg.ParseOptions.ParseOptions.Validate()
return
}
@@ -89,9 +109,6 @@ func NewFrontend(c conf.MapConfig, logic *middleware.Logic) (frontend.Frontend,
if cfg, err = cfg.Validate(); err != nil {
return nil, err
}
if len(cfg.AnnounceRoutes) < 1 || len(cfg.ScrapeRoutes) < 1 {
return nil, errRoutesNotProvided
}
f := &httpFE{
logic: logic,
+13 -10
View File
@@ -9,15 +9,12 @@ import (
)
const (
defaultReadTimeout = 2 * time.Second
defaultWriteTimeout = 2 * time.Second
defaultReadTimeout = 2 * time.Second
defaultWriteTimeout = 2 * time.Second
defaultListenAddress = ":6969"
)
var (
// ErrAddressNotProvided returned if listen address not provided in configuration
ErrAddressNotProvided = errors.New("address not provided")
errUnexpectedListenerType = errors.New("unexpected listener type")
)
var errUnexpectedListenerType = errors.New("unexpected listener type")
// ListenOptions is the base configuration which may be used in net listeners
type ListenOptions struct {
@@ -30,11 +27,17 @@ type ListenOptions struct {
// Validate checks if listen address provided and sets default
// timeout options if needed
func (lo ListenOptions) Validate() (validOptions ListenOptions, err error) {
func (lo ListenOptions) Validate(ignoreTimeouts bool) (validOptions ListenOptions) {
validOptions = lo
if len(lo.Addr) == 0 {
err = ErrAddressNotProvided
} else {
validOptions.Addr = defaultListenAddress
logger.Warn().
Str("name", "Addr").
Str("provided", lo.Addr).
Str("default", validOptions.Addr).
Msg("falling back to default configuration")
}
if !ignoreTimeouts {
if lo.ReadTimeout <= 0 {
validOptions.ReadTimeout = defaultReadTimeout
logger.Warn().
+7 -10
View File
@@ -23,13 +23,16 @@ import (
"github.com/sot-tech/mochi/pkg/timecache"
)
// Name - registered name of the frontend
const Name = "udp"
var (
logger = log.NewLogger("frontend/udp")
allowedGeneratedPrivateKeyRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
)
func init() {
frontend.RegisterBuilder("udp", NewFrontend)
frontend.RegisterBuilder(Name, NewFrontend)
}
// Config represents all the configurable options for a UDP BitTorrent
@@ -43,13 +46,9 @@ type Config struct {
// Validate sanity checks values set in a config and returns a new config with
// default values replacing anything that is invalid.
func (cfg Config) Validate() (validCfg Config, err error) {
if len(cfg.Addr) == 0 {
err = frontend.ErrAddressNotProvided
return
}
func (cfg Config) Validate() (validCfg Config) {
validCfg = cfg
validCfg.ListenOptions = cfg.ListenOptions.Validate(true)
// Generate a private key if one isn't provided by the user.
if cfg.PrivateKey == "" {
@@ -92,9 +91,7 @@ func NewFrontend(c conf.MapConfig, logic *middleware.Logic) (frontend.Frontend,
if err = c.Unmarshal(&cfg); err != nil {
return nil, err
}
if cfg, err = cfg.Validate(); err != nil {
return nil, err
}
cfg = cfg.Validate()
f := &udpFE{
closing: make(chan any),