1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-29 12:30:11 +00:00

change config-base format to allow easier parsing

*note*: only GetConfig has been changed for now.
the config now has "order" and "sections", and each section now has
"meta", "order" and "settings". This allows for the use of a struct for
loading in Go and less janky web code. Also now appears in swagger.
This commit is contained in:
Harvey Tindall 2021-01-02 21:24:26 +00:00
parent 8b2f6fbb8a
commit d629cae71e
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
6 changed files with 761 additions and 703 deletions

49
api.go
View File

@ -1072,13 +1072,14 @@ func (app *appContext) ApplySettings(gc *gin.Context) {
// @Summary Get jfa-go configuration. // @Summary Get jfa-go configuration.
// @Produce json // @Produce json
// @Success 200 {object} configDTO "Uses the same format as config-base.json" // @Success 200 {object} settings "Uses the same format as config-base.json"
// @Router /config [get] // @Router /config [get]
// @Security Bearer // @Security Bearer
// @tags Configuration // @tags Configuration
func (app *appContext) GetConfig(gc *gin.Context) { func (app *appContext) GetConfig(gc *gin.Context) {
app.info.Println("Config requested") app.info.Println("Config requested")
resp := map[string]interface{}{} resp := app.configBase
// Load language options
langPath := filepath.Join(app.localPath, "lang", "form") langPath := filepath.Join(app.localPath, "lang", "form")
app.lang.langFiles, _ = ioutil.ReadDir(langPath) app.lang.langFiles, _ = ioutil.ReadDir(langPath)
app.lang.langOptions = make([]string, len(app.lang.langFiles)) app.lang.langOptions = make([]string, len(app.lang.langFiles))
@ -1095,37 +1096,25 @@ func (app *appContext) GetConfig(gc *gin.Context) {
app.lang.langOptions[i] = meta.(map[string]interface{})["name"].(string) app.lang.langOptions[i] = meta.(map[string]interface{})["name"].(string)
} }
} }
for section, settings := range app.configBase { s := resp.Sections["ui"].Settings["language"]
if section == "order" { s.Options = app.lang.langOptions
resp[section] = settings.([]interface{}) s.Value = app.lang.langOptions[app.lang.chosenIndex]
} else { resp.Sections["ui"].Settings["language"] = s
resp[section] = make(map[string]interface{}) for sectName, section := range resp.Sections {
for key, values := range settings.(map[string]interface{}) { for settingName, setting := range section.Settings {
if key == "order" { val := app.config.Section(sectName).Key(settingName)
resp[section].(map[string]interface{})[key] = values.([]interface{}) s := resp.Sections[sectName].Settings[settingName]
} else { switch setting.Type {
resp[section].(map[string]interface{})[key] = values.(map[string]interface{}) case "text", "email", "select":
if key != "meta" { s.Value = val.MustString("")
dataType := resp[section].(map[string]interface{})[key].(map[string]interface{})["type"].(string) case "number":
configKey := app.config.Section(section).Key(key) s.Value = val.MustInt(0)
if dataType == "number" { case "bool":
if val, err := configKey.Int(); err == nil { s.Value = val.MustBool(false)
resp[section].(map[string]interface{})[key].(map[string]interface{})["value"] = val
} }
} else if dataType == "bool" { resp.Sections[sectName].Settings[settingName] = s
resp[section].(map[string]interface{})[key].(map[string]interface{})["value"] = configKey.MustBool(false)
} else if dataType == "select" && key == "language" {
resp[section].(map[string]interface{})[key].(map[string]interface{})["options"] = app.lang.langOptions
resp[section].(map[string]interface{})[key].(map[string]interface{})["value"] = app.lang.langOptions[app.lang.chosenIndex]
} else {
resp[section].(map[string]interface{})[key].(map[string]interface{})["value"] = configKey.String()
} }
} }
}
}
}
}
// resp["jellyfin"].(map[string]interface{})["language"].(map[string]interface{})["options"].([]string)
gc.JSON(200, resp) gc.JSON(200, resp)
} }

View File

