From e272a4cf4e7b1fc600c64fcf802bd362b8bd543c Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Tue, 2 Dec 2025 15:37:06 +0100 Subject: [PATCH] config: add GetOwner and GetString These patterns are used often enough to justify a little convenience. Also fixes a few godoc comments while at it. --- config/config.go | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 5743e9a14cdecd83c79bd81c653fdea6efe09d9a..7164348b027527aaf075e24b1618b6146790b60f 100644 --- a/config/config.go +++ b/config/config.go @@ -27,7 +27,7 @@ var ( internalIPNet []net.IPNet ) -// Loads the application configuration. +// LoadConfig loads the application configuration. func LoadConfig() ini.File { var ( config ini.File @@ -85,8 +85,22 @@ func LoadConfig() ini.File { return config } -// Returns the URL (scheme, host, port) at which the web application for a -// given service can be found. +// GetOwner returns the owner name and email. Panics if not configured. +func GetOwner(conf ini.File) (ownerName, ownerEmail string) { + var ok bool + ownerName, ok = conf.Get("sr.ht", "owner-name") + if !ok { + panic("missing [sr.ht]owner-name in config") + } + ownerEmail, ok = conf.Get("sr.ht", "owner-email") + if !ok { + panic("missing [sr.ht]owner-email in config") + } + return +} + +// GetOrigin returns the URL (scheme, host, port) at which the web application +// for a given service can be found. // // Set "external" to true if the URL will be accessed by an external user (i.e. // outside of the LAN). @@ -103,8 +117,8 @@ func GetOrigin(conf ini.File, svc string, external bool) string { return origin } -// Returns the URL (scheme, host, port) at which the API for a given service -// can be found. Does not include the /query path! +// GetAPI returns the URL (scheme, host, port) at which the API for a given +// service can be found. Does not include the /query path! // // Set "external" to true if the URL will be accessed by an external user (i.e. // outside of the LAN). @@ -137,8 +151,18 @@ func GetAPI(conf ini.File, svc string, external bool) string { const DefaultQueueSize = 512 -// Returns the configured integer value for the given section and variable name. -// If nothing is configured, the default value will be returned. +// GetString returns the configured string value for the given section and +// variable name. If nothing is configured, the default value will be returned. +func GetString(conf ini.File, section, name string, defValue string) string { + value, ok := conf.Get(section, name) + if !ok { + value = defValue + } + return value +} + +// GetInt returns the configured integer value for the given section and +// variable name. If nothing is configured, the default value will be returned. // If an invalid integer is configured, this function will panic. func GetInt(conf ini.File, section, name string, defValue int) int { value := defValue @@ -151,8 +175,8 @@ func GetInt(conf ini.File, section, name string, defValue int) int { return value } -// Returns the configured boolean value for the given section and variable name. -// If nothing is configured, the default value will be returned. +// GetBool returns the configured boolean value for the given section and +// variable name. If nothing is configured, the default value will be returned. // If an invalid boolean is configured, this function will panic. func GetBool(conf ini.File, section, name string, defValue bool) bool { value := defValue @@ -169,8 +193,8 @@ func GetBool(conf ini.File, section, name string, defValue bool) bool { return value } -// Returns true if the given IP address is part of the internal networks as -// per the configuration of [sr.ht]internal-ipnet. +// IsInternalIP returns true if the given IP address is part of the internal +// networks as per the configuration of [sr.ht]internal-ipnet. func IsInternalIP(ip net.IP) bool { for _, net := range internalIPNet { if net.Contains(ip) {