From 0c41a5a84df9fdfc9ff9367cbc698b6265a108dc Mon Sep 17 00:00:00 2001 From: Andrei Mihu Date: Sat, 11 Feb 2017 16:01:56 +0000 Subject: [PATCH] Display config as YAML in embedded dashboard. Close #19 --- .gitignore | 1 + CHANGELOG.md | 4 +++ dashboard/src/components/ConfigViewer.vue | 33 ++++++++++++++++- server/config.go | 44 +++++++++++------------ 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 1c5492c86..a8b531afb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ build/* data/* .GITHUB_TOKEN .idea +*.iml install/cloud/**/*.json ### Go ### diff --git a/CHANGELOG.md b/CHANGELOG.md index c2146ab7c..fd9fdfd55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p ## [Unreleased] +### Changed + +- Configuration in dashboard is now displayed as YAML. + ### Fixed - Improve group SQL query with type information. diff --git a/dashboard/src/components/ConfigViewer.vue b/dashboard/src/components/ConfigViewer.vue index 2414cb9a3..032ba3e66 100644 --- a/dashboard/src/components/ConfigViewer.vue +++ b/dashboard/src/components/ConfigViewer.vue @@ -33,6 +33,37 @@ d: '', }; + function jsonToYaml(obj, depth, acc) { + const type = typeof obj; + if (obj instanceof Array) { + obj.forEach((ele) => { + const subAcc = []; + jsonToYaml(ele, depth + 1, subAcc); + const empty = subAcc.length === 0; + const prefix = `${' '.repeat(depth)}- `; + acc.push((empty ? '' : '\n') + (empty ? '' : prefix) + subAcc.join(`\n${prefix}`).trim()); + }); + } else if (type === 'object') { + let first = true; + const prefix = ' '.repeat(depth); + Object.keys(obj).forEach((k) => { + if (Object.prototype.hasOwnProperty.call(obj, k)) { + acc.push(`${first ? `\n${prefix}` : prefix}${k}:${jsonToYaml(obj[k], depth + 1, [])}`); + first = false; + } + }); + } else if (type === 'string') { + acc.push(` "${obj}"`); + } else if (type === 'boolean') { + acc.push(obj ? ' true' : ' false'); + } else if (type === 'number') { + acc.push(` ${obj.toString()}`); + } else { + acc.push(' null'); + } + return acc.join('\n'); + } + export default { name: 'config-viewer', created() { @@ -50,7 +81,7 @@ this.$notify.error({ title: 'Operation Failed', message: response.statusText }); } response.json().then((json) => { - configData.d = JSON.stringify(json, null, 2); + configData.d = jsonToYaml(json, 0, []).trim(); }); }).catch(() => { loadingInstance.close(); diff --git a/server/config.go b/server/config.go index ec9d34e82..7332e99d3 100644 --- a/server/config.go +++ b/server/config.go @@ -36,15 +36,15 @@ type Config interface { } type config struct { - Name string `yaml:"name"` - Datadir string `yaml:"data_dir"` - Port int `yaml:"port"` - OpsPort int `yaml:"ops_port"` - Dsns []string `yaml:"dsns"` - Session *SessionConfig `yaml:"session"` - Transport *TransportConfig `yaml:"transport"` - Database *DatabaseConfig `yaml:"database"` - Social *SocialConfig `yaml:"social"` + Name string `yaml:"name" json:"name"` + Datadir string `yaml:"data_dir" json:"data_dir"` + Port int `yaml:"port" json:"port"` + OpsPort int `yaml:"ops_port" json:"ops_port"` + Dsns []string `yaml:"dsns" json:"dsns"` + Session *SessionConfig `yaml:"session" json:"session"` + Transport *TransportConfig `yaml:"transport" json:"transport"` + Database *DatabaseConfig `yaml:"database" json:"database"` + Social *SocialConfig `yaml:"social" json:"social"` } // NewConfig constructs a Config struct which represents server settings. @@ -103,8 +103,8 @@ func (c *config) GetSocial() *SocialConfig { // SessionConfig is configuration relevant to the session type SessionConfig struct { - EncryptionKey string `yaml:"encryption_key"` - TokenExpiryMs int64 `yaml:"token_expiry_ms"` + EncryptionKey string `yaml:"encryption_key" json:"encryption_key"` + TokenExpiryMs int64 `yaml:"token_expiry_ms" json:"token_expiry_ms"` } // NewSessionConfig creates a new SessionConfig struct @@ -117,11 +117,11 @@ func NewSessionConfig() *SessionConfig { // TransportConfig is configuration relevant to the transport socket and protocol type TransportConfig struct { - ServerKey string `yaml:"server_key"` - MaxMessageSizeBytes int64 `yaml:"max_message_size_bytes"` - WriteWaitMs int `yaml:"write_wait_ms"` - PongWaitMs int `yaml:"pong_wait_ms"` - PingPeriodMs int `yaml:"ping_period_ms"` + ServerKey string `yaml:"server_key" json:"server_key"` + MaxMessageSizeBytes int64 `yaml:"max_message_size_bytes" json:"max_message_size_bytes"` + WriteWaitMs int `yaml:"write_wait_ms" json:"write_wait_ms"` + PongWaitMs int `yaml:"pong_wait_ms" json:"pong_wait_ms"` + PingPeriodMs int `yaml:"ping_period_ms" json:"ping_period_ms"` } // NewTransportConfig creates a new TransportConfig struct @@ -137,9 +137,9 @@ func NewTransportConfig() *TransportConfig { // DatabaseConfig is configuration relevant to the Database storage type DatabaseConfig struct { - ConnMaxLifetimeMs int `yaml:"conn_max_lifetime_ms"` - MaxOpenConns int `yaml:"max_open_conns"` - MaxIdleConns int `yaml:"max_idle_conns"` + ConnMaxLifetimeMs int `yaml:"conn_max_lifetime_ms" json:"conn_max_lifetime_ms"` + MaxOpenConns int `yaml:"max_open_conns" json:"max_open_conns"` + MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns"` } // NewDatabaseConfig creates a new DatabaseConfig struct @@ -153,13 +153,13 @@ func NewDatabaseConfig() *DatabaseConfig { // SocialConfig is configuration relevant to the Social providers type SocialConfig struct { - Steam *SocialConfigSteam `yaml:"steam"` + Steam *SocialConfigSteam `yaml:"steam" json:"steam"` } // SocialConfigSteam is configuration relevant to Steam type SocialConfigSteam struct { - PublisherKey string `yaml:"publisher_key"` - AppID int `yaml:"app_id"` + PublisherKey string `yaml:"publisher_key" json:"publisher_key"` + AppID int `yaml:"app_id" json:"app_id"` } // NewSocialConfig creates a new SocialConfig struct -- GitLab