From 39de6f69e4871914060c440e285b0fc762836561 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Wed, 11 Sep 2019 13:08:33 +0200 Subject: [PATCH] Drop all config attributes, just serve a config.json --- config/example.yml | 8 +--- pkg/command/default.go | 8 +--- pkg/command/server.go | 33 ++----------- pkg/handler/config/config.go | 93 +++++++----------------------------- pkg/handler/config/option.go | 41 ++-------------- pkg/router/server/option.go | 45 ++--------------- pkg/router/server/server.go | 18 ++----- 7 files changed, 36 insertions(+), 210 deletions(-) diff --git a/config/example.yml b/config/example.yml index 88cab6ba27..8ea2129ff8 100644 --- a/config/example.yml +++ b/config/example.yml @@ -12,12 +12,6 @@ asset: path: config: - custom: - server: - theme: owncloud - version: 0.1.0 - client: - apps: - - files + file: ... diff --git a/pkg/command/default.go b/pkg/command/default.go index 68700aa443..086d79d299 100644 --- a/pkg/command/default.go +++ b/pkg/command/default.go @@ -14,11 +14,5 @@ func init() { viper.SetDefault("http.root", "/") viper.SetDefault("asset.path", "") - - viper.SetDefault("config.custom", "") - viper.SetDefault("config.server", "") - viper.SetDefault("config.theme", "owncloud") - viper.SetDefault("config.version", "0.1.0") - viper.SetDefault("config.client", "") - viper.SetDefault("config.apps", []string{"files"}) + viper.SetDefault("config.file", "") } diff --git a/pkg/command/server.go b/pkg/command/server.go index 0de0d34e0f..ac1e0ddc89 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -65,12 +65,7 @@ func Server() *cobra.Command { Handler: server.Router( server.WithRoot(viper.GetString("http.root")), server.WithPath(viper.GetString("asset.path")), - server.WithCustom(viper.GetString("config.custom")), - server.WithServer(viper.GetString("config.server")), - server.WithTheme(viper.GetString("config.theme")), - server.WithVersion(viper.GetString("config.version")), - server.WithClient(viper.GetString("config.client")), - server.WithApps(viper.GetStringSlice("config.apps")), + server.WithConfig(viper.GetString("config.file")), ), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, @@ -142,29 +137,9 @@ func Server() *cobra.Command { viper.BindPFlag("asset.path", cmd.Flags().Lookup("asset-path")) viper.BindEnv("asset.path", "PHOENIX_ASSET_PATH") - cmd.Flags().String("config-custom", "", "Path to custom config") - viper.BindPFlag("config.custom", cmd.Flags().Lookup("config-custom")) - viper.BindEnv("config.custom", "PHOENIX_CONFIG_CUSTOM") - - cmd.Flags().String("config-server", "", "URL to ownCloud server") - viper.BindPFlag("config.server", cmd.Flags().Lookup("config-server")) - viper.BindEnv("config.server", "PHOENIX_CONFIG_SERVER") - - cmd.Flags().String("config-theme", "", "The to use with Phoenix") - viper.BindPFlag("config.theme", cmd.Flags().Lookup("config-theme")) - viper.BindEnv("config.theme", "PHOENIX_CONFIG_THEME") - - cmd.Flags().String("config-version", "", "Config version for Phoenix") - viper.BindPFlag("config.version", cmd.Flags().Lookup("config-version")) - viper.BindEnv("config.version", "PHOENIX_CONFIG_VERSION") - - cmd.Flags().String("config-client", "", "Client ID used for OAuth2") - viper.BindPFlag("config.client", cmd.Flags().Lookup("config-client")) - viper.BindEnv("config.client", "PHOENIX_CONFIG_CLIENT") - - cmd.Flags().StringSlice("config-apps", []string{}, "List of enabled apps") - viper.BindPFlag("config.apps", cmd.Flags().Lookup("config-apps")) - viper.BindEnv("config.apps", "PHOENIX_CONFIG_APPS") + cmd.Flags().String("config-file", "", "Path to phoenix config") + viper.BindPFlag("config.file", cmd.Flags().Lookup("config-file")) + viper.BindEnv("config.file", "PHOENIX_CONFIG_FILE") return cmd } diff --git a/pkg/handler/config/config.go b/pkg/handler/config/config.go index b8e91711c8..d1141f7cac 100644 --- a/pkg/handler/config/config.go +++ b/pkg/handler/config/config.go @@ -1,8 +1,6 @@ package config import ( - "encoding/json" - "fmt" "io/ioutil" "net/http" "os" @@ -17,91 +15,36 @@ var ( // config gets initialized by New and provides the handler. type config struct { - custom string - server string - theme string - version string - client string - apps []string -} - -// auth is part of the phoenix config repsonse. -type auth struct { - ClientID string `json:"clientId"` - URL string `json:"url"` - AuthURL string `json:"authUrl"` -} - -// phoenix is part of the phoenix config response. -type phoenix struct { - Server string `json:"server"` - Theme string `json:"theme"` - Version string `json:"version"` - Apps []string `json:"apps"` - Auth auth `json:"auth"` + file string } // ServeHTTP just implements the http.Handler interface. func (c config) ServeHTTP(w http.ResponseWriter, r *http.Request) { - resp, err := c.payload() + if _, err := os.Stat(c.file); os.IsNotExist(err) { + log.Error(). + Err(err). + Str("config", c.file). + Msg("Phoenix config doesn't exist") + + http.Error(w, ErrConfigInvalid, http.StatusUnprocessableEntity) + return + } + + payload, err := ioutil.ReadFile(c.file) if err != nil { + log.Error(). + Err(err). + Str("config", c.file). + Msg("Failed to read custom config") + http.Error(w, ErrConfigInvalid, http.StatusUnprocessableEntity) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(resp) -} - -// payload prepares the phoenix config content. -func (c config) payload() ([]byte, error) { - if c.custom != "" { - if _, err := os.Stat(c.custom); os.IsNotExist(err) { - log.Error(). - Err(err). - Str("config", c.custom). - Msg("Custom config doesn't exist") - - return []byte{}, err - } - - payload, err := ioutil.ReadFile(c.custom) - - if err != nil { - log.Error(). - Err(err). - Str("config", c.custom). - Msg("Failed to read custom config") - - return []byte{}, err - } - - return payload, nil - } - - payload, err := json.Marshal(phoenix{ - Server: c.server, - Theme: c.theme, - Version: c.version, - Apps: c.apps, - Auth: auth{ - ClientID: c.client, - URL: fmt.Sprintf("%s/apps/oauth2/api/v1/token", c.server), - AuthURL: fmt.Sprintf("%s/apps/oauth2/authorize", c.server), - }, - }) - - if err != nil { - log.Error(). - Err(err). - Msg("Failed to generate config") - - return []byte{}, err - } - - return payload, nil + w.Write(payload) } // Handler returns the handler for config endpoint. diff --git a/pkg/handler/config/option.go b/pkg/handler/config/option.go index 22a7086cd3..e4e0dac8dc 100644 --- a/pkg/handler/config/option.go +++ b/pkg/handler/config/option.go @@ -3,44 +3,9 @@ package config // Option configures an assets option. type Option func(*config) -// WithServer returns an option to set server. -func WithServer(val string) Option { +// WithConfig returns an option to set config. +func WithConfig(val string) Option { return func(c *config) { - c.server = val - } -} - -// WithTheme returns an option to set theme. -func WithTheme(val string) Option { - return func(c *config) { - c.theme = val - } -} - -// WithVersion returns an option to set version. -func WithVersion(val string) Option { - return func(c *config) { - c.version = val - } -} - -// WithClient returns an option to set client id. -func WithClient(val string) Option { - return func(c *config) { - c.client = val - } -} - -// WithApps returns an option to set apps. -func WithApps(val []string) Option { - return func(c *config) { - c.apps = val - } -} - -// WithCustom returns an option to set custom config. -func WithCustom(val string) Option { - return func(c *config) { - c.custom = val + c.file = val } } diff --git a/pkg/router/server/option.go b/pkg/router/server/option.go index d188b95734..c406ef8c04 100644 --- a/pkg/router/server/option.go +++ b/pkg/router/server/option.go @@ -3,58 +3,23 @@ package server // Option configures an assets option. type Option func(*server) -// WithRoot returns an option to set a root. +// WithRoot returns an option to set root. func WithRoot(val string) Option { return func(s *server) { s.root = val } } -// WithPath returns an option to set a path. +// WithPath returns an option to set path. func WithPath(val string) Option { return func(s *server) { s.path = val } } -// WithCustom returns an option to set a path. -func WithCustom(val string) Option { +// WithConfig returns an option to set config. +func WithConfig(val string) Option { return func(s *server) { - s.custom = val - } -} - -// WithServer returns an option to set a path. -func WithServer(val string) Option { - return func(s *server) { - s.server = val - } -} - -// WithTheme returns an option to set a path. -func WithTheme(val string) Option { - return func(s *server) { - s.theme = val - } -} - -// WithVersion returns an option to set a path. -func WithVersion(val string) Option { - return func(s *server) { - s.version = val - } -} - -// WithClient returns an option to set a path. -func WithClient(val string) Option { - return func(s *server) { - s.client = val - } -} - -// WithApps returns an option to set a path. -func WithApps(val []string) Option { - return func(s *server) { - s.apps = val + s.config = val } } diff --git a/pkg/router/server/server.go b/pkg/router/server/server.go index 2343d36543..c4c0daeaac 100644 --- a/pkg/router/server/server.go +++ b/pkg/router/server/server.go @@ -15,14 +15,9 @@ import ( // server gets initialized by Router and configures the router. type server struct { - root string - path string - custom string - server string - theme string - version string - client string - apps []string + root string + path string + config string } // Router initializes a router for the http server. @@ -61,12 +56,7 @@ func Router(opts ...Option) *chi.Mux { root.Mount( "/config.json", config.Handler( - config.WithCustom(s.custom), - config.WithServer(s.server), - config.WithTheme(s.theme), - config.WithVersion(s.version), - config.WithClient(s.client), - config.WithApps(s.apps), + config.WithConfig(s.config), ), )