Files
ocis/proxy/pkg/command/root.go
T

141 lines
3.0 KiB
Go
Raw Normal View History

package command
import (
2021-03-04 13:56:49 +01:00
"context"
"os"
"strings"
"github.com/owncloud/ocis/ocis-pkg/sync"
2021-03-09 16:03:47 +00:00
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
2020-09-18 15:18:41 +02:00
"github.com/owncloud/ocis/ocis-pkg/log"
2021-10-05 12:39:31 +02:00
"github.com/owncloud/ocis/ocis-pkg/version"
2020-09-18 12:48:35 +02:00
"github.com/owncloud/ocis/proxy/pkg/config"
"github.com/owncloud/ocis/proxy/pkg/flagset"
"github.com/spf13/viper"
2021-03-11 15:50:01 +01:00
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
// Execute is the entry point for the ocis-proxy command.
2021-03-04 13:56:49 +01:00
func Execute(cfg *config.Config) error {
app := &cli.App{
Name: "ocis-proxy",
Version: version.String,
2020-12-18 15:04:04 +01:00
Usage: "proxy for oCIS",
Compiled: version.Compiled(),
Authors: []*cli.Author{
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},
Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
cfg.Service.Version = version.String
2020-10-09 11:24:04 +02:00
return nil
},
Commands: []*cli.Command{
Server(cfg),
Health(cfg),
PrintVersion(cfg),
},
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help,h",
Usage: "Show the help",
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version,v",
Usage: "Print the version",
}
return app.Run(os.Args)
}
// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return log.NewLogger(
log.Name("proxy"),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
2021-03-17 15:42:24 +01:00
log.File(cfg.Log.File),
)
}
// ParseConfig loads proxy configuration from Viper known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
sync.ParsingViperConfig.Lock()
defer sync.ParsingViperConfig.Unlock()
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("PROXY")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("proxy")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
2020-10-09 11:24:04 +02:00
logger.Debug().
Msg("no config found on preconfigured location")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
2020-10-09 11:24:04 +02:00
Msg("unsupported config type")
default:
logger.Fatal().
Err(err).
2020-10-09 11:24:04 +02:00
Msg("failed to read config")
}
}
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}
return nil
}
2021-03-04 13:56:49 +01:00
2021-03-04 14:03:06 +01:00
// SutureService allows for the proxy command to be embedded and supervised by a suture supervisor tree.
2021-03-04 13:56:49 +01:00
type SutureService struct {
2021-03-11 15:50:01 +01:00
cfg *config.Config
2021-03-04 13:56:49 +01:00
}
2021-03-04 14:03:06 +01:00
// NewSutureService creates a new proxy.SutureService
2021-03-11 15:50:01 +01:00
func NewSutureService(cfg *ociscfg.Config) suture.Service {
if cfg.Mode == 0 {
cfg.Proxy.Supervised = true
}
2021-03-17 15:42:24 +01:00
cfg.Proxy.Log.File = cfg.Log.File
2021-03-04 13:56:49 +01:00
return SutureService{
2021-03-11 15:50:01 +01:00
cfg: cfg.Proxy,
2021-03-04 13:56:49 +01:00
}
}
2021-03-11 15:50:01 +01:00
func (s SutureService) Serve(ctx context.Context) error {
s.cfg.Context = ctx
2021-03-04 13:56:49 +01:00
if err := Execute(s.cfg); err != nil {
2021-03-11 15:50:01 +01:00
return err
2021-03-04 13:56:49 +01:00
}
2021-03-11 15:50:01 +01:00
return nil
2021-03-04 13:56:49 +01:00
}