From 3e1d5d3ae139361802738bcb66818fa1594c7db5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 22 Aug 2025 10:53:04 +0200 Subject: [PATCH] config: add GetAPI --- client/graphql.go | 8 +------- config/config.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/client/graphql.go b/client/graphql.go index 3dacb8280bc9875e18c4eb0fda8b3908766833d6..6fabeadf085bb30aea7356e893ae0bc85856ea45 100644 --- a/client/graphql.go +++ b/client/graphql.go @@ -34,13 +34,7 @@ func Do(ctx context.Context, username string, svc string, query GraphQLQuery, result interface{}, ) error { conf := config.ForContext(ctx) - origin, _ := conf.Get(svc, "api-origin") - if origin == "" { - origin = config.GetOrigin(conf, svc, false) - } - if origin == "" { - panic(fmt.Errorf("No %s origin specified in config.ini", svc)) - } + origin := config.GetAPI(conf, svc, false) var ( contentType string diff --git a/config/config.go b/config/config.go index 0dbf1b3cbfba7d8376afa401794573ba4f76e305..2a88966bc8f9fae3ebfb37a77c285174b3c2554a 100644 --- a/config/config.go +++ b/config/config.go @@ -100,6 +100,11 @@ func LoadConfig(defaultAddr string) ini.File { return config } +// 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). func GetOrigin(conf ini.File, svc string, external bool) string { if external { origin, _ := conf.Get(svc, "origin") @@ -113,6 +118,38 @@ 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! +// +// Set "external" to true if the URL will be accessed by an external user (i.e. +// outside of the LAN). +func GetAPI(conf ini.File, svc string, external bool) string { + var candidates []string + + if external { + candidates = []string{ + "api-origin", + "origin", + } + } else { + candidates = []string{ + "api-internal-origin", + "internal-origin", + "api-origin", + "origin", + } + } + + for _, name := range candidates { + origin, ok := conf.Get(svc, name) + if ok { + return origin + } + } + + panic(fmt.Errorf("No suitable origin configured for requested API")) +} + const DefaultQueueSize = 512 // Returns the configured integer value for the given section and variable name.