mirror of
https://github.com/sot-tech/mochi.git
synced 2026-04-27 08:00:00 -07:00
Add cmd option for quick start w/o config file
This commit is contained in:
@@ -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.
|
||||
//
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user