@ -1,9 +1,13 @@
{ {
"order": [],
"sections": {
"jellyfin": { "jellyfin": {
"order": [],
"meta": { "meta": {
"name": "Jellyfin", "name": "Jellyfin",
"description": "Settings for connecting to Jellyfin" "description": "Settings for connecting to Jellyfin"
}, },
"settings": {
"username": { "username": {
"name": "Jellyfin Username", "name": "Jellyfin Username",
"required": true, "required": true,
@ -51,12 +55,15 @@
"value": 30, "value": 30,
"description": "Timeout of user cache in minutes. Set to 0 to disable." "description": "Timeout of user cache in minutes. Set to 0 to disable."
} }
}
}, },
"ui": { "ui": {
"order": [],
"meta": { "meta": {
"name": "General", "name": "General",
"description": "Settings related to the UI and program functionality." "description": "Settings related to the UI and program functionality."
}, },
"settings": {
"language": { "language": {
"name": "Language", "name": "Language",
"required": false, "required": false,
@ -188,12 +195,15 @@
"value": "", "value": "",
"description": "URL base for when running jfa-go with a reverse proxy in a subfolder." "description": "URL base for when running jfa-go with a reverse proxy in a subfolder."
} }
}
}, },
"password_validation": { "password_validation": {
"order": [],
"meta": { "meta": {
"name": "Password Validation", "name": "Password Validation",
"description": "Password validation (minimum length, etc.)" "description": "Password validation (minimum length, etc.)"
}, },
"settings": {
"enabled": { "enabled": {
"name": "Enabled", "name": "Enabled",
"required": false, "required": false,
@ -236,12 +246,15 @@
"type": "text", "type": "text",
"value": "0" "value": "0"
} }
}
}, },
"email": { "email": {
"order": [],
"meta": { "meta": {
"name": "Email", "name": "Email",
"description": "General email settings. Ignore if not using email features." "description": "General email settings. Ignore if not using email features."
}, },
"settings": {
"no_username": { "no_username": {
"name": "Use email addresses as username", "name": "Use email addresses as username",
"required": false, "required": false,
@ -307,12 +320,15 @@
"value": "Jellyfin", "value": "Jellyfin",
"description": "The name of the sender" "description": "The name of the sender"
} }
}
}, },
"password_resets": { "password_resets": {
"order": [],
"meta": { "meta": {
"name": "Password Resets", "name": "Password Resets",
"description": "Settings for the password reset handler." "description": "Settings for the password reset handler."
}, },
"settings": {
"enabled": { "enabled": {
"name": "Enabled", "name": "Enabled",
"required": false, "required": false,
@ -357,12 +373,15 @@
"value": "Password Reset - Jellyfin", "value": "Password Reset - Jellyfin",
"description": "Subject of password reset emails." "description": "Subject of password reset emails."
} }
}
}, },
"invite_emails": { "invite_emails": {
"order": [],
"meta": { "meta": {
"name": "Invite emails", "name": "Invite emails",
"description": "Settings for sending invites directly to users." "description": "Settings for sending invites directly to users."
}, },
"settings": {
"enabled": { "enabled": {
"name": "Enabled", "name": "Enabled",
"required": false, "required": false,
@ -406,12 +425,15 @@
"value": "http://accounts.jellyf.in:8056/invite", "value": "http://accounts.jellyf.in:8056/invite",
"description": "Base URL for jfa-go. This is necessary because using a reverse proxy means the program has no way of knowing the URL itself." "description": "Base URL for jfa-go. This is necessary because using a reverse proxy means the program has no way of knowing the URL itself."
} }
}
}, },
"notifications": { "notifications": {
"order": [],
"meta": { "meta": {
"name": "Notifications", "name": "Notifications",
"description": "Notification related settings." "description": "Notification related settings."
}, },
"settings": {
"enabled": { "enabled": {
"name": "Enabled", "name": "Enabled",
"required": "false", "required": "false",
@ -456,12 +478,15 @@
"value": "", "value": "",
"description": "Path to user creation notification email in plaintext." "description": "Path to user creation notification email in plaintext."
} }
}
}, },
"mailgun": { "mailgun": {
"order": [],
"meta": { "meta": {
"name": "Mailgun (Email)", "name": "Mailgun (Email)",
"description": "Mailgun API connection settings" "description": "Mailgun API connection settings"
}, },
"settings": {
"api_url": { "api_url": {
"name": "API URL", "name": "API URL",
"required": false, "required": false,
@ -476,12 +501,15 @@
"type": "text", "type": "text",
"value": "your api key" "value": "your api key"
} }
}
}, },
"smtp": { "smtp": {
"order": [],
"meta": { "meta": {
"name": "SMTP (Email)", "name": "SMTP (Email)",
"description": "SMTP Server connection settings." "description": "SMTP Server connection settings."
}, },
"settings": {
"username": { "username": {
"name": "Username", "name": "Username",
"required": false, "required": false,
@ -524,12 +552,15 @@
"type": "password", "type": "password",
"value": "smtp password" "value": "smtp password"
} }
}
}, },
"ombi": { "ombi": {
"order": [],
"meta": { "meta": {
"name": "Ombi Integration", "name": "Ombi Integration",
"description": "Connect to Ombi to automatically create both Ombi and Jellyfin accounts for new users. You'll need to create a user template for this to work. Once enabled, refresh to see an option in settings for this." "description": "Connect to Ombi to automatically create both Ombi and Jellyfin accounts for new users. You'll need to create a user template for this to work. Once enabled, refresh to see an option in settings for this."
}, },
"settings": {
"enabled": { "enabled": {
"name": "Enabled", "name": "Enabled",
"required": false, "required": false,
@ -556,12 +587,15 @@
"depends_true": "enabled", "depends_true": "enabled",
"description": "API Key. Get this from the first tab in Ombi settings." "description": "API Key. Get this from the first tab in Ombi settings."
} }
}
}, },
"deletion": { "deletion": {
"order": [],
"meta": { "meta": {
"name": "Account Deletion", "name": "Account Deletion",
"description": "Subject/email files for account deletion emails." "description": "Subject/email files for account deletion emails."
}, },
"settings": {
"subject": { "subject": {
"name": "Email subject", "name": "Email subject",
"required": false, "required": false,
@ -586,12 +620,15 @@
"value": "", "value": "",
"description": "Path to custom email in plain text" "description": "Path to custom email in plain text"
} }
}
}, },
"files": { "files": {
"order": [],
"meta": { "meta": {
"name": "File Storage", "name": "File Storage",
"description": "Optional settings for changing storage locations." "description": "Optional settings for changing storage locations."
}, },
"settings": {
"invites": { "invites": {
"name": "Invite Storage", "name": "Invite Storage",
"required": false, "required": false,
@ -666,3 +703,5 @@
} }
} }
} }
}
}

