2020-09-18 12:47:26 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-04 13:56:49 +01:00
|
|
|
"context"
|
2020-09-18 12:47:26 +02:00
|
|
|
"os"
|
2021-03-10 12:26:14 +01:00
|
|
|
|
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-11-11 12:51:57 +01:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/shared"
|
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"
|
2021-03-11 15:50:01 +01:00
|
|
|
"github.com/thejerf/suture/v4"
|
2021-09-14 16:05:03 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-09-18 12:47:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Execute is the entry point for the ocis-proxy command.
|
2021-03-04 13:56:49 +01:00
|
|
|
func Execute(cfg *config.Config) error {
|
2020-09-18 12:47:26 +02:00
|
|
|
app := &cli.App{
|
|
|
|
|
Name: "ocis-proxy",
|
|
|
|
|
Version: version.String,
|
2020-12-18 15:04:04 +01:00
|
|
|
Usage: "proxy for oCIS",
|
2020-09-18 12:47:26 +02:00
|
|
|
Compiled: version.Compiled(),
|
|
|
|
|
Authors: []*cli.Author{
|
|
|
|
|
{
|
|
|
|
|
Name: "ownCloud GmbH",
|
|
|
|
|
Email: "support@owncloud.com",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Before: func(c *cli.Context) error {
|
2020-09-25 13:31:14 +02:00
|
|
|
cfg.Service.Version = version.String
|
2020-10-09 11:24:04 +02:00
|
|
|
return nil
|
2020-09-18 12:47:26 +02:00
|
|
|
},
|
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
|
Server(cfg),
|
|
|
|
|
Health(cfg),
|
2020-09-25 13:31:14 +02:00
|
|
|
PrintVersion(cfg),
|
2020-09-18 12:47:26 +02:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 11:14:26 +01:00
|
|
|
// ParseConfig loads proxy configuration. Loading will first attempt to parse config files in the expected locations
|
|
|
|
|
// and then parses environment variables. In the context of oCIS env variables will always overwrite values set
|
|
|
|
|
// in a config file.
|
|
|
|
|
// If this extension is run as a subcommand (i.e: ocis proxy) then there are 2 levels of config parsing:
|
|
|
|
|
// 1. ocis.yaml (if any)
|
|
|
|
|
// 2. proxy.yaml (if any)
|
|
|
|
|
// 3. environment variables.
|
2020-09-18 12:47:26 +02:00
|
|
|
func ParseConfig(c *cli.Context, cfg *config.Config) error {
|
2021-11-03 11:34:56 +01:00
|
|
|
conf, err := ociscfg.BindSourcesToStructs("proxy", cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-11 12:51:57 +01:00
|
|
|
// provide with defaults for shared logging, since we need a valid destination address for BindEnv.
|
|
|
|
|
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
|
|
|
|
|
cfg.Log = &shared.Log{
|
|
|
|
|
Level: cfg.Commons.Log.Level,
|
|
|
|
|
Pretty: cfg.Commons.Log.Pretty,
|
|
|
|
|
Color: cfg.Commons.Log.Color,
|
|
|
|
|
File: cfg.Commons.Log.File,
|
|
|
|
|
}
|
|
|
|
|
} else if cfg.Log == nil && cfg.Commons == nil {
|
|
|
|
|
cfg.Log = &shared.Log{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf.LoadOSEnv(config.GetEnv(cfg), false)
|
|
|
|
|
|
2021-11-07 20:52:36 +01:00
|
|
|
bindings := config.StructMappings(cfg)
|
2021-11-08 11:14:26 +01:00
|
|
|
return ociscfg.BindEnv(conf, bindings)
|
2020-09-18 12:47:26 +02:00
|
|
|
}
|
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 {
|
2021-11-11 12:51:57 +01:00
|
|
|
cfg.Proxy.Commons = cfg.Commons
|
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
|
|
|
}
|
2021-11-11 12:51:57 +01:00
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
|
log.File(cfg.Log.File),
|
|
|
|
|
)
|
|
|
|
|
}
|