Drop all config attributes, just serve a config.json

This commit is contained in:
Thomas Boerger
2019-09-11 13:08:33 +02:00
parent ebfc405449
commit 39de6f69e4
7 changed files with 36 additions and 210 deletions

View File

@@ -12,12 +12,6 @@ asset:
path:
config:
custom:
server:
theme: owncloud
version: 0.1.0
client:
apps:
- files
file:
...

View File

@@ -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", "")
}

View File

@@ -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
}

View File

@@ -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.

View File

@@ -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
}
}

View File

@@ -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
}
}

View File

@@ -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),
),
)