View File

@ -9,17 +9,17 @@ args = parser.parse_args()
with open(args.input, 'r') as f: with open(args.input, 'r') as f:
config = json.load(f) config = json.load(f)
newconfig = {"order": []} newconfig = {"sections": {}, "order": []}
for sect in config: for sect in config["sections"]:
newconfig["order"].append(sect) newconfig["order"].append(sect)
newconfig[sect] = {} newconfig["sections"][sect] = {}
newconfig[sect]["order"] = [] newconfig["sections"][sect]["order"] = []
newconfig[sect]["meta"] = config[sect]["meta"] newconfig["sections"][sect]["meta"] = config["sections"][sect]["meta"]
for setting in config[sect]: newconfig["sections"][sect]["settings"] = {}
if setting != "meta": for setting in config["sections"][sect]["settings"]:
newconfig[sect]["order"].append(setting) newconfig["sections"][sect]["order"].append(setting)
newconfig[sect][setting] = config[sect][setting] newconfig["sections"][sect]["settings"][setting] = config["sections"][sect]["settings"][setting]
with open(args.output, 'w') as f: with open(args.output, 'w') as f:
f.write(json.dumps(newconfig, indent=4)) f.write(json.dumps(newconfig, indent=4))

View File

@ -14,13 +14,14 @@ def generate_ini(base_file, ini_file):
ini = configparser.RawConfigParser(allow_no_value=True) ini = configparser.RawConfigParser(allow_no_value=True)
for section in config_base: for section in config_base["sections"]:
ini.add_section(section) ini.add_section(section)
for entry in config_base[section]: if "meta" in config_base["sections"][section]:
if "description" in config_base[section][entry]: ini.set(section, "; " + config_base["sections"][section]["meta"]["description"])
ini.set(section, "; " + config_base[section][entry]["description"]) for entry in config_base["sections"][section]["settings"]:
if entry != "meta": if "description" in config_base["sections"][section]["settings"][entry]:
value = config_base[section][entry]["value"] ini.set(section, "; " + config_base["sections"][section]["settings"][entry]["description"])
value = config_base["sections"][section]["settings"][entry]["value"]
if isinstance(value, bool): if isinstance(value, bool):
value = str(value).lower() value = str(value).lower()
else: else:

View File

@ -48,7 +48,7 @@ type appContext struct {
config *ini.File config *ini.File
configPath string configPath string
configBasePath string configBasePath string
configBase map[string]interface{} configBase settings
dataPath string dataPath string
localPath string localPath string
cssFile string cssFile string

View File

@ -127,3 +127,32 @@ type errorListDTO map[string]map[string]string
type configDTO map[string]interface{} type configDTO map[string]interface{}
// Below are for sending config
type meta struct {
Name string `json:"name"`
Description string `json:"description"`
}
type setting struct {
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
RequiresRestart bool `json:"requires_restart"`
Type string `json:"type"` // Type (string, number, bool, etc.)
Value interface{} `json:"value"`
Options []string `json:"options,omitempty"`
DependsTrue string `json:"depends_true,omitempty"` // If specified, this field is enabled when the specified bool setting is enabled.
DependsFalse string `json:"depends_false,omitempty"` // If specified, opposite behaviour of DependsTrue.
}
type section struct {
Meta meta `json:"meta"`
Order []string `json:"order"`
Settings map[string]setting `json:"settings"`
}
type settings struct {
Order []string `json:"order"`
Sections map[string]section `json:"sections"`
}