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

104 lines
2.4 KiB
Go
Raw Normal View History

package command
import (
2021-03-04 14:33:16 +01:00
"context"
"os"
2021-11-08 14:08:05 +01:00
"github.com/owncloud/ocis/ocis-pkg/shared"
2021-03-09 16:03:47 +00:00
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
2020-09-18 15:05:53 +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-12-09 14:01:37 +01:00
"github.com/owncloud/ocis/web/pkg/config"
2021-03-11 15:50:01 +01:00
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
2020-12-09 14:01:37 +01:00
// Execute is the entry point for the web command.
2021-03-04 14:33:16 +01:00
func Execute(cfg *config.Config) error {
app := &cli.App{
2020-12-09 14:01:37 +01:00
Name: "web",
Version: version.String,
2020-12-09 14:01:37 +01:00
Usage: "Serve ownCloud Web for oCIS",
Compiled: version.Compiled(),
Authors: []*cli.Author{
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},
Commands: []*cli.Command{
Server(cfg),
Health(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(
2020-12-09 14:01:37 +01:00
log.Name("web"),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
2021-03-17 15:39:24 +01:00
log.File(cfg.Log.File),
)
}
2021-11-05 13:33:03 +01:00
// ParseConfig loads graph configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
2021-11-05 13:33:03 +01:00
conf, err := ociscfg.BindSourcesToStructs("web", cfg)
if err != nil {
return err
}
2021-11-11 15:56:38 +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-08 12:28:56 +01:00
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
}
2021-03-04 14:33:16 +01:00
// SutureService allows for the web command to be embedded and supervised by a suture supervisor tree.
type SutureService struct {
2021-03-11 15:50:01 +01:00
cfg *config.Config
2021-03-04 14:33:16 +01:00
}
// NewSutureService creates a new web.SutureService
2021-03-11 15:50:01 +01:00
func NewSutureService(cfg *ociscfg.Config) suture.Service {
2021-11-11 15:56:38 +01:00
cfg.Web.Commons = cfg.Commons
2021-03-04 14:33:16 +01:00
return SutureService{
2021-03-11 15:50:01 +01:00
cfg: cfg.Web,
2021-03-04 14:33:16 +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 14:33:16 +01:00
if err := Execute(s.cfg); err != nil {
2021-03-11 15:50:01 +01:00
return err
2021-03-04 14:33:16 +01:00
}
2021-03-11 15:50:01 +01:00
return nil
2021-03-04 14:33:16 +01:00
}