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

View File

@@ -7,12 +7,10 @@ import (
"gopkg.in/yaml.v3"
fh "github.com/sot-tech/mochi/frontend/http"
fu "github.com/sot-tech/mochi/frontend/udp"
"github.com/sot-tech/mochi/pkg/conf"
// Imports to register frontends
_ "github.com/sot-tech/mochi/frontend/http"
_ "github.com/sot-tech/mochi/frontend/udp"
// Imports to register middleware hooks.
_ "github.com/sot-tech/mochi/middleware/clientapproval"
_ "github.com/sot-tech/mochi/middleware/jwt"
@@ -21,12 +19,12 @@ import (
// Imports to register storage drivers.
_ "github.com/sot-tech/mochi/storage/keydb"
_ "github.com/sot-tech/mochi/storage/memory"
sm "github.com/sot-tech/mochi/storage/memory"
_ "github.com/sot-tech/mochi/storage/pg"
_ "github.com/sot-tech/mochi/storage/redis"
)
// Config represents the configuration used for executing Conf.
// Config represents the configuration used for Server start.
type Config struct {
// TODO(jzelinskie): Evaluate whether we would like to make
// AnnounceInterval and MinAnnounceInterval optional.
@@ -42,6 +40,27 @@ type Config struct {
PostHooks []conf.NamedMapConfig `yaml:"posthooks"`
}
// QuickConfig is the simple configuration for quick start without config file.
// Includes in-memory store, http and udp frontends without any middleware.
var QuickConfig = &Config{
Frontends: []conf.NamedMapConfig{
{
Name: fh.Name,
Config: conf.MapConfig{},
},
{
Name: fu.Name,
Config: conf.MapConfig{},
},
},
Storage: conf.NamedMapConfig{
Name: sm.Name,
Config: conf.MapConfig{},
},
PreHooks: []conf.NamedMapConfig{},
PostHooks: []conf.NamedMapConfig{},
}
// ParseConfigFile returns a new Config given the path to a YAML
// configuration file.
//

View File

@@ -18,23 +18,36 @@ const (
logPrettyArg = "logPretty"
logColorsArg = "logColored"
configArg = "config"
quickArg = "quick"
)
func main() {
var s Server
var err error
logOut := flag.String(logOutArg, "stderr", "output for logging, might be 'stderr', 'stdout' or file path")
logLevel := flag.String(logLevelArg, "warn", "logging level: trace, debug, info, warn, error, fatal, panic")
logPretty := flag.Bool(logPrettyArg, false, "enable log pretty print. used only if 'logOut' set to 'stdout' or 'stderr'. if not set, log outputs json")
logColored := flag.Bool(logColorsArg, runtime.GOOS == "windows", "enable log coloring. used only if set 'logPretty'")
configPath := flag.String(configArg, "/etc/mochi.yaml", "location of configuration file")
quickStart := flag.Bool(quickArg, false, "start tracker with default configuration (all frontends, in-memory store, no hooks)")
flag.Parse()
if err := l.ConfigureLogger(*logOut, *logLevel, *logPretty, *logColored); err != nil {
if err = l.ConfigureLogger(*logOut, *logLevel, *logPretty, *logColored); err != nil {
log.Fatal("unable to configure logger: ", err)
}
if err := s.Run(*configPath); err != nil {
var cfg *Config
if *quickStart {
cfg = QuickConfig
} else {
cfg, err = ParseConfigFile(*configPath)
if err != nil {
log.Fatal("unable to read config file: ", err)
}
}
var s Server
if err = s.Run(cfg); err != nil {
log.Fatal("unable to start server: ", err)
}
defer s.Shutdown()

View File

@@ -25,12 +25,7 @@ type Server struct {
// Run begins an instance of Conf.
// It is optional to provide an instance of the peer store to avoid the
// creation of a new one.
func (r *Server) Run(configFilePath string) error {
cfg, err := ParseConfigFile(configFilePath)
if err != nil {
return fmt.Errorf("failed to read config: %w", err)
}
func (r *Server) Run(cfg *Config) (err error) {
if len(cfg.MetricsAddr) > 0 {
log.Info().Str("address", cfg.MetricsAddr).Msg("starting metrics server")
r.frontends = append(r.frontends, metrics.NewServer(cfg.MetricsAddr))