-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
356 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/owncloud/ocis/v2/ocis-pkg/config" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/config/parser" | ||
"github.com/owncloud/ocis/v2/ocis/pkg/command/helper" | ||
"github.com/owncloud/ocis/v2/ocis/pkg/register" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/command" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// HubCommand is the entrypoint for the web command. | ||
func HubCommand(cfg *config.Config) *cli.Command { | ||
return &cli.Command{ | ||
Name: cfg.Hub.Service.Name, | ||
Usage: helper.SubcommandDescription(cfg.Hub.Service.Name), | ||
Category: "services", | ||
Before: func(c *cli.Context) error { | ||
configlog.Error(parser.ParseConfig(cfg, true)) | ||
cfg.WebDAV.Commons = cfg.Commons | ||
return nil | ||
}, | ||
Subcommands: command.GetCommands(cfg.Hub), | ||
} | ||
} | ||
|
||
func init() { | ||
register.AddCommand(HubCommand) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/command" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config/defaults" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if err := command.Execute(defaults.DefaultConfig()); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config" | ||
"os" | ||
|
||
"github.com/owncloud/ocis/v2/ocis-pkg/clihelper" | ||
"github.com/thejerf/suture/v4" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// GetCommands provides all commands for this service | ||
func GetCommands(cfg *config.Config) cli.Commands { | ||
return []*cli.Command{ | ||
Server(cfg), | ||
} | ||
} | ||
|
||
// Execute is the entry point for the web command. | ||
func Execute(cfg *config.Config) error { | ||
app := clihelper.DefaultApp(&cli.App{ | ||
Name: "hub", | ||
Usage: "Serve ownCloud hub for oCIS", | ||
Commands: GetCommands(cfg), | ||
}) | ||
|
||
return app.Run(os.Args) | ||
} | ||
|
||
// SutureService allows for the web command to be embedded and supervised by a suture supervisor tree. | ||
type SutureService struct { | ||
cfg *config.Config | ||
} | ||
|
||
// NewSutureService creates a new web.SutureService | ||
func NewSutureService(cfg *ociscfg.Config) suture.Service { | ||
cfg.Hub.Commons = cfg.Commons | ||
return SutureService{ | ||
cfg: cfg.Hub, | ||
} | ||
} | ||
|
||
func (s SutureService) Serve(ctx context.Context) error { | ||
s.cfg.Context = ctx | ||
if err := Execute(s.cfg); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/service/http" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/version" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config/parser" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/service" | ||
"github.com/urfave/cli/v2" | ||
"go-micro.dev/v4" | ||
) | ||
|
||
// Server is the entrypoint for the server command. | ||
func Server(cfg *config.Config) *cli.Command { | ||
return &cli.Command{ | ||
Name: "server", | ||
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", "hub"), | ||
Category: "server", | ||
Before: func(c *cli.Context) error { | ||
return configlog.ReturnFatal(parser.ParseConfig(cfg)) | ||
}, | ||
Action: func(c *cli.Context) error { | ||
var ( | ||
ctx, cancel = func() (context.Context, context.CancelFunc) { | ||
if cfg.Context == nil { | ||
return context.WithCancel(context.Background()) | ||
} | ||
return context.WithCancel(cfg.Context) | ||
}() | ||
) | ||
|
||
defer cancel() | ||
|
||
httpService, err := http.NewService( | ||
http.Name(cfg.Service.Name), | ||
http.Namespace(cfg.HTTP.Namespace), | ||
http.Version(version.GetString()), | ||
http.Address(cfg.HTTP.Addr), | ||
http.Context(ctx), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := micro.RegisterHandler(httpService.Server(), service.New(cfg)); err != nil { | ||
return err | ||
} | ||
|
||
return httpService.Run() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/shared" | ||
) | ||
|
||
// Config combines all available configuration parts. | ||
type Config struct { | ||
Commons *shared.Commons `yaml:"-"` // don't use this directly as configuration for a service | ||
HTTP HTTP `yaml:"http"` | ||
Service Service `yaml:"-"` | ||
TokenManager *TokenManager `yaml:"token_manager"` | ||
Context context.Context `yaml:"-"` | ||
} | ||
|
||
// Service defines the available service configuration. | ||
type Service struct { | ||
Name string `yaml:"-"` | ||
} | ||
|
||
// HTTP defines the available http configuration. | ||
type HTTP struct { | ||
Addr string `yaml:"addr" env:"HUB_HTTP_ADDR" desc:"The bind address of the HTTP service."` | ||
Namespace string `yaml:"-"` | ||
Root string `yaml:"root" env:"HUB_HTTP_ROOT" desc:"Subdirectory that serves as the root for this HTTP service."` | ||
} | ||
|
||
// TokenManager is the config for using the reva token manager | ||
type TokenManager struct { | ||
JWTSecret string `yaml:"jwt_secret" env:"OCIS_JWT_SECRET;HUB_JWT_SECRET" desc:"The secret to mint and validate jwt tokens."` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package defaults | ||
|
||
import ( | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config" | ||
"strings" | ||
) | ||
|
||
// DefaultConfig returns the default config | ||
func DefaultConfig() *config.Config { | ||
return &config.Config{ | ||
Service: config.Service{ | ||
Name: "hub", | ||
}, | ||
HTTP: config.HTTP{ | ||
Addr: "127.0.0.1:9180", | ||
Namespace: "com.owncloud.web", | ||
Root: "/", | ||
}, | ||
} | ||
} | ||
|
||
func EnsureDefaults(cfg *config.Config) { | ||
if cfg.TokenManager == nil && cfg.Commons != nil && cfg.Commons.TokenManager != nil { | ||
cfg.TokenManager = &config.TokenManager{ | ||
JWTSecret: cfg.Commons.TokenManager.JWTSecret, | ||
} | ||
} else if cfg.TokenManager == nil { | ||
cfg.TokenManager = &config.TokenManager{} | ||
} | ||
} | ||
|
||
func Sanitize(cfg *config.Config) { | ||
// sanitize config | ||
if cfg.HTTP.Root != "/" { | ||
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package parser | ||
|
||
import ( | ||
"errors" | ||
|
||
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/shared" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config" | ||
"github.com/owncloud/ocis/v2/services/hub/pkg/config/defaults" | ||
|
||
"github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode" | ||
) | ||
|
||
// ParseConfig loads configuration from known paths. | ||
func ParseConfig(cfg *config.Config) error { | ||
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defaults.EnsureDefaults(cfg) | ||
|
||
// load all env variables relevant to the config in the current context. | ||
if err := envdecode.Decode(cfg); err != nil { | ||
// no environment variable set for this config is an expected "error" | ||
if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) { | ||
return err | ||
} | ||
} | ||
|
||
defaults.Sanitize(cfg) | ||
|
||
return Validate(cfg) | ||
} | ||
|
||
func Validate(cfg *config.Config) error { | ||
if cfg.TokenManager.JWTSecret == "" { | ||
return shared.MissingJWTTokenError(cfg.Service.Name) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.