Skip to content

Commit

Permalink
feat(server): REEARTH_WEB_CONFIG envvar
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 15, 2023
1 parent afc7e23 commit e6e79e3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {
(&WebHandler{
Disabled: cfg.Config.Web_Disabled,
AppDisabled: cfg.Config.Web_App_Disabled,
WebConfig: cfg.Config.Web,
WebConfig: cfg.Config.WebConfig(),
AuthConfig: cfg.Config.AuthForWeb(),
HostPattern: cfg.Config.Published.Host,
FS: nil,
Expand Down
27 changes: 26 additions & 1 deletion server/internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type Config struct {
Policy PolicyConfig
Web_Disabled bool
Web_App_Disabled bool
Web WebConfig
Web map[string]string
Web_Config JSON
SignupSecret string
SignupDisabled bool
// auth
Expand Down Expand Up @@ -410,3 +411,27 @@ func (c *OAuthClientCredentialsConfig) Config() *clientcredentials.Config {
type PolicyConfig struct {
Default *workspace.PolicyID
}

type JSON struct {
Data any
}

func (j *JSON) Decode(value string) error {
if value == "" {
return nil
}
return json.Unmarshal([]byte(value), &j.Data)
}

func (c *Config) WebConfig() map[string]any {
w := make(map[string]any)
for k, v := range c.Web {
w[k] = v
}
if m, ok := c.Web_Config.Data.(map[string]any); ok {
for k, v := range m {
w[k] = v
}
}
return w
}
5 changes: 3 additions & 2 deletions server/internal/app/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestReadConfig(t *testing.T) {
t.Setenv("REEARTH_AUTH", `[{"iss":"bar"}]`)
t.Setenv("REEARTH_AUTH_ISS", "hoge")
t.Setenv("REEARTH_WEB", "a:1,b:2")
t.Setenv("REEARTH_WEB_CONFIG", `{"c":3}`)
cfg, err = ReadConfig(false)
assert.NoError(t, err)
assert.Equal(t, AuthConfigs([]AuthConfig{{ISS: "bar"}}), cfg.Auth)
Expand All @@ -73,7 +74,7 @@ func TestReadConfig(t *testing.T) {
}, cfg.Auths())
assert.Equal(t, "hoge", cfg.Auth_ISS)
assert.Equal(t, "", cfg.Auth_AUD)
assert.Equal(t, WebConfig{"a": "1", "b": "2"}, cfg.Web)
assert.Equal(t, map[string]any{"a": "1", "b": "2", "c": float64(3)}, cfg.WebConfig())

t.Setenv("REEARTH_AUTH_AUD", "foo")
t.Setenv("REEARTH_AUTH0_DOMAIN", "foo")
Expand All @@ -88,7 +89,7 @@ func TestReadConfig(t *testing.T) {
{ISS: "bar"}, // REEARTH_AUTH
}, cfg.Auths())
assert.Equal(t, "foo", cfg.Auth_AUD)
assert.Equal(t, WebConfig{}, cfg.Web)
assert.Equal(t, map[string]string{}, cfg.Web)
}

func Test_AddHTTPScheme(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions server/internal/app/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
"github.com/spf13/afero"
)

type WebConfig map[string]string

type WebHandler struct {
Disabled bool
AppDisabled bool
WebConfig WebConfig
WebConfig map[string]any
AuthConfig *AuthConfig
HostPattern string
FS afero.Fs
Expand All @@ -34,7 +32,7 @@ func (w *WebHandler) Handler(e *echo.Echo) {

e.Logger.Info("web: web directory will be delivered\n")

config := map[string]string{}
config := map[string]any{}
if w.AuthConfig != nil {
if w.AuthConfig.ISS != "" {
config["auth0Domain"] = strings.TrimSuffix(w.AuthConfig.ISS, "/")
Expand Down
2 changes: 1 addition & 1 deletion server/internal/app/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestWeb(t *testing.T) {
(&WebHandler{
Disabled: tt.disabled,
AppDisabled: tt.appDisabled,
WebConfig: WebConfig{
WebConfig: map[string]any{
"a": "b",
},
AuthConfig: &AuthConfig{
Expand Down

0 comments on commit e6e79e3

Please sign in to comment.