Drop all config attributes, just serve a config.json
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user